feat(IHttpClientOptions):
Basispfad zu http-Client-Optionen hinzugefügt
This commit is contained in:
parent
c3a12ba5b7
commit
bd4d4856ea
@ -6,12 +6,12 @@ namespace DigitalData.Core.Abstractions.Client
|
|||||||
{
|
{
|
||||||
string Uri { get; init; }
|
string Uri { get; init; }
|
||||||
|
|
||||||
CookieCollection GetCookies(string route = "");
|
CookieCollection GetCookies(string path = "");
|
||||||
|
|
||||||
Task<HttpResponseMessage> FetchAsync(
|
Task<HttpResponseMessage> FetchAsync(
|
||||||
string? scheme = null,
|
string? scheme = null,
|
||||||
int? port = null,
|
int? port = null,
|
||||||
string? path = null,
|
string path = "",
|
||||||
Dictionary<string, string>? queryParams = null,
|
Dictionary<string, string>? queryParams = null,
|
||||||
HttpMethod? method = null,
|
HttpMethod? method = null,
|
||||||
HttpContent? body = null,
|
HttpContent? body = null,
|
||||||
|
|||||||
@ -3,5 +3,7 @@
|
|||||||
public interface IHttpClientOptions
|
public interface IHttpClientOptions
|
||||||
{
|
{
|
||||||
public string Uri { get; set; }
|
public string Uri { get; set; }
|
||||||
|
|
||||||
|
public string Path { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -9,24 +9,27 @@ namespace DigitalData.Core.Client
|
|||||||
public class BaseHttpClientService : IBaseHttpClientService
|
public class BaseHttpClientService : IBaseHttpClientService
|
||||||
{
|
{
|
||||||
protected readonly HttpClient _client;
|
protected readonly HttpClient _client;
|
||||||
protected readonly CookieContainer _cookies;
|
protected readonly CookieContainer _cookies;
|
||||||
|
|
||||||
[StringSyntax("Uri")]
|
[StringSyntax("Uri")]
|
||||||
public string Uri { get; init; }
|
public string Uri { get; init; }
|
||||||
|
|
||||||
|
public string Path { get; init; } = string.Empty;
|
||||||
|
|
||||||
public BaseHttpClientService(HttpClient client, CookieContainer cookieContainer, IOptions<HttpClientOptions> clientOptions)
|
public BaseHttpClientService(HttpClient client, CookieContainer cookieContainer, IOptions<HttpClientOptions> clientOptions)
|
||||||
{
|
{
|
||||||
_client = client;
|
_client = client;
|
||||||
_cookies = cookieContainer;
|
_cookies = cookieContainer;
|
||||||
Uri = clientOptions.Value.Uri;
|
Uri = clientOptions.Value.Uri.Trim(URI_TRIM_CHARS);
|
||||||
|
Path = clientOptions.Value.Path.Trim(URI_TRIM_CHARS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CookieCollection GetCookies(string route = "") => _cookies.GetCookies(uri: new Uri(Uri + route));
|
public CookieCollection GetCookies(string path = "") => _cookies.GetCookies(uri: new Uri(UriCombine(Uri, path, path.Trim(URI_TRIM_CHARS))));
|
||||||
|
|
||||||
public async Task<HttpResponseMessage> FetchAsync(
|
public async Task<HttpResponseMessage> FetchAsync(
|
||||||
string? scheme = null,
|
string? scheme = null,
|
||||||
int? port = null,
|
int? port = null,
|
||||||
string? path = null,
|
string path = "",
|
||||||
Dictionary<string, string>? queryParams = null,
|
Dictionary<string, string>? queryParams = null,
|
||||||
HttpMethod? method = null,
|
HttpMethod? method = null,
|
||||||
HttpContent? body = null,
|
HttpContent? body = null,
|
||||||
@ -41,11 +44,11 @@ namespace DigitalData.Core.Client
|
|||||||
|
|
||||||
// create URL
|
// create URL
|
||||||
var uriBuilder = new UriBuilder(Uri);
|
var uriBuilder = new UriBuilder(Uri);
|
||||||
if(scheme is not null)
|
if (scheme is not null)
|
||||||
uriBuilder.Scheme = scheme;
|
uriBuilder.Scheme = scheme;
|
||||||
if (port is int portInt)
|
if (port is int portInt)
|
||||||
uriBuilder.Port = portInt;
|
uriBuilder.Port = portInt;
|
||||||
uriBuilder.Path = path;
|
uriBuilder.Path = UriCombine(Path, path.Trim(URI_TRIM_CHARS));
|
||||||
|
|
||||||
// Add query parameters if provided
|
// Add query parameters if provided
|
||||||
if (queryParams?.Count > 0)
|
if (queryParams?.Count > 0)
|
||||||
@ -92,5 +95,9 @@ namespace DigitalData.Core.Client
|
|||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal static readonly char[] URI_TRIM_CHARS = { '\\', '/', ' ' };
|
||||||
|
|
||||||
|
internal static string UriCombine(params string[] paths) => System.IO.Path.Combine(paths).Replace("\\", "/");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -7,12 +7,16 @@ namespace DigitalData.Core.Client
|
|||||||
{
|
{
|
||||||
public static class DIExtensions
|
public static class DIExtensions
|
||||||
{
|
{
|
||||||
public static IServiceCollection AddHttpClientService(this IServiceCollection services, string uri)
|
public static IServiceCollection AddHttpClientService(this IServiceCollection services, string uri, string path = "")
|
||||||
{
|
{
|
||||||
services.TryAddSingleton<HttpClient>();
|
services.TryAddSingleton<HttpClient>();
|
||||||
services.TryAddSingleton<CookieContainer>();
|
services.TryAddSingleton<CookieContainer>();
|
||||||
services.AddSingleton<IBaseHttpClientService, BaseHttpClientService>();
|
services.AddSingleton<IBaseHttpClientService, BaseHttpClientService>();
|
||||||
services.Configure<HttpClientOptions>(opt => opt.Uri = uri);
|
services.Configure<HttpClientOptions>(opt =>
|
||||||
|
{
|
||||||
|
opt.Uri = uri;
|
||||||
|
opt.Path = path;
|
||||||
|
});
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
@ -22,11 +26,11 @@ namespace DigitalData.Core.Client
|
|||||||
{
|
{
|
||||||
services.TryAddSingleton<HttpClient>();
|
services.TryAddSingleton<HttpClient>();
|
||||||
services.TryAddSingleton<CookieContainer>();
|
services.TryAddSingleton<CookieContainer>();
|
||||||
services.AddSingleton<IHttpClientService<TClientOptions>, HttpClientService<TClientOptions>>();
|
services.TryAddSingleton<IHttpClientService<TClientOptions>, HttpClientService<TClientOptions>>();
|
||||||
services.Configure(clientOptions ?? (_ => { }));
|
services.Configure(clientOptions ?? (_ => { }));
|
||||||
|
|
||||||
if (setAsDefaultBase)
|
if (setAsDefaultBase)
|
||||||
services.AddSingleton<IBaseHttpClientService, HttpClientService<TClientOptions>>();
|
services.TryAddSingleton<IBaseHttpClientService, HttpClientService<TClientOptions>>();
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,5 +5,7 @@ namespace DigitalData.Core.Client
|
|||||||
public class HttpClientOptions : IHttpClientOptions
|
public class HttpClientOptions : IHttpClientOptions
|
||||||
{
|
{
|
||||||
public string Uri { get; set; } = string.Empty;
|
public string Uri { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
public string Path { get; set; } = string.Empty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -14,7 +14,7 @@ namespace DigitalData.Core.Tests.Client
|
|||||||
public void SetUp()
|
public void SetUp()
|
||||||
{
|
{
|
||||||
_serviceProvider = new ServiceCollection()
|
_serviceProvider = new ServiceCollection()
|
||||||
.AddHttpClientService("https://jsonplaceholder.typicode.com/todos")
|
.AddHttpClientService("https://jsonplaceholder.typicode.com", "todos")
|
||||||
.BuildServiceProvider();
|
.BuildServiceProvider();
|
||||||
|
|
||||||
_service = _serviceProvider.GetRequiredService<IBaseHttpClientService>();
|
_service = _serviceProvider.GetRequiredService<IBaseHttpClientService>();
|
||||||
@ -24,7 +24,7 @@ namespace DigitalData.Core.Tests.Client
|
|||||||
public async Task FetchJsonAsync_ShouldReturnJsonResponse()
|
public async Task FetchJsonAsync_ShouldReturnJsonResponse()
|
||||||
{
|
{
|
||||||
// Act
|
// Act
|
||||||
var expectedUserId = (int) await _service.FetchAsync("/1", sendWithCookie: false, saveCookie: false)
|
var expectedUserId = (int) await _service.FetchAsync(path: "/1", sendWithCookie: false, saveCookie: false)
|
||||||
.ThenAsync(res => res.Json())
|
.ThenAsync(res => res.Json())
|
||||||
.ThenAsync(todo => todo.userId);
|
.ThenAsync(todo => todo.userId);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user