Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/support service plan #47

Merged
merged 11 commits into from
Apr 3, 2024
27 changes: 27 additions & 0 deletions examples/Console/UseServicePlanIdForSms.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Sinch;
using Sinch.SMS;
using Sinch.SMS.Batches.Send;

namespace Examples
{
public class UseServicePlanIdForSms
{
public void Example()
{
var sinchClient = new SinchClient(default, default, default,
options =>
{
options.UseServicePlanIdWithSms(Environment.GetEnvironmentVariable("SINCH_SERVICE_PLAN_ID"),
Environment.GetEnvironmentVariable("SINCH_API_TOKEN"), SmsServicePlanIdHostingRegion.Ca);
});
sinchClient.Sms.Batches.Send(new SendTextBatchRequest()
{
Body = "Hello, World!",
To = new List<string>()
{
"+480000000"
}
});
}
}
}
27 changes: 27 additions & 0 deletions src/Sinch/Auth/BearerAuth.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Threading.Tasks;

namespace Sinch.Auth
{
public class BearerAuth : ISinchAuth
{
private readonly string _apiToken;

public BearerAuth(string apiToken)
{
if (string.IsNullOrEmpty(apiToken))
{
throw new ArgumentNullException(nameof(apiToken), "Should have a value");
}

_apiToken = apiToken;
}

public Task<string> GetAuthToken(bool force = false)
{
return Task.FromResult(_apiToken);
}

public string Scheme { get; } = AuthSchemes.Bearer;
}
}
28 changes: 14 additions & 14 deletions src/Sinch/SMS/Batches/Batches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ internal sealed class Batches : ISinchSmsBatches
{
private readonly Uri _baseAddress;
private readonly IHttp _http;
private readonly ILoggerAdapter<Batches> _logger;
private readonly string _projectId;
private readonly ILoggerAdapter<ISinchSmsBatches> _logger;
private readonly string _projectOrServicePlanId;

internal Batches(string projectId, Uri baseAddress, ILoggerAdapter<Batches> logger, IHttp http)
internal Batches(string projectOrServicePlanId, Uri baseAddress, ILoggerAdapter<ISinchSmsBatches> logger, IHttp http)
{
_projectId = projectId;
_projectOrServicePlanId = projectOrServicePlanId;
_http = http;
_baseAddress = baseAddress;
_logger = logger;
Expand All @@ -138,27 +138,27 @@ public Task<IBatch> Send(ISendBatchRequest request, CancellationToken cancellati
{
if (request is null) throw new ArgumentNullException(nameof(request));

var uri = new Uri(_baseAddress, $"xms/v1/{_projectId}/batches");
var uri = new Uri(_baseAddress, $"xms/v1/{_projectOrServicePlanId}/batches");
_logger?.LogDebug("Making a request to {uri}", uri);
return _http.Send<ISendBatchRequest, IBatch>(uri, HttpMethod.Post, request, cancellationToken);
}

public Task<ListBatchesResponse> List(ListBatchesRequest request, CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress, $"xms/v1/{_projectId}/batches?{request.GetQueryString()}");
var uri = new Uri(_baseAddress, $"xms/v1/{_projectOrServicePlanId}/batches?{request.GetQueryString()}");
_logger?.LogDebug("Listing batches...");
return _http.Send<ListBatchesResponse>(uri, HttpMethod.Get, cancellationToken);
}

public async IAsyncEnumerable<IBatch> ListAuto(ListBatchesRequest request,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
_logger?.LogDebug("Listing batches...");
_logger?.LogDebug("Auto Listing batches...");
bool lastPage;
do
{
var query = request.GetQueryString();
var relative = $"xms/v1/{_projectId}/batches";
var relative = $"xms/v1/{_projectOrServicePlanId}/batches";
if (!string.IsNullOrEmpty(query))
{
relative += "?" + query;
Expand All @@ -180,7 +180,7 @@ public async IAsyncEnumerable<IBatch> ListAuto(ListBatchesRequest request,

public Task<DryRunResponse> DryRun(DryRunRequest request, CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress, $"xms/v1/{_projectId}/batches/dry_run?{request.GetQueryString()}");
var uri = new Uri(_baseAddress, $"xms/v1/{_projectOrServicePlanId}/batches/dry_run?{request.GetQueryString()}");
_logger?.LogDebug("Performing dry run...");
return _http.Send<ISendBatchRequest, DryRunResponse>(uri, HttpMethod.Post, request.BatchRequest,
cancellationToken);
Expand All @@ -191,38 +191,38 @@ public Task<IBatch> Get(string batchId, CancellationToken cancellationToken = de
if (string.IsNullOrEmpty(batchId))
throw new ArgumentNullException(nameof(batchId), "BatchId could not be empty");

var uri = new Uri(_baseAddress, $"xms/v1/{_projectId}/batches/{batchId}");
var uri = new Uri(_baseAddress, $"xms/v1/{_projectOrServicePlanId}/batches/{batchId}");
_logger?.LogDebug("Getting batch...");
return _http.Send<IBatch>(uri, HttpMethod.Get, cancellationToken);
}

public Task<IBatch> Update(string batchId, IUpdateBatchRequest request,
CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress, $"xms/v1/{_projectId}/batches/{batchId}");
var uri = new Uri(_baseAddress, $"xms/v1/{_projectOrServicePlanId}/batches/{batchId}");
_logger?.LogDebug("Updating a batch with {id}...", batchId);
return _http.Send<IUpdateBatchRequest, IBatch>(uri, HttpMethod.Post, request, cancellationToken);
}

public Task<IBatch> Replace(string batchId, ISendBatchRequest batch,
CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress, $"xms/v1/{_projectId}/batches/{batchId}");
var uri = new Uri(_baseAddress, $"xms/v1/{_projectOrServicePlanId}/batches/{batchId}");
_logger?.LogDebug("Replacing a batch with {id}...", batchId);
return _http.Send<ISendBatchRequest, IBatch>(uri, HttpMethod.Put, batch, cancellationToken);
}

public Task<IBatch> Cancel(string batchId, CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress, $"xms/v1/{_projectId}/batches/{batchId}");
var uri = new Uri(_baseAddress, $"xms/v1/{_projectOrServicePlanId}/batches/{batchId}");
_logger?.LogDebug("Cancelling batch with {id}...", batchId);
return _http.Send<IBatch>(uri, HttpMethod.Delete, cancellationToken);
}

public Task SendDeliveryFeedback(string batchId, IEnumerable<string> recipients,
CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress, $"xms/v1/{_projectId}/batches/{batchId}/delivery_feedback");
var uri = new Uri(_baseAddress, $"xms/v1/{_projectOrServicePlanId}/batches/{batchId}/delivery_feedback");
_logger?.LogDebug("Sending delivery feedback for batch {id}...", batchId);
return _http.Send<object, object>(uri, HttpMethod.Post, new
{
Expand Down
20 changes: 10 additions & 10 deletions src/Sinch/SMS/DeliveryReports/DeliveryReports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ internal class DeliveryReports : ISinchSmsDeliveryReports
{
private readonly Uri _baseAddress;
private readonly IHttp _http;
private readonly ILoggerAdapter<DeliveryReports> _logger;
private readonly string _projectId;
private readonly ILoggerAdapter<ISinchSmsDeliveryReports> _logger;
private readonly string _projectOrServicePlanId;

internal DeliveryReports(string projectId, Uri baseAddress, ILoggerAdapter<DeliveryReports> logger, IHttp http)
internal DeliveryReports(string projectOrServicePlanId, Uri baseAddress, ILoggerAdapter<ISinchSmsDeliveryReports> logger, IHttp http)
{
_projectId = projectId;
_projectOrServicePlanId = projectOrServicePlanId;
_baseAddress = baseAddress;
_logger = logger;
_http = http;
Expand All @@ -73,7 +73,7 @@ internal DeliveryReports(string projectId, Uri baseAddress, ILoggerAdapter<Deliv
public Task<GetDeliveryReportResponse> Get(GetDeliveryReportRequest request, CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress,
$"xms/v1/{_projectId}/batches/{request.BatchId}/delivery_report?{request.GetQueryString()}");
$"xms/v1/{_projectOrServicePlanId}/batches/{request.BatchId}/delivery_report?{request.GetQueryString()}");
_logger?.LogDebug("Fetching delivery report for a batch with {id}", request.BatchId);

return _http.Send<GetDeliveryReportRequest, GetDeliveryReportResponse>(uri, HttpMethod.Get, null, cancellationToken)!;
Expand All @@ -84,7 +84,7 @@ public Task<DeliveryReport> GetForNumber(string batchId, string recipientMsisdn,
CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress,
$"xms/v1/{_projectId}/batches/{batchId}/delivery_report/{recipientMsisdn}");
$"xms/v1/{_projectOrServicePlanId}/batches/{batchId}/delivery_report/{recipientMsisdn}");

_logger?.LogDebug("Fetching delivery report for a {number} of a {batchId}", recipientMsisdn, batchId);

Expand All @@ -95,9 +95,9 @@ public Task<DeliveryReport> GetForNumber(string batchId, string recipientMsisdn,
public Task<ListDeliveryReportsResponse> List(ListDeliveryReportsRequest request, CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress,
$"xms/v1/{_projectId}/delivery_reports?{request.GetQueryString()}");
$"xms/v1/{_projectOrServicePlanId}/delivery_reports?{request.GetQueryString()}");

_logger?.LogDebug("Listing delivery reports for {projectId}", _projectId);
_logger?.LogDebug("Listing delivery reports for {projectOrServicePlanId}", _projectOrServicePlanId);

return _http.Send<object, ListDeliveryReportsResponse>(uri, HttpMethod.Get, null, cancellationToken);
}
Expand All @@ -106,12 +106,12 @@ public Task<ListDeliveryReportsResponse> List(ListDeliveryReportsRequest request
public async IAsyncEnumerable<DeliveryReport> ListAuto(ListDeliveryReportsRequest request,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
_logger?.LogDebug("Listing delivery reports for {projectId}", _projectId);
_logger?.LogDebug("Listing delivery reports for {projectOrServicePlanId}", _projectOrServicePlanId);
bool isLastPage;
do
{
var uri = new Uri(_baseAddress,
$"xms/v1/{_projectId}/delivery_reports?{request.GetQueryString()}");
$"xms/v1/{_projectOrServicePlanId}/delivery_reports?{request.GetQueryString()}");


var response = await _http.Send<ListDeliveryReportsResponse>(uri, HttpMethod.Get, cancellationToken);
Expand Down
24 changes: 12 additions & 12 deletions src/Sinch/SMS/Groups/Groups.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ internal sealed class Groups : ISinchSmsGroups
{
private readonly Uri _baseAddress;
private readonly IHttp _http;
private readonly ILoggerAdapter<Groups> _logger;
private readonly string _projectId;
private readonly ILoggerAdapter<ISinchSmsGroups> _logger;
private readonly string _projectOrServicePlanId;

internal Groups(string projectId, Uri baseAddress, ILoggerAdapter<Groups> logger, IHttp http)
internal Groups(string projectOrServicePlanId, Uri baseAddress, ILoggerAdapter<ISinchSmsGroups> logger, IHttp http)
{
_projectId = projectId;
_projectOrServicePlanId = projectOrServicePlanId;
_baseAddress = baseAddress;
_logger = logger;
_http = http;
Expand All @@ -118,7 +118,7 @@ internal Groups(string projectId, Uri baseAddress, ILoggerAdapter<Groups> logger
/// <inheritdoc />
public Task<ListGroupsResponse> List(ListGroupsRequest request, CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress, $"xms/v1/{_projectId}/groups?{request.GetQueryString()}");
var uri = new Uri(_baseAddress, $"xms/v1/{_projectOrServicePlanId}/groups?{request.GetQueryString()}");
_logger?.LogDebug("Listing groups...");
return _http.Send<ListGroupsResponse>(uri, HttpMethod.Get, cancellationToken);
}
Expand All @@ -131,7 +131,7 @@ public async IAsyncEnumerable<Group> ListAuto(ListGroupsRequest request,
bool isLastPage;
do
{
var uri = new Uri(_baseAddress, $"xms/v1/{_projectId}/groups?{request.GetQueryString()}");
var uri = new Uri(_baseAddress, $"xms/v1/{_projectOrServicePlanId}/groups?{request.GetQueryString()}");
_logger?.LogDebug("Listing group {page}", request.Page);
var response = await _http.Send<ListGroupsResponse>(uri, HttpMethod.Get, cancellationToken);
foreach (var group in response.Groups)
Expand All @@ -147,23 +147,23 @@ public async IAsyncEnumerable<Group> ListAuto(ListGroupsRequest request,
/// <inheritdoc />
public Task<Group> Get(string groupId, CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress, $"xms/v1/{_projectId}/groups/{groupId}");
var uri = new Uri(_baseAddress, $"xms/v1/{_projectOrServicePlanId}/groups/{groupId}");
_logger?.LogDebug("Fetching a group with {groupId}", groupId);
return _http.Send<Group>(uri, HttpMethod.Get, cancellationToken);
}

/// <inheritdoc />
public Task<Group> Create(CreateGroupRequest request, CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress, $"xms/v1/{_projectId}/groups");
var uri = new Uri(_baseAddress, $"xms/v1/{_projectOrServicePlanId}/groups");
_logger?.LogDebug("Creating a group...");
return _http.Send<CreateGroupRequest, Group>(uri, HttpMethod.Post, request, cancellationToken)!;
}

/// <inheritdoc />
public Task<Group> Update(UpdateGroupRequest request, CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress, $"xms/v1/{_projectId}/groups/{request.GroupId}");
var uri = new Uri(_baseAddress, $"xms/v1/{_projectOrServicePlanId}/groups/{request.GroupId}");
_logger?.LogDebug("Updating a group with {id}...", request.GroupId);
// No simple way to conditionally remove name property from request object. Easy to go with anonymous objects
if (request.Name == string.Empty)
Expand All @@ -182,7 +182,7 @@ public Task<Group> Update(UpdateGroupRequest request, CancellationToken cancella
/// <inheritdoc />
public Task<Group> Replace(ReplaceGroupRequest request, CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress, $"xms/v1/{_projectId}/groups/{request.GroupId}");
var uri = new Uri(_baseAddress, $"xms/v1/{_projectOrServicePlanId}/groups/{request.GroupId}");
_logger?.LogDebug("Replacing a group with {id}...", request.GroupId);
var requestInner = new RequestInner
{
Expand All @@ -195,15 +195,15 @@ public Task<Group> Replace(ReplaceGroupRequest request, CancellationToken cancel
/// <inheritdoc />
public Task Delete(string groupId, CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress, $"xms/v1/{_projectId}/groups/{groupId}");
var uri = new Uri(_baseAddress, $"xms/v1/{_projectOrServicePlanId}/groups/{groupId}");
_logger?.LogDebug("Deleting a group with {id}...", groupId);
return _http.Send<object, Group>(uri, HttpMethod.Delete, null, cancellationToken);
}

/// <inheritdoc />
public Task<IEnumerable<string>> ListMembers(string groupId, CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress, $"xms/v1/{_projectId}/groups/{groupId}/members");
var uri = new Uri(_baseAddress, $"xms/v1/{_projectOrServicePlanId}/groups/{groupId}/members");
_logger?.LogDebug("Listing members of a group with {id}...", groupId);
return _http.Send<object, IEnumerable<string>>(uri, HttpMethod.Get, null, cancellationToken)!;
}
Expand Down
14 changes: 7 additions & 7 deletions src/Sinch/SMS/Inbounds/Inbounds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ public class Inbounds : ISinchSmsInbounds
{
private readonly Uri _baseAddress;
private readonly IHttp _http;
private readonly ILoggerAdapter<Inbounds> _logger;
private readonly string _projectId;
private readonly ILoggerAdapter<ISinchSmsInbounds> _logger;
private readonly string _projectOrServicePlanId;

internal Inbounds(string projectId, Uri baseAddress, ILoggerAdapter<Inbounds> logger, IHttp http)
internal Inbounds(string projectOrServicePlanId, Uri baseAddress, ILoggerAdapter<ISinchSmsInbounds> logger, IHttp http)
{
_projectId = projectId;
_projectOrServicePlanId = projectOrServicePlanId;
_baseAddress = baseAddress;
_logger = logger;
_http = http;
}

public Task<ListInboundsResponse> List(ListInboundsRequest request, CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress, $"xms/v1/{_projectId}/inbounds?{request.GetQueryString()}");
var uri = new Uri(_baseAddress, $"xms/v1/{_projectOrServicePlanId}/inbounds?{request.GetQueryString()}");
_logger?.LogDebug("Listing inbounds...");
return _http.Send<ListInboundsResponse>(uri, HttpMethod.Get, cancellationToken);
}
Expand All @@ -68,7 +68,7 @@ public async IAsyncEnumerable<Inbound> ListAuto(ListInboundsRequest request, [En
bool isLastPage;
do
{
var uri = new Uri(_baseAddress, $"xms/v1/{_projectId}/inbounds?{request.GetQueryString()}");
var uri = new Uri(_baseAddress, $"xms/v1/{_projectOrServicePlanId}/inbounds?{request.GetQueryString()}");
_logger?.LogDebug("Auto list {page}", request.Page);
var response = await _http.Send<ListInboundsResponse>(uri, HttpMethod.Get, cancellationToken);
foreach (var inbound in response.Inbounds)
Expand All @@ -84,7 +84,7 @@ public async IAsyncEnumerable<Inbound> ListAuto(ListInboundsRequest request, [En

public Task<Inbound> Get(string inboundId, CancellationToken cancellationToken = default)
{
var uri = new Uri(_baseAddress, $"xms/v1/{_projectId}/inbounds/{inboundId}");
var uri = new Uri(_baseAddress, $"xms/v1/{_projectOrServicePlanId}/inbounds/{inboundId}");
_logger?.LogDebug("Getting inbound with {id}", inboundId);
return _http.Send<Inbound>(uri, HttpMethod.Get, cancellationToken);
}
Expand Down
Loading
Loading