Skip to content

Commit

Permalink
feat: implement request creation from response for GetTemplates in Ve…
Browse files Browse the repository at this point in the history
…rifyV2
  • Loading branch information
Tr00d committed Sep 12, 2024
1 parent ccf80dd commit 0eca99a
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 61 deletions.
58 changes: 57 additions & 1 deletion Vonage.Test/VerifyV2/GetTemplates/E2ETest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#region
using System;
using System.Net;
using System.Threading.Tasks;
using Vonage.Test.Common.Extensions;
Expand All @@ -17,7 +18,7 @@ public E2ETest() : base(typeof(E2ETest).Namespace)
}

[Fact]
public async Task GetTemplates()
public async Task GetTemplatesEmpty()
{
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create()
.WithPath("/v2/verify/templates")
Expand All @@ -31,4 +32,59 @@ await this.Helper.VonageClient.VerifyV2Client
.Should()
.BeSuccessAsync(SerializationTest.VerifyExpectedResponse);
}

[Fact]
public async Task GetTemplatesEmptyFromResponse()
{
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create()
.WithPath("/v2/verify/templates")
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue)
.UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(HttpStatusCode.OK)
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserialize200))));
await this.Helper.VonageClient.VerifyV2Client
.GetTemplateAsync(
new GetTemplatesHalLink(new Uri("https://api.nexmo.com/v2/verify/templates")).BuildRequest())
.Should()
.BeSuccessAsync(SerializationTest.VerifyExpectedResponse);
}

[Fact]
public async Task GetTemplatesFromResponse()
{
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create()
.WithPath("/v2/verify/templates")
.WithParam("page_size", "15")
.WithParam("page", "50")
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue)
.UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(HttpStatusCode.OK)
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserialize200))));
await this.Helper.VonageClient.VerifyV2Client
.GetTemplateAsync(
new GetTemplatesHalLink(new Uri("https://api.nexmo.com/v2/verify/templates?page_size=15&page=50"))
.BuildRequest())
.Should()
.BeSuccessAsync(SerializationTest.VerifyExpectedResponse);
}

[Fact]
public async Task GetTemplates()
{
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create()
.WithPath("/v2/verify/templates")
.WithParam("page_size", "15")
.WithParam("page", "50")
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue)
.UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(HttpStatusCode.OK)
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserialize200))));
await this.Helper.VonageClient.VerifyV2Client
.GetTemplateAsync(GetTemplatesRequest.Build().WithPageSize(15).WithPage(50).Create())
.Should()
.BeSuccessAsync(SerializationTest.VerifyExpectedResponse);
}
}
93 changes: 33 additions & 60 deletions Vonage/VerifyV2/GetTemplates/GetTemplatesResponse.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#region
using System;
using System.Text.Json.Serialization;
using System.Web;
using Vonage.Common;
using Vonage.Common.Monads;
#endregion

namespace Vonage.VerifyV2.GetTemplates;
Expand Down Expand Up @@ -37,64 +39,35 @@ public record GetTemplatesEmbedded(
/// <param name="Href">Hyperlink reference.</param>
public record GetTemplatesHalLink(Uri Href)
{
// /// <summary>
// /// Transforms the link into a GetEventsRequest using the cursor pagination.
// /// </summary>
// /// <returns></returns>
// public Result<GetTemplatesRequest> BuildRequest()
// {
// var parameters = ExtractQueryParameters(this.Href);
// var builder = new GetEventsRequestBuilder(parameters.Cursor)
// .WithConversationId(parameters.ConversationId)
// .WithPageSize(parameters.PageSize)
// .WithOrder(parameters.Order);
// builder = parameters.ApplyOptionalStartId(builder);
// builder = parameters.ApplyOptionalEndDate(builder);
// builder = parameters.ApplyOptionalEventType(builder);
// builder = parameters.ApplyExcludeDeletedEvents(builder);
// return builder.Create();
// }
//
// private static QueryParameters ExtractQueryParameters(Uri uri)
// {
// var queryParameters = HttpUtility.ParseQueryString(uri.Query);
// var startDate = queryParameters["start_id"] ?? Maybe<string>.None;
// var endDate = queryParameters["end_id"] ?? Maybe<string>.None;
// var eventType = queryParameters["event_type"] ?? Maybe<string>.None;
// return new QueryParameters(
// queryParameters["cursor"],
// ExtractConversationId(uri),
// int.Parse(queryParameters["page_size"]),
// Enums.Parse<FetchOrder>(queryParameters["order"], false, EnumFormat.Description),
// startDate,
// endDate,
// eventType,
// bool.Parse(queryParameters["exclude_deleted_events"]));
// }
//
// private static string ExtractConversationId(Uri uri) => uri.AbsolutePath.Replace("/v1/conversations/", string.Empty)
// .Replace("/events", string.Empty);
//
// private record QueryParameters(
// Maybe<string> Cursor,
// string ConversationId,
// int PageSize,
// FetchOrder Order,
// Maybe<string> StartId,
// Maybe<string> EndId,
// Maybe<string> EventType,
// bool ExcludeDeletedEvents)
// {
// public IBuilderForOptional ApplyOptionalStartId(IBuilderForOptional builder) =>
// this.StartId.Match(builder.WithStartId, () => builder);
//
// public IBuilderForOptional ApplyOptionalEndDate(IBuilderForOptional builder) =>
// this.EndId.Match(builder.WithEndId, () => builder);
//
// public IBuilderForOptional ApplyOptionalEventType(IBuilderForOptional builder) =>
// this.EventType.Match(builder.WithEventType, () => builder);
//
// public IBuilderForOptional ApplyExcludeDeletedEvents(IBuilderForOptional builder) =>
// this.ExcludeDeletedEvents ? builder.ExcludeDeletedEvents() : builder;
// }
/// <summary>
/// Transforms the link into a GetEventsRequest using the cursor pagination.
/// </summary>
/// <returns></returns>
public Result<GetTemplatesRequest> BuildRequest()
{
var parameters = ExtractQueryParameters(this.Href);
IBuilderForOptional builder = new GetTemplatesRequestBuilder();
builder = parameters.ApplyPageSize(builder);
builder = parameters.ApplyPage(builder);
return builder.Create();
}

private static QueryParameters ExtractQueryParameters(Uri uri)
{
var queryParameters = HttpUtility.ParseQueryString(uri.Query);
var pageSize = queryParameters["page_size"] ?? Maybe<string>.None;
var page = queryParameters["page"] ?? Maybe<string>.None;
return new QueryParameters(pageSize.Map(int.Parse), page.Map(int.Parse));
}

private record QueryParameters(
Maybe<int> PageSize,
Maybe<int> Page)
{
public IBuilderForOptional ApplyPageSize(IBuilderForOptional builder) =>
this.PageSize.Match(builder.WithPageSize, () => builder);

public IBuilderForOptional ApplyPage(IBuilderForOptional builder) =>
this.Page.Match(builder.WithPage, () => builder);
}
}

0 comments on commit 0eca99a

Please sign in to comment.