Skip to content

Commit

Permalink
Merge pull request #194 from AdrianJSClark/expose-dataclient-implemen…
Browse files Browse the repository at this point in the history
…tation

Make "DataClient" Class "public"
  • Loading branch information
AdrianJSClark authored Nov 26, 2023
2 parents ced98dc + 5d65f66 commit e77a2b1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/Aydsko.iRacingData/DataClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@

namespace Aydsko.iRacingData;

internal class DataClient(HttpClient httpClient,
ILogger<DataClient> logger,
iRacingDataClientOptions options,
CookieContainer cookieContainer)
/// <summary>Default implementation of the client to access the iRacing "/data" API endpoints.</summary>
/// <remarks>
/// Instead of creating an instance of this class directly, it is recommended that you register the library components in the services
/// collection using <see cref="ServicesExtensions.AddIRacingDataApi(Microsoft.Extensions.DependencyInjection.IServiceCollection)"/>
/// and resolve <see cref="IDataClient"/> service from there.
/// </remarks>
public class DataClient(HttpClient httpClient,
ILogger<DataClient> logger,
iRacingDataClientOptions options,
CookieContainer cookieContainer)
: IDataClient
{
public bool IsLoggedIn { get; private set; }
Expand Down Expand Up @@ -2167,8 +2173,17 @@ protected virtual async Task<DataResponse<TData>> CreateResponseViaInfoLinkAsync
return BuildDataResponse<(TData Header, TChunkData[] Results)>(response.Headers, (headerData, searchResults.ToArray()), logger);
}

private void HandleUnsuccessfulResponse(HttpResponseMessage httpResponse, string content, ILogger logger)
protected virtual void HandleUnsuccessfulResponse(HttpResponseMessage httpResponse, string content, ILogger logger)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(httpResponse);
#else
if (httpResponse is null)
{
throw new ArgumentNullException(nameof(httpResponse));
}
#endif

string? errorDescription;
Exception? exception;

Expand Down Expand Up @@ -2212,8 +2227,17 @@ private void HandleUnsuccessfulResponse(HttpResponseMessage httpResponse, string
}
}

private static DataResponse<TData> BuildDataResponse<TData>(HttpResponseHeaders headers, TData data, ILogger logger, DateTimeOffset? expires = null)
protected static DataResponse<TData> BuildDataResponse<TData>(HttpResponseHeaders headers, TData data, ILogger logger, DateTimeOffset? expires = null)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(headers);
#else
if (headers is null)
{
throw new ArgumentNullException(nameof(headers));
}
#endif

var response = new DataResponse<TData> { Data = data };

if (headers.TryGetValues("x-ratelimit-remaining", out var remainingValues)
Expand Down
1 change: 1 addition & 0 deletions src/Aydsko.iRacingData/IDataClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace Aydsko.iRacingData;

/// <summary>Main client to access the iRacing "/data" API endpoints.</summary>
public interface IDataClient
{
/// <summary>Supply the username and password if they weren't supplied through the <see cref="iRacingDataClientOptions"/> object.</summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Aydsko.iRacingData/Package Release Notes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
- Fix "Implementing SaveCookies and RestoreCookies does not prevent unnecessary logins" (Issue: #186)
- If you implement the "SaveCookies" and "RestoreCookies" methods and the cookies are for the correct domain, then the library will not attempt to login again.

- "DataClient" class is now "public" so it can be used outside of the library. It is still recommended to resolve the "IDataClient" interface from the DI container in most situations.



Contributions:
Expand Down

0 comments on commit e77a2b1

Please sign in to comment.