Skip to content

Commit

Permalink
feat: implement builder for Callback in CreateConversation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Dec 7, 2023
1 parent 7a8c4c1 commit 61511f3
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using Vonage.Common.Test.Extensions;
using Vonage.Conversations.CreateConversation;
using Xunit;
Expand Down Expand Up @@ -35,6 +36,23 @@ public void Build_ShouldReturnFailure_GivenDisplayNameLengthIsAbove50Characters(
.Should()
.BeParsingFailure("DisplayName length cannot be higher than 50.");

[Theory]
[InlineData("PUT")]
[InlineData("DELETE")]
[InlineData("HEAD")]
[InlineData("OPTIONS")]
[InlineData("TRACE")]
[InlineData("PATCH")]
[InlineData("CONNECT")]
public void Build_ShouldReturnFailure_GivenHttpMethodIsNotGetOrPost(string method) =>
CreateConversationRequest.Build()
.WithCallback(new Callback(new Uri("https://example.com"), "mask",
new CallbackParameters("appId", new Uri("https://example.com")), new HttpMethod(method)))
.Create()
.Map(request => request.Callback)
.Should()
.BeParsingFailure("Callback HttpMethod must be GET or POST.");

[Theory]
[InlineData("")]
[InlineData(" ")]
Expand All @@ -55,6 +73,17 @@ public void Build_ShouldReturnFailure_GivenNameLengthIsAbove100Characters() =>
.Should()
.BeParsingFailure("Name length cannot be higher than 100.");

[Fact]
public void Build_ShouldSetCallback() =>
CreateConversationRequest.Build()
.WithCallback(new Callback(new Uri("https://example.com"), "mask",
new CallbackParameters("appId", new Uri("https://example.com")), HttpMethod.Get))
.Create()
.Map(request => request.Callback)
.Should()
.BeSuccess(new Callback(new Uri("https://example.com"), "mask",
new CallbackParameters("appId", new Uri("https://example.com")), HttpMethod.Get));

[Fact]
public void Build_ShouldSetDisplayName() =>
CreateConversationRequest.Build()
Expand Down
10 changes: 10 additions & 0 deletions Vonage/Conversations/CreateConversation/Callback.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Net.Http;

namespace Vonage.Conversations.CreateConversation;

public record Callback(
Uri Url,
string EventMask,
CallbackParameters Parameters,
HttpMethod Method);
5 changes: 5 additions & 0 deletions Vonage/Conversations/CreateConversation/CallbackParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using System;

namespace Vonage.Conversations.CreateConversation;

public record CallbackParameters(string ApplicationId, Uri NccoUrl);
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ namespace Vonage.Conversations.CreateConversation;
/// <inheritdoc />
public readonly struct CreateConversationRequest : IVonageRequest
{
/// <summary>
/// </summary>
public Maybe<Callback> Callback { get; internal init; }

/// <summary>
/// </summary>
public Maybe<string> DisplayName { get; internal init; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using Vonage.Common.Client;
using Vonage.Common.Failures;
using Vonage.Common.Monads;
using Vonage.Common.Validation;

Expand All @@ -9,6 +13,9 @@ internal class CreateConversationRequestBuilder : IBuilderForOptional
{
private const int DisplayNameMaxLength = 50;
private const int NameMaxLength = 100;

private readonly IEnumerable<HttpMethod> allowedMethods = new[] {HttpMethod.Get, HttpMethod.Post};
private Maybe<Callback> callback;
private Maybe<Properties> properties;
private Maybe<string> name;
private Maybe<string> displayName;
Expand All @@ -21,13 +28,20 @@ public Result<CreateConversationRequest> Create() => Result<CreateConversationRe
DisplayName = this.displayName,
ImageUrl = this.uri,
Properties = this.properties,
Callback = this.callback,
})
.Map(InputEvaluation<CreateConversationRequest>.Evaluate)
.Bind(evaluation => evaluation.WithRules(
VerifyName,
VerifyNameLength,
VerifyDisplayName,
VerifyDisplayNameLength));
VerifyDisplayNameLength, this.VerifyCallbackHttpMethod));

public IBuilderForOptional WithCallback(Callback value)
{
this.callback = value;
return this;
}

public IBuilderForOptional WithDisplayName(string value)
{
Expand All @@ -53,6 +67,14 @@ public IBuilderForOptional WithProperties(Properties value)
return this;
}

private Result<CreateConversationRequest> VerifyCallbackHttpMethod(CreateConversationRequest request) =>
request.Callback.Match(
some => this.allowedMethods.Contains(some.Method)
? Result<CreateConversationRequest>.FromSuccess(request)
: ResultFailure.FromErrorMessage("Callback HttpMethod must be GET or POST.")
.ToResult<CreateConversationRequest>(),
() => request);

private static Result<CreateConversationRequest> VerifyDisplayName(CreateConversationRequest request) =>
request.DisplayName.Match(
some => InputValidation.VerifyNotEmpty(request, some, nameof(request.DisplayName)),
Expand Down Expand Up @@ -80,6 +102,13 @@ private static Result<CreateConversationRequest> VerifyNameLength(CreateConversa
/// </summary>
public interface IBuilderForOptional : IVonageRequestBuilder<CreateConversationRequest>
{
/// <summary>
/// Sets the Callback.
/// </summary>
/// <param name="value">The callback.</param>
/// <returns>The builder.</returns>
IBuilderForOptional WithCallback(Callback value);

/// <summary>
/// Sets the Display Name.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Vonage.Common;
using Vonage.Common.Monads;
Expand Down Expand Up @@ -27,11 +26,4 @@ public record Timestamp(DateTimeOffset Created,
[property: JsonConverter(typeof(MaybeJsonConverter<DateTimeOffset>))]
Maybe<DateTimeOffset> Destroyed);

public record Properties(
[property: JsonPropertyName("ttl")] int TimeToLive, string Type,
[property: JsonPropertyName("custom_sort_key")]
string CustomSortKey,
[property: JsonPropertyName("custom_data")]
Dictionary<string, string> CustomData);

public record Links(HalLink Self);
11 changes: 11 additions & 0 deletions Vonage/Conversations/CreateConversation/Properties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Vonage.Conversations.CreateConversation;

public record Properties(
[property: JsonPropertyName("ttl")] int TimeToLive, string Type,
[property: JsonPropertyName("custom_sort_key")]
string CustomSortKey,
[property: JsonPropertyName("custom_data")]
Dictionary<string, string> CustomData);

0 comments on commit 61511f3

Please sign in to comment.