-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement QueryParameterHandler and move it to Configurator
- Loading branch information
pmosk
committed
Nov 27, 2024
1 parent
ce0cca6
commit 2c029c9
Showing
6 changed files
with
76 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
44 changes: 44 additions & 0 deletions
44
src/handler/Configurator/Internal/InternalQueryParameterHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |