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; }
|
||||
|
||||
CookieCollection GetCookies(string route = "");
|
||||
CookieCollection GetCookies(string path = "");
|
||||
|
||||
Task<HttpResponseMessage> FetchAsync(
|
||||
string? scheme = null,
|
||||
int? port = null,
|
||||
string? path = null,
|
||||
string path = "",
|
||||
Dictionary<string, string>? queryParams = null,
|
||||
HttpMethod? method = null,
|
||||
HttpContent? body = null,
|
||||
|
||||
@ -3,5 +3,7 @@
|
||||
public interface IHttpClientOptions
|
||||
{
|
||||
public string Uri { get; set; }
|
||||
|
||||
public string Path { get; set; }
|
||||
}
|
||||
}
|
||||
@ -14,19 +14,22 @@ namespace DigitalData.Core.Client
|
||||
[StringSyntax("Uri")]
|
||||
public string Uri { get; init; }
|
||||
|
||||
public string Path { get; init; } = string.Empty;
|
||||
|
||||
public BaseHttpClientService(HttpClient client, CookieContainer cookieContainer, IOptions<HttpClientOptions> clientOptions)
|
||||
{
|
||||
_client = client;
|
||||
_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(
|
||||
string? scheme = null,
|
||||
int? port = null,
|
||||
string? path = null,
|
||||
string path = "",
|
||||
Dictionary<string, string>? queryParams = null,
|
||||
HttpMethod? method = null,
|
||||
HttpContent? body = null,
|
||||
@ -41,11 +44,11 @@ namespace DigitalData.Core.Client
|
||||
|
||||
// create URL
|
||||
var uriBuilder = new UriBuilder(Uri);
|
||||
if(scheme is not null)
|
||||
if (scheme is not null)
|
||||
uriBuilder.Scheme = scheme;
|
||||
if (port is int portInt)
|
||||
uriBuilder.Port = portInt;
|
||||
uriBuilder.Path = path;
|
||||
uriBuilder.Path = UriCombine(Path, path.Trim(URI_TRIM_CHARS));
|
||||
|
||||
// Add query parameters if provided
|
||||
if (queryParams?.Count > 0)
|
||||
@ -92,5 +95,9 @@ namespace DigitalData.Core.Client
|
||||
|
||||
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 IServiceCollection AddHttpClientService(this IServiceCollection services, string uri)
|
||||
public static IServiceCollection AddHttpClientService(this IServiceCollection services, string uri, string path = "")
|
||||
{
|
||||
services.TryAddSingleton<HttpClient>();
|
||||
services.TryAddSingleton<CookieContainer>();
|
||||
services.AddSingleton<IBaseHttpClientService, BaseHttpClientService>();
|
||||
services.Configure<HttpClientOptions>(opt => opt.Uri = uri);
|
||||
services.Configure<HttpClientOptions>(opt =>
|
||||
{
|
||||
opt.Uri = uri;
|
||||
opt.Path = path;
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
@ -22,11 +26,11 @@ namespace DigitalData.Core.Client
|
||||
{
|
||||
services.TryAddSingleton<HttpClient>();
|
||||
services.TryAddSingleton<CookieContainer>();
|
||||
services.AddSingleton<IHttpClientService<TClientOptions>, HttpClientService<TClientOptions>>();
|
||||
services.TryAddSingleton<IHttpClientService<TClientOptions>, HttpClientService<TClientOptions>>();
|
||||
services.Configure(clientOptions ?? (_ => { }));
|
||||
|
||||
if (setAsDefaultBase)
|
||||
services.AddSingleton<IBaseHttpClientService, HttpClientService<TClientOptions>>();
|
||||
services.TryAddSingleton<IBaseHttpClientService, HttpClientService<TClientOptions>>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
@ -5,5 +5,7 @@ namespace DigitalData.Core.Client
|
||||
public class HttpClientOptions : IHttpClientOptions
|
||||
{
|
||||
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()
|
||||
{
|
||||
_serviceProvider = new ServiceCollection()
|
||||
.AddHttpClientService("https://jsonplaceholder.typicode.com/todos")
|
||||
.AddHttpClientService("https://jsonplaceholder.typicode.com", "todos")
|
||||
.BuildServiceProvider();
|
||||
|
||||
_service = _serviceProvider.GetRequiredService<IBaseHttpClientService>();
|
||||
@ -24,7 +24,7 @@ namespace DigitalData.Core.Tests.Client
|
||||
public async Task FetchJsonAsync_ShouldReturnJsonResponse()
|
||||
{
|
||||
// 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(todo => todo.userId);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user