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
httpClientHttpClientThe HTTP client to use for API requests.
loggerILoggerThe logger instance for logging operations. Can be null.
clientIdstringThe Strava API client ID.
clientSecretstringThe Strava API client secret.
accessTokenstringThe access token for authentication. Optional.
refreshTokenstringThe 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
httpClientHttpClientThe HTTP client used to send requests to the Strava API. Cannot be null.
loggerILoggerThe logger used for diagnostic and error messages. If null, a no-op logger is used.
stravaAuthorizationStravaAuthorizationThe 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
httpClientHttpClientThe HTTP client instance used to send requests to the Strava API. Must not be null.
loggerILogger<StravaClient>The logger used to record diagnostic and operational information for the StravaClient. Must not be null.
optionsStravaOptionsThe 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
httpClientHttpClientThe HttpClient instance to use for sending HTTP requests to the Strava API. Cannot be null.
clientIdstringThe client identifier assigned by Strava for API access. Cannot be null or empty.
clientSecretstringThe 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
httpClientFactoryIHttpClientFactoryThe factory used to create an HttpClient instance for communicating with the Strava API. Cannot be null.
loggerILogger<StravaClient>The logger used to record diagnostic and operational information. Cannot be null.
optionsStravaOptionsThe 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
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
uriStringOrPathstringThe path or URI string of the API endpoint to request. Cannot be null.
cancellationTokenCancellationTokenA 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
TThe 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
requestUriUriThe URI of the API endpoint to request. Cannot be null.
cancellationTokenCancellationTokenA 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
TThe 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
athleteIdlong?Optional; Athlete Id (default = logged in user).
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
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
requestUristringThe URI of the request.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
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
uriStringOrPathstringThe path or URI string to which the POST request is sent. Cannot be
null.bodyHttpContentThe request body to send with the POST request.
cancellationTokenCancellationTokenA 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
TResultThe 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
requestUriUriThe URI to which the POST request is sent. Cannot be
null.bodyHttpContentThe request body to send with the POST request.
cancellationTokenCancellationTokenA 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
TResultThe 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
uriStringOrPathstringThe path or URI String to which the PUT request is sent. Cannot be
null.bodyTBodyThe request body to send with the PUT request. May be
nullif the API allows an empty body.cancellationTokenCancellationTokenA 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
TBodyThe type of the request body to serialize and send.
TResultThe 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
requestUriUriThe URI to which the PUT request is sent. Cannot be
null.bodyTBodyThe request body to send with the PUT request. May be
nullif the API allows an empty body.cancellationTokenCancellationTokenA 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
TBodyThe type of the request body to serialize and send.
TResultThe type to which the response content is deserialized.