Skip to content

Commit

Permalink
Merge branch 'master' into switch-source-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
aspriddell authored Aug 9, 2024
2 parents d6418d6 + e49dd76 commit 9c2224d
Showing 1 changed file with 57 additions and 20 deletions.
77 changes: 57 additions & 20 deletions DragonFruit.Data/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,38 @@ namespace DragonFruit.Data
/// Represents a strongly-typed serializer version of <see cref="ApiClient"/>
/// </summary>
/// <typeparam name="T">The type of the <see cref="ApiSerializer"/></typeparam>
public class ApiClient<T>() : ApiClient(Activator.CreateInstance<T>()) where T : ApiSerializer, new();
public class ApiClient<T> : ApiClient where T : ApiSerializer, new()
{
public ApiClient()
: base(new T())
{
}

public ApiClient(Uri baseAddress)
: base(new T(), baseAddress)
{
}
}

/// <summary>
/// The <see cref="ApiClient"/> responsible for building, submitting and processing HTTP requests
/// </summary>
public class ApiClient
{
private HttpClient _client;
private Uri _baseAddress;

public ApiClient(ApiSerializer serializer)

Check notice on line 46 in DragonFruit.Data/ApiClient.cs

View workflow job for this annotation

GitHub Actions / quality

Convert into primary constructor in DragonFruit.Data\ApiClient.cs on line 46
{
Serializers = new SerializerResolver(serializer);
}

public ApiClient(ApiSerializer serializer, Uri baseAddress)
: this(serializer)
{
_baseAddress = baseAddress;
}

~ApiClient()
{
_client?.Dispose();
Expand Down Expand Up @@ -69,6 +87,23 @@ public string UserAgent
}
}

/// <summary>
/// Gets or sets the <see cref="Uri"/> that should act as the base address for relative-URI requests
/// </summary>
public Uri BaseAddress
{
get => _client?.BaseAddress ?? _baseAddress;
set
{
_baseAddress = value;

if (_client != null)
{
_client.BaseAddress = value;
}
}
}

/// <summary>
/// The <see cref="SerializerResolver"/> instance used to resolve serializers for requests.
/// Caches and reused serializers where possible.
Expand Down Expand Up @@ -327,6 +362,7 @@ protected virtual HttpClient CreateClient()
client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrHigher;
#endif

client.BaseAddress = _baseAddress;
return client;
}

Expand Down Expand Up @@ -362,40 +398,41 @@ protected virtual async Task<T> ValidateAndProcess<T>(HttpResponseMessage respon
/// <returns>The <see cref="HttpRequestMessage"/> to send</returns>
protected virtual async ValueTask<HttpRequestMessage> BuildRequest(ApiRequest request, string expectedContentType)
{
if (request is IRequestExecutingCallback callback)
switch (request)
{
callback.OnRequestExecuting(this);
}
case IRequestExecutingCallback callback:

Check warning on line 403 in DragonFruit.Data/ApiClient.cs

View workflow job for this annotation

GitHub Actions / quality

Suspicious type check: there is no type in the solution that is inherited from both 'DragonFruit.Data.ApiRequest' and 'DragonFruit.Data.Requests.IRequestExecutingCallback' in DragonFruit.Data\ApiClient.cs on line 403
callback.OnRequestExecuting(this);
break;

if (request is IAsyncRequestExecutingCallback asyncCallback)
{
await asyncCallback.OnRequestExecuting(this);
case IAsyncRequestExecutingCallback asyncCallback:

Check warning on line 407 in DragonFruit.Data/ApiClient.cs

View workflow job for this annotation

GitHub Actions / quality

Suspicious type check: there is no type in the solution that is inherited from both 'DragonFruit.Data.ApiRequest' and 'DragonFruit.Data.Requests.IAsyncRequestExecutingCallback' in DragonFruit.Data\ApiClient.cs on line 407
await asyncCallback.OnRequestExecuting(this);
break;
}

var requestMessage = request is IRequestBuilder rb
? rb.BuildRequest(Serializers)
: ReflectionRequestMessageBuilder.CreateHttpRequestMessage(request, Serializers);

var requestMessage = (request as IRequestBuilder)?.BuildRequest(Serializers) ?? ReflectionRequestMessageBuilder.CreateHttpRequestMessage(request, Serializers);

Check warning on line 412 in DragonFruit.Data/ApiClient.cs

View workflow job for this annotation

GitHub Actions / quality

Suspicious cast: there is no type in the solution which is inherited from both 'DragonFruit.Data.ApiRequest' and 'DragonFruit.Data.Requests.IRequestBuilder' in DragonFruit.Data\ApiClient.cs on line 412
requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(expectedContentType));

return requestMessage;
}

public static HttpMessageHandler CreateDefaultHandler()
{
#if NETSTANDARD2_0
#if !NETSTANDARD2_0
if (SocketsHttpHandler.IsSupported)
{
return new SocketsHttpHandler
{
UseCookies = false,
AutomaticDecompression = DecompressionMethods.All,
PooledConnectionLifetime = TimeSpan.FromMinutes(10)
};
}
#endif
return new HttpClientHandler
{
UseCookies = false,
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip
};
#else
return new SocketsHttpHandler
{
UseCookies = false,
AutomaticDecompression = DecompressionMethods.All,
PooledConnectionLifetime = TimeSpan.FromMinutes(10)
};
#endif
}
}
}

0 comments on commit 9c2224d

Please sign in to comment.