Skip to content

Commit

Permalink
refactor: make SendRequest pure
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Dec 6, 2024
1 parent 276eb21 commit dbab981
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions Vonage/Common/Client/VonageHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ internal VonageHttpClient<T> WithDifferentHeader<T>(Result<AuthenticationHeaderV
/// <param name="request">The request to send.</param>
/// <returns>Success if the operation succeeds, Failure it if fails.</returns>
public async Task<Result<Unit>> SendAsync<T>(Result<T> request) where T : IVonageRequest =>
await this.SendRequest(request, this.BuildHttpRequestMessage, this.ParseFailure<Unit>, CreateSuccessResult)
await SendRequest(request, this.BuildHttpRequestMessage, this.SendRequest, this.ParseFailure<Unit>,
CreateSuccessResult)
.ConfigureAwait(false);

/// <summary>
Expand All @@ -51,7 +52,7 @@ await this.SendRequest(request, this.BuildHttpRequestMessage, this.ParseFailure<
/// <param name="request">The request to send.</param>
/// <returns>Success if the operation succeeds, Failure it if fails.</returns>
public async Task<Result<Unit>> SendWithoutHeadersAsync<T>(Result<T> request) where T : IVonageRequest =>
await this.SendRequest(request, value => value.BuildRequestMessage(), this.ParseFailure<Unit>,
await SendRequest(request, value => value.BuildRequestMessage(), this.SendRequest, this.ParseFailure<Unit>,
CreateSuccessResult).ConfigureAwait(false);

/// <summary>
Expand All @@ -62,7 +63,9 @@ await this.SendRequest(request, value => value.BuildRequestMessage(), this.Parse
/// <returns>Success if the operation succeeds, Failure it if fails.</returns>
public async Task<Result<string>> SendWithRawResponseAsync<TRequest>(Result<TRequest> request)
where TRequest : IVonageRequest =>
await this.SendRequest(request, this.BuildHttpRequestMessage, this.ParseFailure<string>,
await SendRequest(request, this.BuildHttpRequestMessage,
this.SendRequest,
this.ParseFailure<string>,
responseData => responseData.Content).ConfigureAwait(false);

/// <summary>
Expand All @@ -72,7 +75,9 @@ await this.SendRequest(request, this.BuildHttpRequestMessage, this.ParseFailure<
/// <returns>Success if the operation succeeds, Failure it if fails.</returns>
public async Task<Result<TResponse>> SendWithResponseAsync<TRequest, TResponse>(Result<TRequest> request)
where TRequest : IVonageRequest =>
await this.SendRequest(request, this.BuildHttpRequestMessage, this.ParseFailure<TResponse>,
await SendRequest(request, this.BuildHttpRequestMessage,
this.SendRequest,
this.ParseFailure<TResponse>,
this.ParseSuccess<TResponse>).ConfigureAwait(false);

private Result<HttpRequestMessage> BuildHttpRequestMessage<T>(T value) where T : IVonageRequest =>
Expand Down Expand Up @@ -107,18 +112,21 @@ private Result<T> ParseSuccess<T>(ResponseData response) =>
.DeserializeObject<T>(response.Content)
.Match(Result<T>.FromSuccess, Result<T>.FromFailure);

private async Task<Result<TResponse>> SendRequest<TRequest, TResponse>(
private static async Task<Result<TResponse>> SendRequest<TRequest, TResponse>(
Result<TRequest> request,
Func<TRequest, Result<HttpRequestMessage>> httpRequestConversion,
Func<HttpRequestMessage, Task<HttpResponseMessage>> sendRequest,
Func<ResponseData, Result<TResponse>> failure,
Func<ResponseData, Result<TResponse>> success) =>
await request
.Bind(httpRequestConversion)
.MapAsync(value => this.client.SendAsync(value))
.MapAsync(sendRequest)
.MapAsync(ExtractResponseData)
.Bind(response => !response.IsSuccessStatusCode ? failure(response) : success(response))
.ConfigureAwait(false);

private Task<HttpResponseMessage> SendRequest(HttpRequestMessage request) => this.client.SendAsync(request);

private sealed record ResponseData(HttpStatusCode Code, bool IsSuccessStatusCode, string Content);

private sealed record HttpClientOptions(AuthenticationHeaderValue AuthenticationHeader, string UserAgent);
Expand Down

0 comments on commit dbab981

Please sign in to comment.