Skip to content

Commit

Permalink
Implement QueryParameterHandler and move it to Configurator
Browse files Browse the repository at this point in the history
  • Loading branch information
pmosk committed Nov 27, 2024
1 parent ce0cca6 commit 2c029c9
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 12 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ jobs:
- name: Pack Cache
run: dotnet pack ./src/*/*/Cache.csproj --no-restore -o ~/nuget -c Release

# Header
# Configurator

- name: Restore Header
run: dotnet restore ./src/*/*/Header.csproj
- name: Restore Configurator
run: dotnet restore ./src/*/*/Configurator.csproj

- name: Build Header
run: dotnet build ./src/*/*/Header.csproj --no-restore -c Release
- name: Build Configurator
run: dotnet build ./src/*/*/Configurator.csproj --no-restore -c Release

- name: Pack Header
run: dotnet pack ./src/*/*/Header.csproj --no-restore -o ~/nuget -c Release
- name: Pack Configurator
run: dotnet pack ./src/*/*/Configurator.csproj --no-restore -o ~/nuget -c Release

# Push

Expand Down
2 changes: 1 addition & 1 deletion Infra.Http.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SocketsHandlerProvider.Abst
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cache", "src\handler\Cache\Cache.csproj", "{CDB1ADA8-75C7-4862-9E34-5CEA847EBAE4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Header", "src\handler\Header\Header.csproj", "{FC276BC8-9144-433B-BA43-663F136DE054}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Configurator", "src\handler\Configurator\Configurator.csproj", "{FC276BC8-9144-433B-BA43-663F136DE054}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>$(NoWarn);IDE0130;CA1859</NoWarn>
<RootNamespace>GarageGroup.Infra</RootNamespace>
<AssemblyName>GarageGroup.Infra.Http.Header</AssemblyName>
<Version>0.1.0-build.1</Version>
<AssemblyName>GarageGroup.Infra.Http.Configurator</AssemblyName>
<Version>0.0.1-build.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace GarageGroup.Infra;

public static class HttpHeaderHandlerDependency
public static class HttpConfiguratorHandlerDependency
{
public static Dependency<HttpMessageHandler> ConfigureHttpHeader(
this Dependency<HttpMessageHandler> dependency, [DisallowNull] string headerName, string configurationKey)
Expand All @@ -28,7 +28,27 @@ InternalHttpHeaderHandler CreateHandler(IServiceProvider serviceProvider, HttpMe
}
}

public static Dependency<HttpMessageHandler> ConfigureQueryParameter(
this Dependency<HttpMessageHandler> dependency, [DisallowNull] string parameterName, string configurationKey)
{
ArgumentNullException.ThrowIfNull(dependency);
ArgumentException.ThrowIfNullOrWhiteSpace(parameterName);

return dependency.Map<HttpMessageHandler>(CreateHandler);

InternalQueryParameterHandler CreateHandler(IServiceProvider serviceProvider, HttpMessageHandler innerHandler)
{
ArgumentNullException.ThrowIfNull(serviceProvider);
ArgumentNullException.ThrowIfNull(innerHandler);

return new(
innerHandler: innerHandler,
parameterName: parameterName,
parameterValue: serviceProvider.GetServiceOrThrow<IConfiguration>()[configurationKey.OrEmpty()].OrEmpty());
}
}

private static string OrEmpty(this string? source)
=>
string.IsNullOrEmpty(source) ? string.Empty : source;
source ?? string.Empty;
}
44 changes: 44 additions & 0 deletions src/handler/Configurator/Internal/InternalQueryParameterHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web;

namespace GarageGroup.Infra;

internal sealed class InternalQueryParameterHandler : DelegatingHandler
{
private readonly string parameterName;

private readonly string parameterValue;

internal InternalQueryParameterHandler(HttpMessageHandler innerHandler, string parameterName, string parameterValue) : base(innerHandler)
{
this.parameterName = parameterName;
this.parameterValue = parameterValue;
}

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Debug.Assert(request is not null);

if (request.RequestUri is not null)
{
request.RequestUri = AddOrUpdateQueryParameter(request.RequestUri);
}

return base.SendAsync(request, cancellationToken);
}

private Uri AddOrUpdateQueryParameter(Uri uri)
{
var uriBuilder = new UriBuilder(uri);

var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query[parameterName] = parameterValue;

uriBuilder.Query = query.ToString();
return uriBuilder.Uri;
}
}

0 comments on commit 2c029c9

Please sign in to comment.