Skip to content
Merged
Show file tree
Hide file tree
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
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,24 @@ Add a section to `IConfiguration`
{
"FluentHttp": {
"BaseAddress": "https://jsonplaceholder.typicode.com",
"NtlmEnabled": false
"NtlmEnabled": false,
"Timeout": 100
}
}
```
- `Timeout` in seconds. for `Timeout.Infinite` pass -1.

Register a service
```cs
using MyNihongo.FluentHttp;

services.AddFluentHttp();

// Or optionally configer the HTTP client
services.AddFluentHttp((services, httpClient) =>
{
// configure
});
```

## HTTP methods
Expand Down Expand Up @@ -90,4 +99,13 @@ var result = await fluentHttp
.AppendPathSegment("posts")
.WithHeader("my-header", "value")
.GetJsonAsync<PostRecord>();
```

#### Basic authentication
Append the authentication header for the basic authentication
```cs
var result = await fluentHttp
.AppendPathSegment("posts")
.WithBasicAuth("username", "strong password")
.GetJsonAsync<PostRecord>();
```
4 changes: 4 additions & 0 deletions src/DefaultFluentHttp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,13 @@ private async Task<T> GetResponseAsync<T>(HttpRequestMessage req, JsonTypeInfo<T
var httpClient = _factory.CreateClient(Const.FactoryName);
var url = httpClient.BaseAddress.GetAbsoluteUri(req.RequestUri);

var startTime = DateTime.Now;

using var res = await httpClient.SendAsync(req, ct)
.ConfigureAwait(false);

_logger.LogDebug("{CodeName} ({Code:D}). Request time: {RequestTime}", res.StatusCode, res.StatusCode, DateTime.Now - startTime);

await using var stream = await res.Content
.ReadAsStreamAsync(ct)
.ConfigureAwait(false);
Expand Down
2 changes: 1 addition & 1 deletion src/MyNihongo.FluentHttp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.2</Version>
<Version>1.0.3</Version>
<Authors>MyNihongo</Authors>
<Description>Fluent wrapper around IHttpClientFactory</Description>
<Copyright>Copyright © 2022 MyNihongo</Copyright>
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/ConfigKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ internal static class ConfigKeys
{
public const string Section = "FluentHttp",
BaseAddress = "BaseAddress",
UseNtlmAuthentication = "NtlmEnabled";
UseNtlmAuthentication = "NtlmEnabled",
Timeout = "Timeout";
}
28 changes: 25 additions & 3 deletions src/Utils/ServiceRegistration/ServiceCollectionEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace MyNihongo.FluentHttp;

public static class ServiceCollectionEx
{
public static IServiceCollection AddFluentHttp(this IServiceCollection @this)
public static IServiceCollection AddFluentHttp(this IServiceCollection @this, Action<IServiceProvider, HttpClient>? configure = null)
{
@this
.AddHttpClient(Const.FactoryName)
Expand All @@ -15,9 +15,12 @@ public static IServiceCollection AddFluentHttp(this IServiceCollection @this)
var configuration = services.GetRequiredService<IConfiguration>()
.GetSection(ConfigKeys.Section);

var baseAddress = configuration[ConfigKeys.BaseAddress];
if (!string.IsNullOrEmpty(baseAddress))
if (configuration.TryGetBaseAddress(out var baseAddress))
http.BaseAddress = new Uri(baseAddress, UriKind.Absolute);
if (configuration.TryGetTimeout(out var timeout))
http.Timeout = timeout;

configure?.Invoke(services, http);
})
.ConfigurePrimaryHttpMessageHandler(static services =>
{
Expand All @@ -36,4 +39,23 @@ public static IServiceCollection AddFluentHttp(this IServiceCollection @this)

return @this.AddSingleton<IFluentHttp, DefaultFluentHttp>();
}

private static bool TryGetBaseAddress(this IConfiguration configuration, out string value)
{
value = configuration[ConfigKeys.BaseAddress];
return !string.IsNullOrEmpty(value);
}

private static bool TryGetTimeout(this IConfiguration configuration, out TimeSpan value)
{
value = TimeSpan.Zero;
if (!int.TryParse(configuration[ConfigKeys.Timeout], out var timeoutSeconds))
return false;

value = timeoutSeconds != Timeout.Infinite
? TimeSpan.FromSeconds(timeoutSeconds)
: Timeout.InfiniteTimeSpan;

return true;
}
}