Skip to content

Commit

Permalink
feat: implement GetConversations
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Dec 19, 2023
1 parent e519ca9 commit d53529f
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 32 deletions.
60 changes: 60 additions & 0 deletions Vonage.Test.Unit/Conversations/GetConversations/E2ETest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Globalization;
using System.Net;
using System.Threading.Tasks;
using Vonage.Common.Test.Extensions;
using Vonage.Conversations.GetConversations;
using WireMock.ResponseBuilders;
using Xunit;

namespace Vonage.Test.Unit.Conversations.GetConversations
{
[Trait("Category", "E2E")]
public class E2ETest : E2EBase
{
public E2ETest() : base(typeof(E2ETest).Namespace)
{
}

[Fact]
public async Task GetConversations()
{
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create()
.WithPath("/v1/conversations")
.WithParam("page_size", "50")
.WithParam("order", "desc")
.WithParam("date_start", "2023-12-18T09:56:08Z")
.WithParam("date_end", "2023-12-18T10:56:08Z")
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue)
.UsingGet())
.RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK)
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserialize200))));
await this.Helper.VonageClient.ConversationsClient
.GetConversationsAsync(GetConversationsRequest.Build()
.WithPageSize(50)
.WithOrder(FetchOrder.Descending)
.WithStartDate(DateTimeOffset.Parse("2023-12-18T09:56:08.152Z", CultureInfo.InvariantCulture))
.WithEndDate(DateTimeOffset.Parse("2023-12-18T10:56:08.152Z", CultureInfo.InvariantCulture))
.Create())
.Should()
.BeSuccessAsync(SerializationTest.VerifyExpectedResponse);
}

[Fact]
public async Task GetConversationsWithDefaultRequest()
{
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create()
.WithPath("/v1/conversations")
.WithParam("page_size", "10")
.WithParam("order", "asc")
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue)
.UsingGet())
.RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK)
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserialize200))));
await this.Helper.VonageClient.ConversationsClient
.GetConversationsAsync(GetConversationsRequest.Build().Create())
.Should()
.BeSuccessAsync(SerializationTest.VerifyExpectedResponse);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,38 @@ public class SerializationTest
public void ShouldDeserialize200() => this.helper.Serializer
.DeserializeObject<GetConversationsResponse>(this.helper.GetResponseJson())
.Should()
.BeSuccess(response =>
.BeSuccess(VerifyExpectedResponse);

internal static void VerifyExpectedResponse(GetConversationsResponse response)
{
response.PageSize.Should().Be(10);
response.Embedded.Conversations.Should().BeEquivalentTo(new[]
{
response.PageSize.Should().Be(10);
response.Embedded.Conversations.Should().BeEquivalentTo(new[]
{
new Conversation(
"CON-d66d47de-5bcb-4300-94f0-0c9d4b948e9a",
"customer_chat",
"Customer Chat",
new Uri("https://example.com/image.png"),
null,
0,
new Timestamp(DateTimeOffset.Parse("2019-09-03T18:40:24.324Z", CultureInfo.InvariantCulture),
DateTimeOffset.Parse("2019-09-03T18:40:24.324Z", CultureInfo.InvariantCulture),
DateTimeOffset.Parse("2019-09-03T18:40:24.324Z", CultureInfo.InvariantCulture)),
Maybe<Properties>.None,
new Links(new HalLink(new Uri(
"https://api.nexmo.com/v1/conversations/CON-d66d47de-5bcb-4300-94f0-0c9d4b948e9a")))),
});
response.Links.First.Href.Should()
.Be(new Uri("https://api.nexmo.com/v1/conversations?order=desc&page_size=10"));
response.Links.Self.Href.Should()
.Be(new Uri(
"https://api.nexmo.com/v1/conversations?order=desc&page_size=10&cursor=7EjDNQrAcipmOnc0HCzpQRkhBULzY44ljGUX4lXKyUIVfiZay5pv9wg%3D"));
response.Links.Next.Href.Should()
.Be(new Uri(
"https://api.nexmo.com/v1/conversations?order=desc&page_size=10&cursor=7EjDNQrAcipmOnc0HCzpQRkhBULzY44ljGUX4lXKyUIVfiZay5pv9wg%3D"));
response.Links.Previous.Href.Should()
.Be(new Uri(
"https://api.nexmo.com/v1/conversations?order=desc&page_size=10&cursor=7EjDNQrAcipmOnc0HCzpQRkhBULzY44ljGUX4lXKyUIVfiZay5pv9wg%3D"));
new Conversation(
"CON-d66d47de-5bcb-4300-94f0-0c9d4b948e9a",
"customer_chat",
"Customer Chat",
new Uri("https://example.com/image.png"),
null,
0,
new Timestamp(DateTimeOffset.Parse("2019-09-03T18:40:24.324Z", CultureInfo.InvariantCulture),
DateTimeOffset.Parse("2019-09-03T18:40:24.324Z", CultureInfo.InvariantCulture),
DateTimeOffset.Parse("2019-09-03T18:40:24.324Z", CultureInfo.InvariantCulture)),
Maybe<Properties>.None,
new Links(new HalLink(new Uri(
"https://api.nexmo.com/v1/conversations/CON-d66d47de-5bcb-4300-94f0-0c9d4b948e9a")))),
});
response.Links.First.Href.Should()
.Be(new Uri("https://api.nexmo.com/v1/conversations?order=desc&page_size=10"));
response.Links.Self.Href.Should()
.Be(new Uri(
"https://api.nexmo.com/v1/conversations?order=desc&page_size=10&cursor=7EjDNQrAcipmOnc0HCzpQRkhBULzY44ljGUX4lXKyUIVfiZay5pv9wg%3D"));
response.Links.Next.Href.Should()
.Be(new Uri(
"https://api.nexmo.com/v1/conversations?order=desc&page_size=10&cursor=7EjDNQrAcipmOnc0HCzpQRkhBULzY44ljGUX4lXKyUIVfiZay5pv9wg%3D"));
response.Links.Previous.Href.Should()
.Be(new Uri(
"https://api.nexmo.com/v1/conversations?order=desc&page_size=10&cursor=7EjDNQrAcipmOnc0HCzpQRkhBULzY44ljGUX4lXKyUIVfiZay5pv9wg%3D"));
}
}
}
12 changes: 12 additions & 0 deletions Vonage/Conversations/ConversationsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Vonage.Conversations.CreateConversation;
using Vonage.Conversations.DeleteConversation;
using Vonage.Conversations.GetConversation;
using Vonage.Conversations.GetConversations;
using Vonage.Serialization;

namespace Vonage.Conversations;
Expand Down Expand Up @@ -33,6 +34,13 @@ public interface IConversationsClient
/// <param name="request">The request.</param>
/// <returns>Success or Failure.</returns>
Task<Result<Conversation>> GetConversationAsync(Result<GetConversationRequest> request);

/// <summary>
/// Retrieves conversations.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>Success or Failure.</returns>
Task<Result<GetConversationsResponse>> GetConversationsAsync(Result<GetConversationsRequest> request);
}

internal class ConversationsClient : IConversationsClient
Expand All @@ -58,4 +66,8 @@ public Task<Result<Unit>> DeleteConversationAsync(Result<DeleteConversationReque
/// <inheritdoc />
public Task<Result<Conversation>> GetConversationAsync(Result<GetConversationRequest> request) =>
this.vonageClient.SendWithResponseAsync<GetConversationRequest, Conversation>(request);

/// <inheritdoc />
public Task<Result<GetConversationsResponse>> GetConversationsAsync(Result<GetConversationsRequest> request) =>
this.vonageClient.SendWithResponseAsync<GetConversationsRequest, GetConversationsResponse>(request);
}
10 changes: 7 additions & 3 deletions Vonage/Conversations/GetConversations/GetConversationsRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace Vonage.Conversations.GetConversations;
/// <inheritdoc />
public readonly struct GetConversationsRequest : IVonageRequest
{
private const string ExpectedDateFormat = "yyyy-MM-ddTHH:mm:ssZ";

/// <summary>
/// The cursor to start returning results from. You are not expected to provide this manually, but to follow the url
/// provided in _links.next.href or _links.prev.href in the response which contains a cursor value.
Expand Down Expand Up @@ -45,7 +47,9 @@ namespace Vonage.Conversations.GetConversations;
public static IBuilderForOptional Build() => new GetConversationsRequestBuilder();

/// <inheritdoc />
public HttpRequestMessage BuildRequestMessage() => throw new NotImplementedException();
public HttpRequestMessage BuildRequestMessage() => VonageRequestBuilder
.Initialize(HttpMethod.Get, this.GetEndpointPath())
.Build();

/// <inheritdoc />
public string GetEndpointPath() => UriHelpers.BuildUri("/v1/conversations", this.GetQueryStringParameters());
Expand All @@ -58,9 +62,9 @@ private Dictionary<string, string> GetQueryStringParameters()
{"order", this.Order.AsString(EnumFormat.Description)},
};
this.StartDate.IfSome(value =>
parameters.Add("date_start", value.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture)));
parameters.Add("date_start", value.ToString(ExpectedDateFormat, CultureInfo.InvariantCulture)));
this.EndDate.IfSome(value =>
parameters.Add("date_end", value.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture)));
parameters.Add("date_end", value.ToString(ExpectedDateFormat, CultureInfo.InvariantCulture)));
this.Cursor.IfSome(value => parameters.Add("cursor", value));
return parameters;
}
Expand Down

0 comments on commit d53529f

Please # to comment.