Compare commits
14 Commits
0c2334cefb
...
dd3d6e70cc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dd3d6e70cc | ||
|
|
02a87309df | ||
|
|
0f7bdc9d0e | ||
|
|
f9df2fb29e | ||
|
|
ef7da0e52c | ||
|
|
f602a842be | ||
|
|
52a7664e57 | ||
|
|
ea3d1312b8 | ||
|
|
3b8b315fea | ||
|
|
c65eefb954 | ||
|
|
997fd533ac | ||
|
|
bcfb5a8a70 | ||
|
|
049e9977f4 | ||
|
|
0334fc4cdf |
@@ -4,19 +4,17 @@ namespace DigitalData.Core.Abstractions.Client
|
|||||||
{
|
{
|
||||||
public interface IBaseHttpClientService
|
public interface IBaseHttpClientService
|
||||||
{
|
{
|
||||||
string Uri { get; init; }
|
|
||||||
|
|
||||||
CookieCollection GetCookies(string path = "");
|
CookieCollection GetCookies(string path = "");
|
||||||
|
|
||||||
Task<HttpResponseMessage> FetchAsync(
|
Task<HttpResponseMessage> FetchAsync(
|
||||||
string? scheme = null,
|
string? scheme = null,
|
||||||
int? port = null,
|
int? port = null,
|
||||||
string path = "",
|
string path = "",
|
||||||
Dictionary<string, object?>? queryParams = null,
|
IEnumerable<KeyValuePair<string, object?>>? queryParams = null,
|
||||||
HttpMethod? method = null,
|
HttpMethod? method = null,
|
||||||
HttpContent? body = null,
|
HttpContent? body = null,
|
||||||
Dictionary<string, string>? form = null,
|
IEnumerable<KeyValuePair<string, object>>? form = null,
|
||||||
Dictionary<string, string>? headers = null,
|
IEnumerable<KeyValuePair<string, object>>? headers = null,
|
||||||
bool sendWithCookie = true,
|
bool sendWithCookie = true,
|
||||||
bool saveCookie = true
|
bool saveCookie = true
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -2,8 +2,12 @@
|
|||||||
{
|
{
|
||||||
public interface IHttpClientOptions
|
public interface IHttpClientOptions
|
||||||
{
|
{
|
||||||
public string Uri { get; set; }
|
string Uri { get; init; }
|
||||||
|
|
||||||
public string Path { get; set; }
|
string? Path { get; init; }
|
||||||
|
|
||||||
|
IEnumerable<KeyValuePair<string, object>>? Headers { get; init; }
|
||||||
|
|
||||||
|
IEnumerable<KeyValuePair<string, object?>>? QueryParams { get; init; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -17,9 +17,9 @@
|
|||||||
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
|
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
|
||||||
<PackAsTool>False</PackAsTool>
|
<PackAsTool>False</PackAsTool>
|
||||||
<PackageIcon>core_icon.png</PackageIcon>
|
<PackageIcon>core_icon.png</PackageIcon>
|
||||||
<Version>2.1.0.0</Version>
|
<Version>2.2.0</Version>
|
||||||
<AssemblyVersion>2.1.0.0</AssemblyVersion>
|
<AssemblyVersion>2.2.0</AssemblyVersion>
|
||||||
<FileVersion>2.1.0.0</FileVersion>
|
<FileVersion>2.2.0</FileVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using DigitalData.Core.Abstractions.Client;
|
using DigitalData.Core.Abstractions.Client;
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
@@ -12,43 +11,75 @@ namespace DigitalData.Core.Client
|
|||||||
protected readonly CookieContainer _cookies;
|
protected readonly CookieContainer _cookies;
|
||||||
|
|
||||||
[StringSyntax("Uri")]
|
[StringSyntax("Uri")]
|
||||||
public string Uri { get; init; }
|
protected readonly string _uri;
|
||||||
|
|
||||||
public string Path { get; init; } = string.Empty;
|
protected readonly string _path;
|
||||||
|
|
||||||
public BaseHttpClientService(HttpClient client, CookieContainer cookieContainer, IOptions<HttpClientOptions> clientOptions)
|
protected IEnumerable<KeyValuePair<string, object>>? _headers;
|
||||||
|
|
||||||
|
protected IEnumerable<KeyValuePair<string, object?>>? _queryParams;
|
||||||
|
|
||||||
|
public BaseHttpClientService(HttpClient client, CookieContainer cookieContainer, IHttpClientOptions clientOptions)
|
||||||
{
|
{
|
||||||
_client = client;
|
_client = client;
|
||||||
_cookies = cookieContainer;
|
_cookies = cookieContainer;
|
||||||
Uri = clientOptions.Value.Uri.Trim(URI_TRIM_CHARS);
|
_uri = clientOptions.Uri.Trim(URI_TRIM_CHARS);
|
||||||
Path = clientOptions.Value.Path.Trim(URI_TRIM_CHARS);
|
_path = clientOptions.Path?.Trim(URI_TRIM_CHARS) ?? string.Empty;
|
||||||
|
_headers = clientOptions.Headers;
|
||||||
|
_queryParams = clientOptions.QueryParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CookieCollection GetCookies(string path = "") => _cookies.GetCookies(uri: new Uri(UriCombine(Uri, path, path.Trim(URI_TRIM_CHARS))));
|
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 = "",
|
string path = "",
|
||||||
Dictionary<string, object?>? queryParams = null,
|
IEnumerable<KeyValuePair<string, object?>>? queryParams = null,
|
||||||
HttpMethod? method = null,
|
HttpMethod? method = null,
|
||||||
HttpContent? body = null,
|
HttpContent? body = null,
|
||||||
Dictionary<string, string>? form = null,
|
IEnumerable<KeyValuePair<string, object>>? form = null,
|
||||||
Dictionary<string, string>? headers = null,
|
IEnumerable<KeyValuePair<string, object>>? headers = null,
|
||||||
bool sendWithCookie = true,
|
bool sendWithCookie = true,
|
||||||
bool saveCookie = true
|
bool saveCookie = true
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
// merge with default headers
|
||||||
|
if(_headers is not null)
|
||||||
|
{
|
||||||
|
if (headers is null)
|
||||||
|
headers = _headers;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var mergedHeaders = headers.ToList();
|
||||||
|
mergedHeaders.AddRange(_headers);
|
||||||
|
headers = mergedHeaders;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add default query parameters
|
||||||
|
if(_queryParams is not null)
|
||||||
|
{
|
||||||
|
if (queryParams is null)
|
||||||
|
queryParams = _queryParams;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var mergedQueryParams = queryParams.ToList();
|
||||||
|
mergedQueryParams.AddRange(_queryParams);
|
||||||
|
queryParams = mergedQueryParams;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// set default HTTP method as GET
|
// set default HTTP method as GET
|
||||||
method ??= HttpMethod.Get;
|
method ??= HttpMethod.Get;
|
||||||
|
|
||||||
// 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 = UriCombine(Path, path?.Trim(URI_TRIM_CHARS) ?? string.Empty);
|
uriBuilder.Path = UriCombine(_path, path?.Trim(URI_TRIM_CHARS) ?? string.Empty);
|
||||||
|
|
||||||
// Add query parameters if provided
|
// Add query parameters if provided
|
||||||
if (queryParams?.Any() ?? false)
|
if (queryParams?.Any() ?? false)
|
||||||
@@ -76,7 +107,7 @@ namespace DigitalData.Core.Client
|
|||||||
var requestMessage = new HttpRequestMessage(method, requestUri);
|
var requestMessage = new HttpRequestMessage(method, requestUri);
|
||||||
|
|
||||||
// Add headers if provided
|
// Add headers if provided
|
||||||
headers?.ForEach(header => requestMessage.Headers.Add(header.Key, header.Value));
|
headers?.ForEach(header => requestMessage.Headers.Add(header.Key, header.Value.ToString()));
|
||||||
|
|
||||||
// Add cookie to request
|
// Add cookie to request
|
||||||
if (sendWithCookie)
|
if (sendWithCookie)
|
||||||
@@ -94,7 +125,7 @@ namespace DigitalData.Core.Client
|
|||||||
else if (body != null)
|
else if (body != null)
|
||||||
requestMessage.Content = body;
|
requestMessage.Content = body;
|
||||||
else if (form != null)
|
else if (form != null)
|
||||||
requestMessage.Content = new FormUrlEncodedContent(form);
|
requestMessage.Content = new FormUrlEncodedContent(form.Select(e => KeyValuePair.Create(e.Key, e.Value.ToString())));
|
||||||
|
|
||||||
var response = await _client.SendAsync(requestMessage);
|
var response = await _client.SendAsync(requestMessage);
|
||||||
|
|
||||||
|
|||||||
@@ -1,37 +1,36 @@
|
|||||||
using DigitalData.Core.Abstractions.Client;
|
using DigitalData.Core.Abstractions.Client;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
|
||||||
namespace DigitalData.Core.Client
|
namespace DigitalData.Core.Client
|
||||||
{
|
{
|
||||||
public static class DIExtensions
|
public static class DIExtensions
|
||||||
{
|
{
|
||||||
public static IServiceCollection AddHttpClientService(this IServiceCollection services, string uri, string path = "")
|
internal static IServiceCollection AddHttpClientServiceDefaults(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.TryAddSingleton<HttpClient>();
|
services.TryAddSingleton<HttpClient>();
|
||||||
services.TryAddSingleton<CookieContainer>();
|
services.TryAddSingleton<CookieContainer>();
|
||||||
services.AddSingleton<IBaseHttpClientService, BaseHttpClientService>();
|
services.TryAddSingleton<IBaseHttpClientService, BaseHttpClientService>();
|
||||||
services.Configure<HttpClientOptions>(opt =>
|
|
||||||
{
|
|
||||||
opt.Uri = uri;
|
|
||||||
opt.Path = path;
|
|
||||||
});
|
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IServiceCollection AddHttpClientService<TClientOptions>(this IServiceCollection services, Action<TClientOptions>? clientOptions = null, bool setAsDefaultBase = false)
|
public static IServiceCollection AddHttpClientService<THttpClientOptions>(this IServiceCollection services, IConfigurationSection section)
|
||||||
where TClientOptions : HttpClientOptions
|
where THttpClientOptions : class, IHttpClientOptions
|
||||||
{
|
{
|
||||||
services.TryAddSingleton<HttpClient>();
|
services.AddHttpClientServiceDefaults();
|
||||||
services.TryAddSingleton<CookieContainer>();
|
services.TryAddSingleton<IHttpClientService<THttpClientOptions>, HttpClientService<THttpClientOptions>>();
|
||||||
services.TryAddSingleton<IHttpClientService<TClientOptions>, HttpClientService<TClientOptions>>();
|
return services.Configure<THttpClientOptions>(section);
|
||||||
services.Configure(clientOptions ?? (_ => { }));
|
}
|
||||||
|
|
||||||
if (setAsDefaultBase)
|
|
||||||
services.TryAddSingleton<IBaseHttpClientService, HttpClientService<TClientOptions>>();
|
|
||||||
|
|
||||||
|
public static IServiceCollection AddHttpClientService<THttpClientOptions>(this IServiceCollection services, THttpClientOptions options)
|
||||||
|
where THttpClientOptions : class, IHttpClientOptions
|
||||||
|
{
|
||||||
|
services.AddHttpClientServiceDefaults();
|
||||||
|
services.TryAddSingleton<IHttpClientService<THttpClientOptions>, HttpClientService<THttpClientOptions>>();
|
||||||
|
services.TryAddSingleton(Options.Create(options));
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<Description>This package provides HTTP client extension methods for the DigitalData.Core library, offering simplified and asynchronous methods for fetching and handling HTTP responses. It includes utility methods for sending GET requests, reading response content as text or JSON, and deserializing JSON into dynamic or strongly-typed objects using Newtonsoft.Json. These extensions facilitate efficient and easy-to-read HTTP interactions in client applications.</Description>
|
<Description>This package provides HTTP client extension methods for the DigitalData.Core library, offering simplified and asynchronous methods for fetching and handling HTTP responses. It includes utility methods for sending GET requests, reading response content as text or JSON, and deserializing JSON into dynamic or strongly-typed objects using Newtonsoft.Json. These extensions facilitate efficient and easy-to-read HTTP interactions in client applications.</Description>
|
||||||
<PackageId>DigitalData.Core.Client</PackageId>
|
<PackageId>DigitalData.Core.Client</PackageId>
|
||||||
<Version>1.1.0</Version>
|
<Version>2.0.0</Version>
|
||||||
<Authors>Digital Data GmbH</Authors>
|
<Authors>Digital Data GmbH</Authors>
|
||||||
<Company>Digital Data GmbH</Company>
|
<Company>Digital Data GmbH</Company>
|
||||||
<Product>Digital Data GmbH</Product>
|
<Product>Digital Data GmbH</Product>
|
||||||
@@ -15,8 +15,8 @@
|
|||||||
<PackageIcon>core_icon.png</PackageIcon>
|
<PackageIcon>core_icon.png</PackageIcon>
|
||||||
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
|
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
|
||||||
<PackageTags>digital data core http client json serilization</PackageTags>
|
<PackageTags>digital data core http client json serilization</PackageTags>
|
||||||
<AssemblyVersion>1.1.0</AssemblyVersion>
|
<AssemblyVersion>2.0.0</AssemblyVersion>
|
||||||
<FileVersion>1.1.0</FileVersion>
|
<FileVersion>2.0.0</FileVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -29,6 +29,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
using DigitalData.Core.Abstractions.Client;
|
|
||||||
|
|
||||||
namespace DigitalData.Core.Client
|
|
||||||
{
|
|
||||||
public class HttpClientOptions : IHttpClientOptions
|
|
||||||
{
|
|
||||||
public string Uri { get; set; } = string.Empty;
|
|
||||||
|
|
||||||
public string Path { get; set; } = string.Empty;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,9 +5,9 @@ using System.Net;
|
|||||||
namespace DigitalData.Core.Client
|
namespace DigitalData.Core.Client
|
||||||
{
|
{
|
||||||
public class HttpClientService<TClientOptions> : BaseHttpClientService, IHttpClientService<TClientOptions>, IBaseHttpClientService
|
public class HttpClientService<TClientOptions> : BaseHttpClientService, IHttpClientService<TClientOptions>, IBaseHttpClientService
|
||||||
where TClientOptions : HttpClientOptions
|
where TClientOptions : class, IHttpClientOptions
|
||||||
{
|
{
|
||||||
public HttpClientService(HttpClient client, CookieContainer cookieContainer, IOptions<TClientOptions> clientOptions) : base(client, cookieContainer, clientOptions)
|
public HttpClientService(HttpClient client, CookieContainer cookieContainer, IOptions<TClientOptions> clientOptions) : base(client, cookieContainer, clientOptions.Value)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,8 +53,8 @@ Global
|
|||||||
{0B051A5F-BD38-47D1-BAFF-D44BA30D3FB7}.Debug|Any CPU.Build.0 = Release|Any CPU
|
{0B051A5F-BD38-47D1-BAFF-D44BA30D3FB7}.Debug|Any CPU.Build.0 = Release|Any CPU
|
||||||
{0B051A5F-BD38-47D1-BAFF-D44BA30D3FB7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{0B051A5F-BD38-47D1-BAFF-D44BA30D3FB7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{0B051A5F-BD38-47D1-BAFF-D44BA30D3FB7}.Release|Any CPU.Build.0 = Release|Any CPU
|
{0B051A5F-BD38-47D1-BAFF-D44BA30D3FB7}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{6A80FFEC-9B83-40A7-8C78-124440B48B33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{6A80FFEC-9B83-40A7-8C78-124440B48B33}.Debug|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{6A80FFEC-9B83-40A7-8C78-124440B48B33}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{6A80FFEC-9B83-40A7-8C78-124440B48B33}.Debug|Any CPU.Build.0 = Release|Any CPU
|
||||||
{6A80FFEC-9B83-40A7-8C78-124440B48B33}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{6A80FFEC-9B83-40A7-8C78-124440B48B33}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{6A80FFEC-9B83-40A7-8C78-124440B48B33}.Release|Any CPU.Build.0 = Release|Any CPU
|
{6A80FFEC-9B83-40A7-8C78-124440B48B33}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{13E40DF1-6123-4838-9BF8-086C94E6ADF6}.Debug|Any CPU.ActiveCfg = Release|Any CPU
|
{13E40DF1-6123-4838-9BF8-086C94E6ADF6}.Debug|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
|||||||
Reference in New Issue
Block a user