Skip to content

Commit

Permalink
refactor: clean SimSwap
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed May 23, 2024
1 parent a0d8d27 commit e92db28
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
6 changes: 3 additions & 3 deletions Vonage.Test/SimSwap/Check/E2ETest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task CheckAsync()
{
this.SetupAuthorization();
this.SetupToken();
this.SetupSimSwap(nameof(SerializationTest.ShouldSerialize));
this.SetupCheck(nameof(SerializationTest.ShouldSerialize));
await this.Helper.VonageClient.SimSwapClient
.CheckAsync(CheckRequest.Build().WithPhoneNumber("346661113334").Create())
.Should()
Expand All @@ -31,14 +31,14 @@ public async Task CheckAsyncWithPeriod()
{
this.SetupAuthorization();
this.SetupToken();
this.SetupSimSwap(nameof(SerializationTest.ShouldSerializeWithPeriod));
this.SetupCheck(nameof(SerializationTest.ShouldSerializeWithPeriod));
await this.Helper.VonageClient.SimSwapClient
.CheckAsync(CheckRequest.Build().WithPhoneNumber("346661113334").WithPeriod(15).Create())
.Should()
.BeSuccessAsync(true);
}

private void SetupSimSwap(string expectedOutput) =>
private void SetupCheck(string expectedOutput) =>
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create()
.WithPath("/camara/sim-swap/v040/check")
.WithHeader("Authorization", "Bearer ABCDEFG")
Expand Down
4 changes: 2 additions & 2 deletions Vonage.Test/SimSwap/GetSwapDate/E2ETest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public async Task GetSwapDateAsync()
{
this.SetupAuthorization();
this.SetupToken();
this.SetupSimSwap(nameof(SerializationTest.ShouldSerialize));
this.SetupGetSwapDate(nameof(SerializationTest.ShouldSerialize));
await this.Helper.VonageClient.SimSwapClient
.GetSwapDateAsync(GetSwapDateRequest.Parse("346661113334"))
.Should()
.BeSuccessAsync(DateTimeOffset.Parse("2019-08-24T14:15:22Z"));
}

private void SetupSimSwap(string expectedOutput) =>
private void SetupGetSwapDate(string expectedOutput) =>
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create()
.WithPath("/camara/sim-swap/v040/retrieve-date")
.WithHeader("Authorization", "Bearer ABCDEFG")
Expand Down
24 changes: 13 additions & 11 deletions Vonage/SimSwap/SimSwapClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,36 @@ internal class SimSwapClient : ISimSwapClient

/// <inheritdoc />
public async Task<Result<bool>> CheckAsync(Result<CheckRequest> request) =>
await request.BindAsync(this.AuthenticateCheckRequest)
await request
.Map(BuildAuthenticationRequest)
.BindAsync(this.AuthenticateAsync)
.Map(BuildAuthenticationHeader)
.Map(this.BuildClientWithAuthenticationHeader)
.BindAsync(client => client.SendWithResponseAsync<CheckRequest, CheckResponse>(request))
.Map(response => response.Swapped);

/// <inheritdoc />
public async Task<Result<DateTimeOffset>> GetSwapDateAsync(Result<GetSwapDateRequest> request)
{
return await request.BindAsync(this.AuthenticateGetSwapDateRequest)
public async Task<Result<DateTimeOffset>> GetSwapDateAsync(Result<GetSwapDateRequest> request) =>
await request
.Map(BuildAuthenticationRequest)
.BindAsync(this.AuthenticateAsync)
.Map(BuildAuthenticationHeader)
.Map(this.BuildClientWithAuthenticationHeader)
.BindAsync(client => client.SendWithResponseAsync<GetSwapDateRequest, GetSwapDateResponse>(request))
.Map(response => response.LatestSimChange);
}

private static Result<AuthenticateRequest> BuildAuthenticationRequest(CheckRequest request) =>
request.BuildAuthenticationRequest();

private static Result<AuthenticateRequest> BuildAuthenticationRequest(GetSwapDateRequest request) =>
request.BuildAuthenticationRequest();

private VonageHttpClient BuildClientWithAuthenticationHeader(AuthenticationHeaderValue header) =>
this.vonageClient.WithDifferentHeader(header);

private static AuthenticationHeaderValue BuildAuthenticationHeader(AuthenticateResponse authentication) =>
authentication.BuildAuthenticationHeader();

private Task<Result<AuthenticateResponse>> AuthenticateCheckRequest(CheckRequest request) =>
this.AuthenticateAsync(request.BuildAuthenticationRequest());

private Task<Result<AuthenticateResponse>> AuthenticateGetSwapDateRequest(GetSwapDateRequest request) =>
this.AuthenticateAsync(request.BuildAuthenticationRequest());

private static AuthenticateResponse BuildAuthenticateResponse(GetTokenResponse response) =>
new AuthenticateResponse(response.AccessToken);

Expand Down

0 comments on commit e92db28

Please sign in to comment.