Skip to content

Commit

Permalink
refactor: add verification channel enum for VerifyV2
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Sep 13, 2024
1 parent 6dae0f9 commit af7cd63
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 43 deletions.
17 changes: 10 additions & 7 deletions Vonage/VerifyV2/StartVerification/Email/EmailWorkflow.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#region
using System.Text.Json.Serialization;
using EnumsNET;
using Vonage.Common;
using Vonage.Common.Monads;
using Vonage.Common.Serialization;
#endregion

namespace Vonage.VerifyV2.StartVerification.Email;

Expand All @@ -16,10 +19,6 @@ private EmailWorkflow(MailAddress to, Maybe<MailAddress> from)
this.From = from;
}

/// <inheritdoc />
[JsonPropertyOrder(0)]
public string Channel => "email";

/// <summary>
/// The email address to send the verification request from.
/// </summary>
Expand All @@ -35,6 +34,13 @@ private EmailWorkflow(MailAddress to, Maybe<MailAddress> from)
[JsonConverter(typeof(EmailJsonConverter))]
public MailAddress To { get; }

/// <inheritdoc />
[JsonPropertyOrder(0)]
public string Channel => VerificationChannel.Email.AsString(EnumFormat.Description);

/// <inheritdoc />
public string Serialize(IJsonSerializer serializer) => serializer.SerializeObject(this);

/// <summary>
/// Parses the input into a EmailWorkflow.
/// </summary>
Expand All @@ -52,7 +58,4 @@ public static Result<EmailWorkflow> Parse(string to) =>
public static Result<EmailWorkflow> Parse(string to, string from) =>
MailAddress.Parse(to)
.Merge(MailAddress.Parse(from), (toNumber, fromNumber) => new EmailWorkflow(toNumber, fromNumber));

/// <inheritdoc />
public string Serialize(IJsonSerializer serializer) => serializer.SerializeObject(this);
}
17 changes: 10 additions & 7 deletions Vonage/VerifyV2/StartVerification/SilentAuth/SilentAuthWorkflow.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#region
using System;
using System.Text.Json.Serialization;
using EnumsNET;
using Vonage.Common;
using Vonage.Common.Monads;
using Vonage.Common.Serialization;
#endregion

namespace Vonage.VerifyV2.StartVerification.SilentAuth;

Expand All @@ -17,10 +20,6 @@ private SilentAuthWorkflow(PhoneNumber to, Uri redirectUrl = null)
this.RedirectUrl = redirectUrl ?? Maybe<Uri>.None;
}

/// <inheritdoc />
[JsonPropertyOrder(0)]
public string Channel => "silent_auth";

/// <summary>
/// Final redirect added at the end of the check_url request/response lifecycle. See the documentation for integrations. Will contain the request_id and code as a url fragment after the URL.
/// </summary>
Expand All @@ -37,6 +36,13 @@ private SilentAuthWorkflow(PhoneNumber to, Uri redirectUrl = null)
[JsonConverter(typeof(PhoneNumberJsonConverter))]
public PhoneNumber To { get; }

/// <inheritdoc />
[JsonPropertyOrder(0)]
public string Channel => VerificationChannel.SilentAuth.AsString(EnumFormat.Description);

/// <inheritdoc />
public string Serialize(IJsonSerializer serializer) => serializer.SerializeObject(this);

/// <summary>
/// Parses the input into a SilentAuthWorkflow.
/// </summary>
Expand All @@ -53,7 +59,4 @@ public static Result<SilentAuthWorkflow> Parse(string to) =>
/// <returns>Success or failure.</returns>
public static Result<SilentAuthWorkflow> Parse(string to, Uri redirectUrl) =>
PhoneNumber.Parse(to).Map(phoneNumber => new SilentAuthWorkflow(phoneNumber, redirectUrl));

/// <inheritdoc />
public string Serialize(IJsonSerializer serializer) => serializer.SerializeObject(this);
}
23 changes: 15 additions & 8 deletions Vonage/VerifyV2/StartVerification/Sms/SmsWorkflow.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#region
using System.Text.Json.Serialization;
using EnumsNET;
using Vonage.Common;
using Vonage.Common.Monads;
using Vonage.Common.Serialization;
using Vonage.Common.Validation;
#endregion

namespace Vonage.VerifyV2.StartVerification.Sms;

Expand All @@ -24,10 +27,6 @@ private SmsWorkflow(PhoneNumber to, Maybe<string> hash, Maybe<string> entityId,
this.To = to;
}

/// <inheritdoc />
[JsonPropertyOrder(0)]
public string Channel => "sms";

/// <summary>
/// Optional Android Application Hash Key for automatic code detection on a user's device.
/// </summary>
Expand Down Expand Up @@ -74,14 +73,25 @@ private SmsWorkflow(PhoneNumber to, Maybe<string> hash, Maybe<string> entityId,
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public Maybe<PhoneNumber> From { get; }

/// <inheritdoc />
[JsonPropertyOrder(0)]
public string Channel => VerificationChannel.Sms.AsString(EnumFormat.Description);

/// <inheritdoc />
public string Serialize(IJsonSerializer serializer) => serializer.SerializeObject(this);

/// <summary>
/// Parses the input into a SmsWorkflow.
/// </summary>
/// <param name="to">The phone number to contact.</param>
/// <param name="hash">The Android application hash key.</param>
/// <param name="entityId">Optional PEID required for SMS delivery using Indian Carriers</param>
/// <param name="contentId">Optional value corresponding to a TemplateID for SMS delivery using Indian Carriers</param>
/// <param name="from">An optional sender number, in the E.164 format. Don't use a leading + or 00 when entering a phone number, start with the country code, for example, 447700900000. If no from number is given, the request will default to the brand.</param>
/// <param name="from">
/// An optional sender number, in the E.164 format. Don't use a leading + or 00 when entering a phone
/// number, start with the country code, for example, 447700900000. If no from number is given, the request will
/// default to the brand.
/// </param>
/// <returns>Success or failure.</returns>
public static Result<SmsWorkflow> Parse(string to, string hash = null, string entityId = null,
string contentId = null, string from = null)
Expand All @@ -106,9 +116,6 @@ public static Result<SmsWorkflow> Parse(string to, string hash = null, string en
.Bind(VerifyWorkflowContentIdLength);
}

/// <inheritdoc />
public string Serialize(IJsonSerializer serializer) => serializer.SerializeObject(this);

private static Result<SmsWorkflow> VerifyWorkflowHashLength(
SmsWorkflow request) =>
request.Hash.Match(some => InputValidation.VerifyLength(request, some, 11, nameof(request.Hash)),
Expand Down
17 changes: 10 additions & 7 deletions Vonage/VerifyV2/StartVerification/Voice/VoiceWorkflow.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#region
using System.Text.Json.Serialization;
using EnumsNET;
using Vonage.Common;
using Vonage.Common.Monads;
using Vonage.Common.Serialization;
#endregion

namespace Vonage.VerifyV2.StartVerification.Voice;

Expand All @@ -12,10 +15,6 @@ namespace Vonage.VerifyV2.StartVerification.Voice;
{
private VoiceWorkflow(PhoneNumber to) => this.To = to;

/// <inheritdoc />
[JsonPropertyOrder(0)]
public string Channel => "voice";

/// <summary>
/// The phone number to contact, in the E.164 format. Don't use a leading + or 00 when entering a phone number, start
/// with the country code, for example, 447700900000.
Expand All @@ -24,14 +23,18 @@ namespace Vonage.VerifyV2.StartVerification.Voice;
[JsonConverter(typeof(PhoneNumberJsonConverter))]
public PhoneNumber To { get; }

/// <inheritdoc />
[JsonPropertyOrder(0)]
public string Channel => VerificationChannel.Voice.AsString(EnumFormat.Description);

/// <inheritdoc />
public string Serialize(IJsonSerializer serializer) => serializer.SerializeObject(this);

/// <summary>
/// Parses the input into a VoiceWorkflow.
/// </summary>
/// <param name="to">The phone number to contact.</param>
/// <returns>Success or failure.</returns>
public static Result<VoiceWorkflow> Parse(string to) =>
PhoneNumber.Parse(to).Map(phoneNumber => new VoiceWorkflow(phoneNumber));

/// <inheritdoc />
public string Serialize(IJsonSerializer serializer) => serializer.SerializeObject(this);
}
17 changes: 10 additions & 7 deletions Vonage/VerifyV2/StartVerification/WhatsApp/WhatsAppWorkflow.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#region
using System.Text.Json.Serialization;
using EnumsNET;
using Vonage.Common;
using Vonage.Common.Monads;
using Vonage.Common.Serialization;
#endregion

namespace Vonage.VerifyV2.StartVerification.WhatsApp;

Expand All @@ -16,10 +19,6 @@ private WhatsAppWorkflow(PhoneNumber to, PhoneNumber from)
this.From = from;
}

/// <inheritdoc />
[JsonPropertyOrder(0)]
public string Channel => "whatsapp";

/// <summary>
/// An optional sender number, in the E.164 format. Don't use a leading + or 00 when entering a phone number, start
/// with the country code, for example, 447700900000.
Expand All @@ -36,6 +35,13 @@ private WhatsAppWorkflow(PhoneNumber to, PhoneNumber from)
[JsonConverter(typeof(PhoneNumberJsonConverter))]
public PhoneNumber To { get; }

/// <inheritdoc />
[JsonPropertyOrder(0)]
public string Channel => VerificationChannel.WhatsApp.AsString(EnumFormat.Description);

/// <inheritdoc />
public string Serialize(IJsonSerializer serializer) => serializer.SerializeObject(this);

/// <summary>
/// Parses the input into a WhatsAppWorkflow.
/// </summary>
Expand All @@ -45,7 +51,4 @@ private WhatsAppWorkflow(PhoneNumber to, PhoneNumber from)
public static Result<WhatsAppWorkflow> Parse(string to, string from) =>
PhoneNumber.Parse(to).Merge(PhoneNumber.Parse(from),
(toNumber, fromNumber) => new WhatsAppWorkflow(toNumber, fromNumber));

/// <inheritdoc />
public string Serialize(IJsonSerializer serializer) => serializer.SerializeObject(this);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#region
using System.Text.Json.Serialization;
using EnumsNET;
using Vonage.Common;
using Vonage.Common.Monads;
using Vonage.Common.Serialization;
#endregion

namespace Vonage.VerifyV2.StartVerification.WhatsAppInteractive;

Expand All @@ -12,10 +15,6 @@ namespace Vonage.VerifyV2.StartVerification.WhatsAppInteractive;
{
private WhatsAppInteractiveWorkflow(PhoneNumber to) => this.To = to;

/// <inheritdoc />
[JsonPropertyOrder(0)]
public string Channel => "whatsapp_interactive";

/// <summary>
/// The phone number to contact, in the E.164 format. Don't use a leading + or 00 when entering a phone number, start
/// with the country code, for example, 447700900000.
Expand All @@ -24,14 +23,18 @@ namespace Vonage.VerifyV2.StartVerification.WhatsAppInteractive;
[JsonConverter(typeof(PhoneNumberJsonConverter))]
public PhoneNumber To { get; }

/// <inheritdoc />
[JsonPropertyOrder(0)]
public string Channel => VerificationChannel.WhatsAppInteractive.AsString(EnumFormat.Description);

/// <inheritdoc />
public string Serialize(IJsonSerializer serializer) => serializer.SerializeObject(this);

/// <summary>
/// Parses the input into a WhatsAppInteractiveWorkflow.
/// </summary>
/// <param name="to">The phone number to contact.</param>
/// <returns>Success or failure.</returns>
public static Result<WhatsAppInteractiveWorkflow> Parse(string to) =>
PhoneNumber.Parse(to).Map(phoneNumber => new WhatsAppInteractiveWorkflow(phoneNumber));

/// <inheritdoc />
public string Serialize(IJsonSerializer serializer) => serializer.SerializeObject(this);
}
41 changes: 41 additions & 0 deletions Vonage/VerifyV2/VerificationChannel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#region
using System.ComponentModel;
#endregion

namespace Vonage.VerifyV2;

/// <summary>
/// Represents supported verification channels.
/// </summary>
public enum VerificationChannel
{
/// <summary>
/// SMS
/// </summary>
[Description("sms")] Sms,

/// <summary>
/// Voice
/// </summary>
[Description("voice")] Voice,

/// <summary>
/// Email
/// </summary>
[Description("email")] Email,

/// <summary>
/// Silent Auth
/// </summary>
[Description("silent_auth")] SilentAuth,

/// <summary>
/// WhatsApp
/// </summary>
[Description("whatsapp")] WhatsApp,

/// <summary>
/// WhatsApp Interactive
/// </summary>
[Description("whatsapp_interactive")] WhatsAppInteractive,
}

0 comments on commit af7cd63

Please sign in to comment.