Skip to content

Commit

Permalink
feat: update auth for Numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Dec 4, 2024
1 parent 165ab9a commit 0d18216
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
12 changes: 6 additions & 6 deletions Vonage.Test/NumbersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public async Task GetAvailableNumbersAsync()
const string expectedResponse =
@"{""count"": 1234,""numbers"": [{""country"": ""GB"",""msisdn"": ""447700900000"",""type"": ""mobile-lvn"",""cost"": ""1.25"",""features"": [""VOICE"",""SMS""]}]}";
var expectedUri =
$"{this.RestUrl}/number/search?country=GB&api_key={this.ApiKey}&api_secret={this.ApiSecret}&";
$"{this.RestUrl}/number/search?country=GB&";
var request = new NumberSearchRequest {Country = "GB"};
this.Setup(expectedUri, expectedResponse);
var response = await this.client.NumbersClient.GetAvailableNumbersAsync(request);
Expand All @@ -116,7 +116,7 @@ public async Task GetAvailableNumbersAsyncWithAdditionalData()
const string expectedResponse =
@"{""count"": 1234,""numbers"": [{""country"": ""GB"",""msisdn"": ""447700900000"",""type"": ""mobile-lvn"",""cost"": ""1.25"",""features"": [""VOICE"",""SMS""], ""moHttpUrl"": ""https://example.com/webhooks/inbound-sms"", ""messagesCallbackType"": ""app"", ""messagesCallbackValue"": ""aaaaaaaa-bbbb-cccc-dddd-0123456789ab"", ""voiceCallbackType"": ""app"", ""voiceCallbackValue"": ""aaaaaaaa-bbbb-cccc-dddd-0123456789ab""}]}";
var expectedUri =
$"{this.RestUrl}/number/search?country=GB&api_key={this.ApiKey}&api_secret={this.ApiSecret}&";
$"{this.RestUrl}/number/search?country=GB&";
var request = new NumberSearchRequest {Country = "GB"};
this.Setup(expectedUri, expectedResponse);
var response = await this.client.NumbersClient.GetAvailableNumbersAsync(request);
Expand All @@ -141,7 +141,7 @@ public async Task GetAvailableNumbersAsyncWithCredentials()
const string expectedResponse =
@"{""count"": 1234,""numbers"": [{""country"": ""GB"",""msisdn"": ""447700900000"",""type"": ""mobile-lvn"",""cost"": ""1.25"",""features"": [""VOICE"",""SMS""]}]}";
var expectedUri =
$"{this.RestUrl}/number/search?country=GB&type=mobile-lvn&pattern=12345&search_pattern=1&features=SMS&size=10&index=1&api_key={this.ApiKey}&api_secret={this.ApiSecret}&";
$"{this.RestUrl}/number/search?country=GB&type=mobile-lvn&pattern=12345&search_pattern=1&features=SMS&size=10&index=1&";
var request = new NumberSearchRequest
{
Country = "GB", Type = "mobile-lvn", Pattern = "12345", SearchPattern = SearchPattern.Contains,
Expand All @@ -165,7 +165,7 @@ public async Task GetOwnedNumbersAsync()
const string expectedResponse =
@"{""count"": 1234,""numbers"": [{""country"": ""GB"",""msisdn"": ""447700900000"",""type"": ""mobile-lvn"",""cost"": ""1.25"",""features"": [""VOICE"",""SMS""]}]}";
var expectedUri =
$"{this.RestUrl}/account/numbers?country=GB&api_key={this.ApiKey}&api_secret={this.ApiSecret}&";
$"{this.RestUrl}/account/numbers?country=GB&";
var request = new NumberSearchRequest {Country = "GB"};
this.Setup(expectedUri, expectedResponse);
var response = await this.client.NumbersClient.GetOwnedNumbersAsync(request);
Expand All @@ -186,7 +186,7 @@ public async Task GetOwnedNumbersAsyncWithAdditionalData()
const string expectedResponse =
@"{""count"": 1234,""numbers"": [{""country"": ""GB"",""msisdn"": ""447700900000"",""type"": ""mobile-lvn"",""cost"": ""1.25"",""features"": [""VOICE"",""SMS""], ""moHttpUrl"": ""https://example.com/webhooks/inbound-sms"", ""messagesCallbackType"": ""app"", ""messagesCallbackValue"": ""aaaaaaaa-bbbb-cccc-dddd-0123456789ab"", ""voiceCallbackType"": ""app"", ""voiceCallbackValue"": ""aaaaaaaa-bbbb-cccc-dddd-0123456789ab""}]}";
var expectedUri =
$"{this.RestUrl}/account/numbers?country=GB&api_key={this.ApiKey}&api_secret={this.ApiSecret}&";
$"{this.RestUrl}/account/numbers?country=GB&";
var request = new NumberSearchRequest {Country = "GB"};
this.Setup(expectedUri, expectedResponse);
var response = await this.client.NumbersClient.GetOwnedNumbersAsync(request);
Expand All @@ -212,7 +212,7 @@ public async Task GetOwnedNumbersAsyncWithCredentials()
const string expectedResponse =
@"{""count"": 1234,""numbers"": [{""country"": ""GB"",""msisdn"": ""447700900000"",""type"": ""mobile-lvn"",""cost"": ""1.25"",""features"": [""VOICE"",""SMS""],""app_id"": ""9907a0d2-5206-4ec0-af8c-b335685ef9b8""}]}";
var expectedUri =
$"{this.RestUrl}/account/numbers?country=GB&type=mobile-lvn&pattern=12345&search_pattern=1&features=SMS&size=10&index=1&application_id=testApp&api_key={this.ApiKey}&api_secret={this.ApiSecret}&";
$"{this.RestUrl}/account/numbers?country=GB&type=mobile-lvn&pattern=12345&search_pattern=1&features=SMS&size=10&index=1&application_id=testApp&";
var request = new NumberSearchRequest
{
Country = "GB", Type = "mobile-lvn", Pattern = "12345", SearchPattern = SearchPattern.Contains,
Expand Down
30 changes: 16 additions & 14 deletions Vonage/Numbers/NumbersClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#region
using System.Net.Http;
using System.Threading.Tasks;
using Vonage.Common;
using Vonage.Request;
#endregion

namespace Vonage.Numbers;

Expand All @@ -11,7 +13,7 @@ public class NumbersClient : INumbersClient
private const string SuccessStatusCode = "200";
private readonly Configuration configuration;
private readonly ITimeProvider timeProvider = new TimeProvider();

/// <summary>
/// Constructor for NumbersClients.
/// </summary>
Expand All @@ -21,19 +23,19 @@ public NumbersClient(Credentials credentials = null)
this.Credentials = credentials;
this.configuration = Configuration.Instance;
}

internal NumbersClient(Credentials credentials, Configuration configuration, ITimeProvider timeProvider)
{
this.Credentials = credentials;
this.configuration = configuration;
this.timeProvider = timeProvider;
}

/// <summary>
/// Gets or sets credentials to be used in further requests.
/// </summary>
public Credentials Credentials { get; set; }

/// <inheritdoc />
public async Task<NumberTransactionResponse> BuyANumberAsync(NumberTransactionRequest request,
Credentials creds = null)
Expand All @@ -48,7 +50,7 @@ public async Task<NumberTransactionResponse> BuyANumberAsync(NumberTransactionRe
ValidateNumbersResponse(response);
return response;
}

/// <inheritdoc />
public async Task<NumberTransactionResponse> CancelANumberAsync(NumberTransactionRequest request,
Credentials creds = null)
Expand All @@ -63,27 +65,27 @@ public async Task<NumberTransactionResponse> CancelANumberAsync(NumberTransactio
ValidateNumbersResponse(response);
return response;
}

/// <inheritdoc />
public Task<NumbersSearchResponse> GetAvailableNumbersAsync(NumberSearchRequest request,
Credentials creds = null) =>
ApiRequest.Build(this.GetCredentials(creds), this.configuration, this.timeProvider)
.DoGetRequestWithQueryParametersAsync<NumbersSearchResponse>(
ApiRequest.GetBaseUri(ApiRequest.UriType.Rest, this.configuration, "/number/search"),
AuthType.Query,
AuthType.Basic,
request
);

/// <inheritdoc />
public Task<NumbersSearchResponse>
GetOwnedNumbersAsync(NumberSearchRequest request, Credentials creds = null) =>
ApiRequest.Build(this.GetCredentials(creds), this.configuration, this.timeProvider)
.DoGetRequestWithQueryParametersAsync<NumbersSearchResponse>(
ApiRequest.GetBaseUri(ApiRequest.UriType.Rest, this.configuration, "/account/numbers"),
AuthType.Query,
AuthType.Basic,
request
);

/// <inheritdoc />
public Task<NumberTransferResponse> TransferANumberAsync(NumberTransferRequest request, string apiKey,
Credentials creds = null) =>
Expand All @@ -95,7 +97,7 @@ public Task<NumberTransferResponse> TransferANumberAsync(NumberTransferRequest r
request,
AuthType.Basic
);

/// <inheritdoc />
public async Task<NumberTransactionResponse> UpdateANumberAsync(UpdateNumberRequest request,
Credentials creds = null)
Expand All @@ -110,12 +112,12 @@ public async Task<NumberTransactionResponse> UpdateANumberAsync(UpdateNumberRequ
ValidateNumbersResponse(response);
return response;
}

private static string FormatQueryStringCredentials(Credentials credentials) =>
$"api_key={credentials.ApiKey}&api_secret={credentials.ApiSecret}";

private Credentials GetCredentials(Credentials overridenCredentials) => overridenCredentials ?? this.Credentials;

private static void ValidateNumbersResponse(NumberTransactionResponse response)
{
if (response.ErrorCode != SuccessStatusCode)
Expand Down

0 comments on commit 0d18216

Please # to comment.