Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add base url property to ApiClient #142

Merged
merged 4 commits into from
Aug 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
/// 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 @@
}
}

/// <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 @@
client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrHigher;
#endif

client.BaseAddress = _baseAddress;
return client;
}

Expand Down Expand Up @@ -362,40 +398,41 @@
/// <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
}
}
}
Loading