Table of Contents

Class StravaClient

Namespace
Tudormobile.Strava.Client
Assembly
Tudormobile.Strava.Client.dll

Provides a client for interacting with the Strava API, supporting authentication and various API operations.

public class StravaClient : IStravaClient, IActivitiesApi, IAthletesApi, IClubsApi, IGearsApi, IStravaApi
Inheritance
StravaClient
Implements
Inherited Members

Constructors

StravaClient(HttpClient, ILogger?, string, string, string?, string?)

Initializes a new instance of the StravaClient class with the specified HTTP client, logger, client ID, client secret, access token, and refresh token.

public StravaClient(HttpClient httpClient, ILogger? logger, string clientId, string clientSecret, string? accessToken = null, string? refreshToken = null)

Parameters

httpClient HttpClient

The HTTP client to use for API requests.

logger ILogger

The logger instance for logging operations. Can be null.

clientId string

The Strava API client ID.

clientSecret string

The Strava API client secret.

accessToken string

The access token for authentication. Optional.

refreshToken string

The refresh token for authentication. Optional.

StravaClient(HttpClient, ILogger?, StravaAuthorization)

Initializes a new instance of the StravaClient class with the specified HTTP client, logger, and Strava authorization credentials.

public StravaClient(HttpClient httpClient, ILogger? logger, StravaAuthorization stravaAuthorization)

Parameters

httpClient HttpClient

The HTTP client used to send requests to the Strava API. Cannot be null.

logger ILogger

The logger used for diagnostic and error messages. If null, a no-op logger is used.

stravaAuthorization StravaAuthorization

The Strava authorization credentials, including client ID and client secret. Cannot be null, and both ClientId and ClientSecret must be non-empty strings.

Exceptions

ArgumentNullException

Thrown if httpClient is null, or if stravaAuthorization is null, or if stravaAuthorization.ClientId or stravaAuthorization.ClientSecret is null.

ArgumentException

Thrown if stravaAuthorization.ClientId or stravaAuthorization.ClientSecret is an empty or whitespace string.

StravaClient(HttpClient, ILogger<StravaClient>, StravaOptions)

Initializes a new instance of the StravaClient class using the specified HTTP client, logger, and Strava API options.

public StravaClient(HttpClient httpClient, ILogger<StravaClient> logger, StravaOptions options)

Parameters

httpClient HttpClient

The HTTP client instance used to send requests to the Strava API. Must not be null.

logger ILogger<StravaClient>

The logger used to record diagnostic and operational information for the StravaClient. Must not be null.

options StravaOptions

The options containing Strava API credentials and tokens. Must not be null.

Remarks

This constructor simplifies initialization by accepting a StravaOptions object containing all required credentials and tokens. The provided HttpClient should be configured for use with the Strava API and is not disposed by the StravaClient.

StravaClient(HttpClient, string, string)

Initializes a new instance of the StravaClient class using the specified HTTP client and client credentials.

public StravaClient(HttpClient httpClient, string clientId, string clientSecret)

Parameters

httpClient HttpClient

The HttpClient instance to use for sending HTTP requests to the Strava API. Cannot be null.

clientId string

The client identifier assigned by Strava for API access. Cannot be null or empty.

clientSecret string

The client secret associated with the Strava application. Cannot be null or empty.

Remarks

This constructor is intended for scenarios where only the basic client credentials are available. For advanced configuration, use an overload that accepts additional parameters such as a logger or access tokens.

StravaClient(IHttpClientFactory, ILogger<StravaClient>, StravaOptions)

Initializes a new instance of the StravaClient class using an IHttpClientFactory to create the HTTP client, along with the specified logger and options.

public StravaClient(IHttpClientFactory httpClientFactory, ILogger<StravaClient> logger, StravaOptions options)

Parameters

httpClientFactory IHttpClientFactory

The factory used to create an HttpClient instance for communicating with the Strava API. Cannot be null.

logger ILogger<StravaClient>

The logger used to record diagnostic and operational information. Cannot be null.

options StravaOptions

The configuration options for the StravaClient. Cannot be null.

Remarks

This constructor is useful when integrating with dependency injection frameworks that provide IHttpClientFactory for managing HttpClient lifetimes.

Properties

IsAuthenticated

Gets a value indicating whether the client is authenticated.

public bool IsAuthenticated { get; }

Property Value

bool

Methods

GetApiResultAsync<T>(string, CancellationToken)

Sends an asynchronous HTTP request to the specified URI and returns the result as an ApiResult<T>.

public Task<ApiResult<T>> GetApiResultAsync<T>(string uriStringOrPath, CancellationToken cancellationToken = default)

Parameters

uriStringOrPath string

The path or URI string of the API endpoint to request. Cannot be null.

cancellationToken CancellationToken

A token to monitor for cancellation requests. The default value is None.

Returns

Task<ApiResult<T>>

A task that represents the asynchronous operation. The task result contains an ApiResult<T> representing the outcome of the API request, including the deserialized response data or error information.

Type Parameters

T

The type to which the response content is deserialized.

GetApiResultAsync<T>(Uri, CancellationToken)

Sends an asynchronous HTTP request to the specified URI and returns the result as an ApiResult<T>.

public Task<ApiResult<T>> GetApiResultAsync<T>(Uri requestUri, CancellationToken cancellationToken = default)

Parameters

requestUri Uri

The URI of the API endpoint to request. Cannot be null.

cancellationToken CancellationToken

A token to monitor for cancellation requests. The default value is None.

Returns

Task<ApiResult<T>>

A task that represents the asynchronous operation. The task result contains an ApiResult<T> representing the outcome of the API request, including the deserialized response data or error information.

Type Parameters

T

The type to which the response content is deserialized.

GetAthleteAsync(long?, CancellationToken)

Retrieve Athlete record by Id for logged in user.

public Task<ApiResult<Athlete>> GetAthleteAsync(long? athleteId = 0, CancellationToken cancellationToken = default)

Parameters

athleteId long?

Optional; Athlete Id (default = logged in user).

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<ApiResult<Athlete>>

Athlete record associated with the Id.

Remarks

Returns the currently authenticated athlete. Tokens with profile:read_all scope will receive a detailed athlete representation; all others will receive a summary representation.

GetStreamAsync(string, CancellationToken)

Retrieves a stream from the Strava API.

public Task<Stream> GetStreamAsync(string requestUri, CancellationToken cancellationToken = default)

Parameters

requestUri string

The URI of the request.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<Stream>

A Stream containing the API response.

Remarks

It is the responsibility of the caller to dispose of the returned stream.

PostApiResultAsync<TResult>(string, HttpContent, CancellationToken)

Sends an HTTP POST request to the specified URI string or path with the provided request body and returns the deserialized API result.

public Task<ApiResult<TResult>> PostApiResultAsync<TResult>(string uriStringOrPath, HttpContent body, CancellationToken cancellationToken = default)

Parameters

uriStringOrPath string

The path or URI string to which the POST request is sent. Cannot be null.

body HttpContent

The request body to send with the POST request.

cancellationToken CancellationToken

A token to monitor for cancellation requests. The default value is None.

Returns

Task<ApiResult<TResult>>

A task that represents the asynchronous operation. The task result contains an ApiResult<T> representing the deserialized response from the API.

Type Parameters

TResult

The type to which the response content is deserialized.

PostApiResultAsync<TResult>(Uri, HttpContent, CancellationToken)

Sends an HTTP POST request to the specified URI with the provided request body and returns the deserialized API result.

public Task<ApiResult<TResult>> PostApiResultAsync<TResult>(Uri requestUri, HttpContent body, CancellationToken cancellationToken = default)

Parameters

requestUri Uri

The URI to which the POST request is sent. Cannot be null.

body HttpContent

The request body to send with the POST request.

cancellationToken CancellationToken

A token to monitor for cancellation requests. The default value is None.

Returns

Task<ApiResult<TResult>>

A task that represents the asynchronous operation. The task result contains an ApiResult<T> representing the deserialized response from the API.

Type Parameters

TResult

The type to which the response content is deserialized.

PutApiResultAsync<TBody, TResult>(string, TBody?, CancellationToken)

Sends an HTTP PUT request to the specified URI with the provided request body and returns the deserialized API result.

public Task<ApiResult<TResult>> PutApiResultAsync<TBody, TResult>(string uriStringOrPath, TBody? body, CancellationToken cancellationToken = default)

Parameters

uriStringOrPath string

The path or URI String to which the PUT request is sent. Cannot be null.

body TBody

The request body to send with the PUT request. May be null if the API allows an empty body.

cancellationToken CancellationToken

A token to monitor for cancellation requests. The default value is None.

Returns

Task<ApiResult<TResult>>

A task that represents the asynchronous operation. The task result contains an ApiResult<T> representing the deserialized response from the API.

Type Parameters

TBody

The type of the request body to serialize and send.

TResult

The type to which the response content is deserialized.

PutApiResultAsync<TBody, TResult>(Uri, TBody?, CancellationToken)

Sends an HTTP PUT request to the specified URI with the provided request body and returns the deserialized API result.

public Task<ApiResult<TResult>> PutApiResultAsync<TBody, TResult>(Uri requestUri, TBody? body, CancellationToken cancellationToken = default)

Parameters

requestUri Uri

The URI to which the PUT request is sent. Cannot be null.

body TBody

The request body to send with the PUT request. May be null if the API allows an empty body.

cancellationToken CancellationToken

A token to monitor for cancellation requests. The default value is None.

Returns

Task<ApiResult<TResult>>

A task that represents the asynchronous operation. The task result contains an ApiResult<T> representing the deserialized response from the API.

Type Parameters

TBody

The type of the request body to serialize and send.

TResult

The type to which the response content is deserialized.