Skip to content

Commit

Permalink
Feat/verification dedicated report for each method (#40)
Browse files Browse the repository at this point in the history
* refactor: add dedicated report id by method

* refactor: add dedicated ReportIdentity

* refactor: remove generic report id/identity

delete AccessingPolymorphicType.cs

* refactor: rename report related classes

* refactor: rename phonecall to callout

* refactor: rename variables to callout

* refactor: inline variables in VerificationStartTests.cs
  • Loading branch information
Dovchik authored Feb 13, 2024
1 parent 4bda45e commit 0f794f6
Show file tree
Hide file tree
Showing 18 changed files with 211 additions and 154 deletions.
33 changes: 0 additions & 33 deletions examples/Console/AccessingPolymorphicType.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Sinch.Verification.Hooks
{
public class PhoneCallRequestEventResponse : RequestEventResponseBase
public class CalloutRequestEventResponse : RequestEventResponseBase
{
[JsonPropertyName("callout")]
public Callout Callout { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Sinch.Verification.Report.Request
{
public class PhoneCallVerificationReportRequest : VerifyReportRequest
public class ReportCalloutVerificationRequest : VerifyReportRequest
{
public override string Method { get; } = VerificationMethod.Callout.Value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Sinch.Verification.Report.Request
{
public class FlashCallVerificationReportRequest : VerifyReportRequest
public class ReportFlashCallVerificationRequest : VerifyReportRequest
{
/// <inheritdoc />
public override string Method { get; } = VerificationMethod.FlashCall.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Sinch.Verification.Report.Request
{
public class SmsVerificationReportRequest : VerifyReportRequest
public class ReportSmsVerificationRequest : VerifyReportRequest
{
public override string Method { get; } = VerificationMethod.Sms.Value;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Sinch.Verification.Report.Response
{
public class PhoneCallVerificationReportResponse : VerificationReportResponseBase, IVerificationReportResponse
public class ReportCalloutVerificationResponse : VerificationReportResponseBase, IVerificationReportResponse
{
/// <summary>
/// Shows whether the call is complete or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Sinch.Verification.Report.Response
{
public class FlashCallVerificationReportResponse : VerificationReportResponseBase, IVerificationReportResponse
public class ReportFlashCallVerificationResponse : VerificationReportResponseBase, IVerificationReportResponse
{
/// <summary>
/// Free text that the client is sending, used to show if the call/SMS was intercepted or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Sinch.Verification.Report.Response
{
public class SmsVerificationReportResponse : VerificationReportResponseBase, IVerificationReportResponse
public class ReportSmsVerificationResponse : VerificationReportResponseBase, IVerificationReportResponse
{
/// <summary>
/// Free text that the client is sending, used to show if the call/SMS was intercepted or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ public override IVerificationReportResponse Read(ref Utf8JsonReader reader, Type
var method = descriptor.Value.GetString();
if (method == VerificationMethod.Sms.Value)
{
return (SmsVerificationReportResponse)elem.Deserialize(
typeof(SmsVerificationReportResponse),
return (ReportSmsVerificationResponse)elem.Deserialize(
typeof(ReportSmsVerificationResponse),
options);
}

if (method == VerificationMethod.Callout.Value)
{
return (PhoneCallVerificationReportResponse)elem.Deserialize(
typeof(PhoneCallVerificationReportResponse),
return (ReportCalloutVerificationResponse)elem.Deserialize(
typeof(ReportCalloutVerificationResponse),
options);
}

if (method == VerificationMethod.FlashCall.Value)
{
return (FlashCallVerificationReportResponse)elem.Deserialize(
typeof(FlashCallVerificationReportResponse), options);
return (ReportFlashCallVerificationResponse)elem.Deserialize(
typeof(ReportFlashCallVerificationResponse), options);
}

throw new JsonException($"Failed to match verification method object, got {descriptor.Name}");
Expand All @@ -75,15 +75,15 @@ public override void Write(Utf8JsonWriter writer, IVerificationReportResponse va
{
switch (value)
{
case FlashCallVerificationReportResponse flashCallVerificationReportResponse:
case ReportFlashCallVerificationResponse flashCallVerificationReportResponse:
JsonSerializer.Serialize(
writer, flashCallVerificationReportResponse, options);
break;
case PhoneCallVerificationReportResponse phoneCallVerificationReportResponse:
case ReportCalloutVerificationResponse reportCalloutVerificationResponse:
JsonSerializer.Serialize(
writer, phoneCallVerificationReportResponse, options);
writer, reportCalloutVerificationResponse, options);
break;
case SmsVerificationReportResponse smsVerificationReportResponse:
case ReportSmsVerificationResponse smsVerificationReportResponse:
JsonSerializer.Serialize(
writer, smsVerificationReportResponse, options);
break;
Expand Down
142 changes: 118 additions & 24 deletions src/Sinch/Verification/SinchVerification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Task<StartFlashCallVerificationResponse> StartFlashCall(StartFlashCallVerificati
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<StartPhoneCallVerificationResponse> StartPhoneCall(StartPhoneCallVerificationRequest request,
Task<StartCalloutVerificationResponse> StartCallout(StartCalloutVerificationRequest request,
CancellationToken cancellationToken = default);

/// <summary>
Expand All @@ -66,14 +66,46 @@ Task<StartDataVerificationResponse> StartSeamless(StartDataVerificationRequest r
/// <summary>
/// Report the received verification code to verify it,
/// using the identity of the user (in most cases, the phone number).
/// For an SMS PIN verification or Phone Call verification, this is the OTP code.
/// For flashcalls, this is the CLI.
/// </summary>
/// <param name="endpoint">For type number use a E.164-compatible phone number.</param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<IVerificationReportResponse> ReportIdentity(string endpoint, VerifyReportRequest request,
Task<ReportSmsVerificationResponse> ReportSmsByIdentity(string endpoint, ReportSmsVerificationRequest request,
CancellationToken cancellationToken = default);

/// <summary>
/// Report the received verification code to verify it,
/// using the identity of the user (in most cases, the phone number).
/// </summary>
/// <param name="endpoint">For type number use a E.164-compatible phone number.</param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<ReportFlashCallVerificationResponse> ReportFlashCallByIdentity(string endpoint,
ReportFlashCallVerificationRequest request,
CancellationToken cancellationToken = default);

/// <summary>
/// Report the received verification code to verify it,
/// using the identity of the user (in most cases, the phone number).
/// </summary>
/// <param name="endpoint">For type number use a E.164-compatible phone number.</param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<ReportCalloutVerificationResponse> ReportCalloutByIdentity(string endpoint,
ReportCalloutVerificationRequest request,
CancellationToken cancellationToken = default);

/// <summary>
/// Report the received verification code to verify it, using the Verification ID of the Verification request.
/// </summary>
/// <param name="id"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<ReportSmsVerificationResponse> ReportSmsById(string id, ReportSmsVerificationRequest request,
CancellationToken cancellationToken = default);

/// <summary>
Expand All @@ -83,7 +115,19 @@ Task<IVerificationReportResponse> ReportIdentity(string endpoint, VerifyReportRe
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<IVerificationReportResponse> ReportId(string id, VerifyReportRequest request,
Task<ReportFlashCallVerificationResponse> ReportFlashCallById(string id,
ReportFlashCallVerificationRequest request,
CancellationToken cancellationToken = default);

/// <summary>
/// Report the received verification code to verify it, using the Verification ID of the Verification request.
/// </summary>
/// <param name="id"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<ReportCalloutVerificationResponse> ReportCalloutById(string id,
ReportCalloutVerificationRequest request,
CancellationToken cancellationToken = default);
}

Expand All @@ -100,15 +144,6 @@ public SinchVerification(ILoggerAdapter<SinchVerification> logger, Uri baseAddre
_http = http;
}

private Task<IStartVerificationResponse> Start(StartVerificationRequest request,
CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress, "verification/v1/verifications");
_logger?.LogDebug("Starting verification...");
return _http.Send<StartVerificationRequest, IStartVerificationResponse>(uri, HttpMethod.Post, request,
cancellationToken);
}

/// <inheritdoc />
public async Task<StartSmsVerificationResponse> StartSms(StartSmsVerificationRequest request,
CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -151,7 +186,7 @@ public async Task<StartFlashCallVerificationResponse> StartFlashCall(StartFlashC
}

/// <inheritdoc />
public async Task<StartPhoneCallVerificationResponse> StartPhoneCall(StartPhoneCallVerificationRequest request,
public async Task<StartCalloutVerificationResponse> StartCallout(StartCalloutVerificationRequest request,
CancellationToken cancellationToken = default)
{
var result = await Start(new StartVerificationRequest
Expand All @@ -161,7 +196,7 @@ public async Task<StartPhoneCallVerificationResponse> StartPhoneCall(StartPhoneC
Method = request.Method,
Reference = request.Reference
}, cancellationToken);
return result as StartPhoneCallVerificationResponse;
return result as StartCalloutVerificationResponse;
}

/// <inheritdoc />
Expand All @@ -179,7 +214,7 @@ public async Task<StartDataVerificationResponse> StartSeamless(StartDataVerifica
}


public Task<IVerificationReportResponse> ReportIdentity(string endpoint, VerifyReportRequest request,
private Task<IVerificationReportResponse> ReportIdentity(string endpoint, VerifyReportRequest request,
CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress, $"verification/v1/verifications/number/{endpoint}");
Expand All @@ -188,7 +223,31 @@ public Task<IVerificationReportResponse> ReportIdentity(string endpoint, VerifyR
return Report(request, uri, cancellationToken);
}

public Task<IVerificationReportResponse> ReportId(string id, VerifyReportRequest request,
public async Task<ReportSmsVerificationResponse> ReportSmsByIdentity(string endpoint,
ReportSmsVerificationRequest request,
CancellationToken cancellationToken = default)
{
var result = await ReportIdentity(endpoint, request, cancellationToken);
return result as ReportSmsVerificationResponse;
}

public async Task<ReportFlashCallVerificationResponse> ReportFlashCallByIdentity(string endpoint,
ReportFlashCallVerificationRequest request,
CancellationToken cancellationToken = default)
{
var result = await ReportIdentity(endpoint, request, cancellationToken);
return result as ReportFlashCallVerificationResponse;
}

public async Task<ReportCalloutVerificationResponse> ReportCalloutByIdentity(string endpoint,
ReportCalloutVerificationRequest request,
CancellationToken cancellationToken = default)
{
var result = await ReportIdentity(endpoint, request, cancellationToken);
return result as ReportCalloutVerificationResponse;
}

private Task<IVerificationReportResponse> ReportId(string id, VerifyReportRequest request,
CancellationToken cancellationToken = default)
{
_logger?.LogDebug("Reporting the the code with id...");
Expand All @@ -197,22 +256,57 @@ public Task<IVerificationReportResponse> ReportId(string id, VerifyReportRequest
return Report(request, uri, cancellationToken);
}

/// <inheritdoc />
public async Task<ReportSmsVerificationResponse> ReportSmsById(string id, ReportSmsVerificationRequest request,
CancellationToken cancellationToken = default)
{
var result = await ReportId(id, request, cancellationToken);
return result as ReportSmsVerificationResponse;
}

/// <inheritdoc />
public async Task<ReportFlashCallVerificationResponse> ReportFlashCallById(string id,
ReportFlashCallVerificationRequest request,
CancellationToken cancellationToken = default)
{
var result = await ReportId(id, request, cancellationToken);
return result as ReportFlashCallVerificationResponse;
}

/// <inheritdoc />
public async Task<ReportCalloutVerificationResponse> ReportCalloutById(string id,
ReportCalloutVerificationRequest request,
CancellationToken cancellationToken = default)
{
var result = await ReportId(id, request, cancellationToken);
return result as ReportCalloutVerificationResponse;
}

private Task<IStartVerificationResponse> Start(StartVerificationRequest request,
CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress, "verification/v1/verifications");
_logger?.LogDebug("Starting verification...");
return _http.Send<StartVerificationRequest, IStartVerificationResponse>(uri, HttpMethod.Post, request,
cancellationToken);
}

private Task<IVerificationReportResponse> Report(VerifyReportRequest request,
Uri uri, CancellationToken cancellationToken)
{
return request switch
{
FlashCallVerificationReportRequest flashCallVerificationReportRequest =>
_http.Send<FlashCallVerificationReportRequest, IVerificationReportResponse>(uri, HttpMethod.Put,
ReportFlashCallVerificationRequest flashCallVerificationReportRequest =>
_http.Send<ReportFlashCallVerificationRequest, IVerificationReportResponse>(uri, HttpMethod.Put,
flashCallVerificationReportRequest,
cancellationToken),
SmsVerificationReportRequest smsVerificationRequest => _http
.Send<SmsVerificationReportRequest, IVerificationReportResponse>(
ReportSmsVerificationRequest smsVerificationRequest => _http
.Send<ReportSmsVerificationRequest, IVerificationReportResponse>(
uri, HttpMethod.Put,
smsVerificationRequest,
cancellationToken),
PhoneCallVerificationReportRequest phoneRequest => _http
.Send<PhoneCallVerificationReportRequest, IVerificationReportResponse>(uri, HttpMethod.Put,
ReportCalloutVerificationRequest phoneRequest => _http
.Send<ReportCalloutVerificationRequest, IVerificationReportResponse>(uri, HttpMethod.Put,
phoneRequest,
cancellationToken),
_ => throw new ArgumentOutOfRangeException(nameof(request))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Sinch.Verification.Start.Request
{
public class StartPhoneCallVerificationRequest : StartVerificationRequestBase
public class StartCalloutVerificationRequest : StartVerificationRequestBase
{
/// <summary>
/// The type of the verification request. Set to Phone Call
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Sinch.Verification.Start.Response
{
public class StartCalloutVerificationResponse : VerificationStartResponseBase, IStartVerificationResponse
{

}
}

This file was deleted.

Loading

0 comments on commit 0f794f6

Please sign in to comment.