Skip to content

Commit 992c33d

Browse files
committed
Refactor and update nullability and configurations
Refactored multiple methods for better readability and updated several methods to return nullable types. Added XML documentation to `IFabioHttpClient` and updated `README_NUGET.md` to reflect new load balancing focus. Introduced new `BindRequestFromRoute` property in `WebApiOptions`. Removed outdated configurations from `appsettings.development.json` and added new `webApi` section to `appsettings.json`. Refactor methods and update configurations Refactored multiple methods for better readability and null safety. Updated `CreateConsulAgentRegistration`, `GetServiceAgentsAsync`, and `DeserializeJsonFromStream` to return nullable types. Refactored `HttpResult` class to use a primary constructor. Added XML documentation to `IFabioHttpClient`. Updated `README_NUGET.md` to reflect new package focus. Added `BindRequestFromRoute` property to `WebApiOptions`. Improved null handling in `ReadJsonAsync`. Removed `prometheus` and `vault` configurations and the empty `appsettings.Development.json`. Added `webApi` section to `appsettings.json`.
1 parent 77bbd75 commit 992c33d

File tree

13 files changed

+85
-113
lines changed

13 files changed

+85
-113
lines changed

src/Genocs.Discovery.Consul/Extensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static void AddConsulHttpClient(this IGenocsBuilder builder, string clien
8080
c.GetRequiredService<IConsulServicesRegistry>(),
8181
c.GetRequiredService<ConsulOptions>(), serviceName, true));
8282

83-
private static ServiceRegistration CreateConsulAgentRegistration(this IGenocsBuilder builder, ConsulOptions options)
83+
private static ServiceRegistration? CreateConsulAgentRegistration(this IGenocsBuilder builder, ConsulOptions options)
8484
{
8585
bool enabled = options.Enabled;
8686
string? consulEnabled = Environment.GetEnvironmentVariable("CONSUL_ENABLED")?.ToLowerInvariant();

src/Genocs.Discovery.Consul/IConsulService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ public interface IConsulService
66
{
77
Task<HttpResponseMessage> RegisterServiceAsync(ServiceRegistration registration);
88
Task<HttpResponseMessage> DeregisterServiceAsync(string id);
9-
Task<IDictionary<string, ServiceAgent>> GetServiceAgentsAsync(string? service = null);
9+
Task<IDictionary<string, ServiceAgent>?> GetServiceAgentsAsync(string? service = null);
1010
}

src/Genocs.Discovery.Consul/Services/ConsulService.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public Task<HttpResponseMessage> RegisterServiceAsync(ServiceRegistration regist
2121
public Task<HttpResponseMessage> DeregisterServiceAsync(string id)
2222
=> _client.PutAsync(GetEndpoint($"agent/service/deregister/{id}"), EmptyRequest);
2323

24-
public async Task<IDictionary<string, ServiceAgent>> GetServiceAgentsAsync(string? service = null)
24+
public async Task<IDictionary<string, ServiceAgent>?> GetServiceAgentsAsync(string? service = null)
2525
{
2626
string filter = string.IsNullOrWhiteSpace(service) ? string.Empty : $"?filter=Service==\"{service}\"";
2727
var response = await _client.GetAsync(GetEndpoint($"agent/services{filter}"));
@@ -38,5 +38,6 @@ public async Task<IDictionary<string, ServiceAgent>> GetServiceAgentsAsync(strin
3838
private static StringContent GetPayload(object request)
3939
=> new(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");
4040

41-
private static string GetEndpoint(string endpoint) => $"{Version}/{endpoint}";
41+
private static string GetEndpoint(string endpoint)
42+
=> $"{Version}/{endpoint}";
4243
}

src/Genocs.HTTP/GenocsHttpClient.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ public void SetHeaders(IDictionary<string, string> headers)
170170
}
171171
}
172172

173-
public void SetHeaders(Action<HttpRequestHeaders> headers) => headers?.Invoke(_client.DefaultRequestHeaders);
173+
public void SetHeaders(Action<HttpRequestHeaders> headers)
174+
=> headers?.Invoke(_client.DefaultRequestHeaders);
174175

175176
protected virtual async Task<T> SendAsync<T>(string uri, Method method, HttpContent? content = null, IHttpClientSerializer? serializer = null)
176177
{
@@ -241,7 +242,7 @@ protected virtual Task<HttpResponseMessage> GetResponseAsync(string uri, Method
241242
return content;
242243
}
243244

244-
protected async Task<T> DeserializeJsonFromStream<T>(Stream stream, IHttpClientSerializer? serializer = null)
245+
protected async Task<T?> DeserializeJsonFromStream<T>(Stream stream, IHttpClientSerializer? serializer = null)
245246
{
246247
if (stream is null || stream.CanRead is false)
247248
{

src/Genocs.HTTP/HttpResult.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
namespace Genocs.HTTP;
22

3-
public class HttpResult<T>
3+
public class HttpResult<T>(T result, HttpResponseMessage response)
44
{
5-
public T Result { get; }
6-
public HttpResponseMessage Response { get; }
5+
public T Result { get; } = result;
6+
public HttpResponseMessage Response { get; } = response;
77
public bool HasResult => Result is not null;
8-
9-
public HttpResult(T result, HttpResponseMessage response)
10-
{
11-
Result = result;
12-
Response = response;
13-
}
148
}

src/Genocs.LoadBalancing.Fabio/Extensions.cs

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,32 @@ public static IGenocsBuilder AddFabio(
3737
b => b.AddConsul(consulOptions, httpClientOptions));
3838
}
3939

40-
public static IGenocsBuilder AddFabio(this IGenocsBuilder builder,
41-
Func<IFabioOptionsBuilder, IFabioOptionsBuilder> buildOptions,
42-
Func<IConsulOptionsBuilder, IConsulOptionsBuilder> buildConsulOptions,
43-
HttpClientOptions httpClientOptions)
40+
public static IGenocsBuilder AddFabio(
41+
this IGenocsBuilder builder,
42+
Func<IFabioOptionsBuilder, IFabioOptionsBuilder> buildOptions,
43+
Func<IConsulOptionsBuilder, IConsulOptionsBuilder> buildConsulOptions,
44+
HttpClientOptions httpClientOptions)
4445
{
4546
var fabioOptions = buildOptions(new FabioOptionsBuilder()).Build();
46-
return builder.AddFabio(fabioOptions, httpClientOptions,
47-
b => b.AddConsul(buildConsulOptions, httpClientOptions));
47+
48+
return builder.AddFabio(
49+
fabioOptions,
50+
httpClientOptions,
51+
b => b.AddConsul(buildConsulOptions, httpClientOptions));
4852
}
4953

50-
public static IGenocsBuilder AddFabio(this IGenocsBuilder builder, FabioOptions fabioOptions,
51-
ConsulOptions consulOptions, HttpClientOptions httpClientOptions)
54+
public static IGenocsBuilder AddFabio(
55+
this IGenocsBuilder builder,
56+
FabioOptions fabioOptions,
57+
ConsulOptions consulOptions,
58+
HttpClientOptions httpClientOptions)
5259
=> builder.AddFabio(fabioOptions, httpClientOptions, b => b.AddConsul(consulOptions, httpClientOptions));
5360

54-
private static IGenocsBuilder AddFabio(this IGenocsBuilder builder, FabioOptions fabioOptions,
55-
HttpClientOptions httpClientOptions, Action<IGenocsBuilder> registerConsul)
61+
private static IGenocsBuilder AddFabio(
62+
this IGenocsBuilder builder,
63+
FabioOptions fabioOptions,
64+
HttpClientOptions httpClientOptions,
65+
Action<IGenocsBuilder> registerConsul)
5666
{
5767
registerConsul(builder);
5868
builder.Services.AddSingleton(fabioOptions);
@@ -64,19 +74,24 @@ private static IGenocsBuilder AddFabio(this IGenocsBuilder builder, FabioOptions
6474

6575
if (httpClientOptions.Type?.ToLowerInvariant() == "fabio")
6676
{
67-
builder.Services.AddTransient<FabioMessageHandler>();
68-
builder.Services.AddHttpClient<IFabioHttpClient, FabioHttpClient>("fabio-http")
69-
.AddHttpMessageHandler<FabioMessageHandler>();
77+
builder.Services
78+
.AddTransient<FabioMessageHandler>();
7079

80+
builder.Services
81+
.AddHttpClient<IFabioHttpClient, FabioHttpClient>("fabio-http")
82+
.AddHttpMessageHandler<FabioMessageHandler>();
7183

7284
builder.RemoveHttpClient();
73-
builder.Services.AddHttpClient<IHttpClient, FabioHttpClient>("fabio")
74-
.AddHttpMessageHandler<FabioMessageHandler>();
85+
86+
builder.Services
87+
.AddHttpClient<IHttpClient, FabioHttpClient>("fabio")
88+
.AddHttpMessageHandler<FabioMessageHandler>();
7589
}
7690

7791
using var serviceProvider = builder.Services.BuildServiceProvider();
7892
var registration = serviceProvider.GetRequiredService<ServiceRegistration>();
7993
var tags = GetFabioTags(registration.Name, fabioOptions.Service);
94+
8095
if (registration.Tags is null)
8196
{
8297
registration.Tags = tags;
@@ -91,12 +106,17 @@ private static IGenocsBuilder AddFabio(this IGenocsBuilder builder, FabioOptions
91106
return builder;
92107
}
93108

94-
public static void AddFabioHttpClient(this IGenocsBuilder builder, string clientName, string serviceName)
95-
=> builder.Services.AddHttpClient<IHttpClient, FabioHttpClient>(clientName)
96-
.AddHttpMessageHandler(c => new FabioMessageHandler(c.GetRequiredService<FabioOptions>(), serviceName));
97-
98-
private static void UpdateConsulRegistration(this IServiceCollection services,
99-
ServiceRegistration registration)
109+
public static void AddFabioHttpClient(
110+
this IGenocsBuilder builder,
111+
string clientName,
112+
string serviceName)
113+
=> builder.Services
114+
.AddHttpClient<IHttpClient, FabioHttpClient>(clientName)
115+
.AddHttpMessageHandler(c => new FabioMessageHandler(c.GetRequiredService<FabioOptions>(), serviceName));
116+
117+
private static void UpdateConsulRegistration(
118+
this IServiceCollection services,
119+
ServiceRegistration registration)
100120
{
101121
var serviceDescriptor = services.FirstOrDefault(sd => sd.ServiceType == typeof(ServiceRegistration));
102122
services.Remove(serviceDescriptor);

src/Genocs.LoadBalancing.Fabio/IFabioHttpClient.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Genocs.LoadBalancing.Fabio;
44

5+
/// <summary>
6+
/// The Fabio HTTP client interface definition.
7+
/// </summary>
58
public interface IFabioHttpClient : IHttpClient
69
{
710
}
Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
# .NET query builder library
1+
# Genocs Load Balancing library
2+
3+
This package contains support for load balancing functionality by means of Fabio.
24

3-
This package contains a query builder that is agnostic about the persistence layer. The library is designed by Genocs.
4-
The libraries are built using .NET standard 2.1.
55

66
## Description
77

8-
Persistence agnostic query builder service.
8+
Fabio is a fast, modern, zero-conf load balancing HTTP(S) router for deploying microservices managed by consul.
9+
10+
The libraries are built using .NET Core.
911

1012

1113
## Support
@@ -14,52 +16,3 @@ Please check the GitHub repository getting more info.
1416

1517

1618
## Release notes
17-
18-
### [2024-01-23] 5.0.6
19-
- Refactory Settings
20-
- Updated nuget packages
21-
22-
### [2023-11-25] 5.0.5
23-
- Moved to NET8
24-
25-
### [yyyy-mm-dd] 5.0.4
26-
-
27-
28-
### [yyyy-mm-dd] 5.0.3
29-
-
30-
31-
### [yyyy-mm-dd] 5.0.2
32-
-
33-
34-
### [yyyy-mm-dd] 5.0.1
35-
-
36-
37-
### [2023-11-25] 5.0.0
38-
- Moved to NET8
39-
40-
### [2023-10-13] 5.0.0-preview.5.0
41-
- Added [editorconfig](https://editorconfig.org/)
42-
- Added StyleCop
43-
- Updated logo
44-
- Updated readme
45-
46-
### [2023-03-12] 5.0.0-preview.4.0
47-
- Implemented MongoDB repository interfaces
48-
49-
### [2023-03-12] 5.0.0
50-
- New Architecture
51-
52-
### [2023-03-12] 3.1.0
53-
- Added Builders
54-
55-
### [2023-03-12] 3.0.0
56-
- Refactory to implement CQRS pattern
57-
58-
### [2023-03-04] 2.4.1
59-
- Updated System.Text.Json
60-
61-
### [2023-01-23] 1.1.0
62-
- Refactory enum
63-
64-
### [2023-01-13] 1.0.0
65-
- First Release

src/Genocs.WebApi/Configurations/WebApiOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ namespace Genocs.WebApi.Configurations;
55
/// </summary>
66
public class WebApiOptions
77
{
8+
/// <summary>
9+
/// It defines whether the request body should be bound from the route or from body.
10+
/// </summary>
811
public bool BindRequestFromRoute { get; set; }
912
}

src/Genocs.WebApi/Extensions.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public static async Task WriteJsonAsync<T>(this HttpResponse response, T value)
303303
await serializer.SerializeAsync(response.Body, value);
304304
}
305305

306-
public static async Task<T> ReadJsonAsync<T>(this HttpContext httpContext)
306+
public static async Task<T?> ReadJsonAsync<T>(this HttpContext httpContext)
307307
{
308308
var logger = httpContext.RequestServices.GetService<ILogger>();
309309

@@ -323,18 +323,31 @@ public static async Task<T> ReadJsonAsync<T>(this HttpContext httpContext)
323323
if (_bindRequestFromRoute && request.HasRouteData())
324324
{
325325
var values = request.HttpContext.GetRouteData().Values;
326+
327+
if (values is null)
328+
{
329+
return payload;
330+
}
331+
326332
foreach (var (key, value) in values)
327333
{
328-
var field = payload.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
329-
.SingleOrDefault(f => f.Name.ToLowerInvariant().StartsWith($"<{key}>",
330-
StringComparison.InvariantCultureIgnoreCase));
334+
var field = payload?.GetType()
335+
.GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
336+
.SingleOrDefault(f => f.Name.ToLowerInvariant().StartsWith(
337+
$"<{key}>",
338+
StringComparison.InvariantCultureIgnoreCase));
331339

332340
if (field is null)
333341
{
334342
continue;
335343
}
336344

337-
var fieldValue = TypeDescriptor.GetConverter(field.FieldType)
345+
if (value is null)
346+
{
347+
continue;
348+
}
349+
350+
object? fieldValue = TypeDescriptor.GetConverter(field.FieldType)
338351
.ConvertFromInvariantString(value.ToString());
339352
field.SetValue(payload, fieldValue);
340353
}

0 commit comments

Comments
 (0)