Skip to content

Commit

Permalink
Add RequestReadyAsync override to HttpClientService.
Browse files Browse the repository at this point in the history
  • Loading branch information
ejball committed Feb 2, 2024
1 parent 503df20 commit cfe0384
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<PackageVersion Include="ArgsReading" Version="2.3.2" />
<PackageVersion Include="BenchmarkDotNet" Version="0.13.3" />
<PackageVersion Include="Facility.AspNetCore" Version="3.4.0" />
<PackageVersion Include="Facility.Definition" Version="2.11.0" />
<PackageVersion Include="Facility.CodeGen.Console" Version="2.11.0" />
<PackageVersion Include="Facility.Definition" Version="2.12.0-alpha.1" />
<PackageVersion Include="Facility.CodeGen.Console" Version="2.12.0-alpha.1" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="MessagePack" Version="2.5.140" />
<PackageVersion Include="Microsoft.CSharp" Version="4.7.0" />
Expand Down
7 changes: 7 additions & 0 deletions src/Facility.Core/Http/HttpClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,15 @@ protected virtual bool ShouldCreateErrorFromException(Exception exception)
/// </summary>
protected virtual ServiceErrorDto CreateErrorFromException(Exception exception) => ServiceErrorUtility.CreateInternalErrorForException(exception);

/// <summary>
/// Called right before the request is sent, before aspects are applied.
/// </summary>
protected virtual Task RequestReadyAsync(HttpRequestMessage httpRequest, ServiceDto requestDto, CancellationToken cancellationToken) => Task.CompletedTask;

private async Task<HttpResponseMessage> SendRequestAsync(HttpRequestMessage httpRequest, ServiceDto requestDto, CancellationToken cancellationToken)
{
await AdaptTask(RequestReadyAsync(httpRequest, requestDto, cancellationToken)).ConfigureAwait(true);

if (m_aspects != null)
{
foreach (var aspect in m_aspects)
Expand Down
15 changes: 15 additions & 0 deletions tools/EdgeCases.fsd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ service EdgeCases
{
}

/// Custom HTTP method.
method customHttp
{
value: string;

[http(from: "custom")]
extras: map<string>;
}:
{
value: string;

[http(from: "custom")]
extras: map<string>;
}

/// An old DTO.
[obsolete]
data OldEmptyData
Expand Down
47 changes: 47 additions & 0 deletions tools/EdgeCases/CustomHttpRequestDto.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions tools/EdgeCases/CustomHttpResponseDto.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tools/EdgeCases/DelegatingEdgeCases.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tools/EdgeCases/EdgeCasesMethods.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion tools/EdgeCases/Http/EdgeCasesHttpHandler.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions tools/EdgeCases/Http/EdgeCasesHttpMapping.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions tools/EdgeCases/Http/HttpClientEdgeCases.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Web;
using Facility.Core;

namespace EdgeCases.Http;

public sealed partial class HttpClientEdgeCases
{
protected override Task RequestReadyAsync(HttpRequestMessage httpRequest, ServiceDto requestDto, CancellationToken cancellationToken)
{
if (requestDto is CustomHttpRequestDto customDto)
{
const string prefix = "extra-";
var query = HttpUtility.ParseQueryString(httpRequest.RequestUri.Query);
customDto.Extras = query.AllKeys.Where(x => x?.StartsWith(prefix, StringComparison.Ordinal) is true).ToDictionary(x => x.Substring(prefix.Length), x => query[x]);
}

return Task.CompletedTask;
}
}
6 changes: 6 additions & 0 deletions tools/EdgeCases/Http/HttpClientEdgeCases.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tools/EdgeCases/IEdgeCases.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cfe0384

Please sign in to comment.