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

Refactor/sinch client entrypoint v2 #102

Open
wants to merge 17 commits into
base: v2.0-next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion examples/Console/Fax/DownloadFax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ public class DownloadFax
{
public static async Task Example()
{
var sinchClient = new SinchClient("PROJECT_ID", "KEY_ID", "KEY_SECRET");
var sinchClient = new SinchClient(new SinchClientConfiguration()
{
SinchUnifiedCredentials = new SinchUnifiedCredentials()
{
ProjectId = "PROJECT_ID",
KeyId = "KEY_ID",
KeySecret = "KEY_SECRET"
}
});
const string faxId = "FAX_ID";

await using var contentResult = await sinchClient.Fax.Faxes.DownloadContent("faxId");
Expand Down
13 changes: 9 additions & 4 deletions examples/Console/HandlingExceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ public async Task Example()
{
// for the sake of example, no real logger is created.
var logger = LoggerFactory.Create(_ => { }).CreateLogger("example");
var sinch = new SinchClient(Environment.GetEnvironmentVariable("SINCH_PROJECT_ID")!,
Environment.GetEnvironmentVariable("SINCH_KEY_ID")!,
Environment.GetEnvironmentVariable("SINCH_KEY_SECRET")!
);
var sinch = new SinchClient(new SinchClientConfiguration()
{
SinchUnifiedCredentials = new SinchUnifiedCredentials()
{
ProjectId = Environment.GetEnvironmentVariable("SINCH_PROJECT_ID")!,
KeyId = Environment.GetEnvironmentVariable("SINCH_KEY_ID")!,
KeySecret = Environment.GetEnvironmentVariable("SINCH_KEY_SECRET")!
}
});

try
{
Expand Down
13 changes: 9 additions & 4 deletions examples/Console/ListActiveNumbers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ public class ListActiveNumbers
{
public async Task Example()
{
var sinch = new SinchClient(Environment.GetEnvironmentVariable("SINCH_PROJECT_ID")!,
Environment.GetEnvironmentVariable("SINCH_KEY_ID")!,
Environment.GetEnvironmentVariable("SINCH_KEY_SECRET")!
);
var sinch = new SinchClient(new SinchClientConfiguration()
{
SinchUnifiedCredentials = new SinchUnifiedCredentials()
{
ProjectId = Environment.GetEnvironmentVariable("SINCH_PROJECT_ID")!,
KeyId = Environment.GetEnvironmentVariable("SINCH_KEY_ID")!,
KeySecret = Environment.GetEnvironmentVariable("SINCH_KEY_SECRET")!
}
});
ListActiveNumbersResponse response = await sinch.Numbers.List(new ListActiveNumbersRequest
{
RegionCode = "US",
Expand Down
20 changes: 15 additions & 5 deletions examples/Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
using DotNetEnv;
using Sinch;
using Sinch.Verification;

// Assume .env file is present in your output directory
Env.Load();


var sinch = new SinchClient(Environment.GetEnvironmentVariable("SINCH_PROJECT_ID")!,
Environment.GetEnvironmentVariable("SINCH_KEY_ID")!,
Environment.GetEnvironmentVariable("SINCH_KEY_SECRET")!);
_ = sinch.Verification(Environment.GetEnvironmentVariable("SINCH_APP_KEY")!,
Environment.GetEnvironmentVariable("SINCH_APP_SECRET")!);
var sinch = new SinchClient(new SinchClientConfiguration()
{
SinchUnifiedCredentials = new SinchUnifiedCredentials()
{
ProjectId = Environment.GetEnvironmentVariable("SINCH_PROJECT_ID")!,
KeyId = Environment.GetEnvironmentVariable("SINCH_KEY_ID")!,
KeySecret = Environment.GetEnvironmentVariable("SINCH_KEY_SECRET")!
},
VerificationConfiguration = new SinchVerificationConfiguration()
{
AppKey = Environment.GetEnvironmentVariable("SINCH_APP_KEY")!,
AppSecret = Environment.GetEnvironmentVariable("SINCH_APP_KEY_SECRET")!
}
});

Console.ReadLine();
10 changes: 9 additions & 1 deletion examples/Console/RentAndConfigureNumbers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ public class RentAndConfigureNumbers
{
public static async Task Example()
{
var sinchClient = new SinchClient("PROJECT_ID", "KEY_ID", "KEY_SECRET");
var sinchClient = new SinchClient(new SinchClientConfiguration()
{
SinchUnifiedCredentials = new SinchUnifiedCredentials()
{
ProjectId = "PROJECT_ID",
KeyId = "KEY_ID",
KeySecret = "KEY_SECRET"
}
});
var response = await sinchClient.Numbers.Rent("+4811111111", new RentActiveNumberRequest()
{
SmsConfiguration = new SmsConfiguration
Expand Down
35 changes: 31 additions & 4 deletions examples/Console/UseOnlyVoiceOrVerification.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
using Sinch;
using Sinch.Verification;
using Sinch.Verification.Common;
using Sinch.Verification.Start.Request;
using Sinch.Voice;

namespace Examples
{
public class UseOnlyVoiceOrVerification
{
/// <summary>
/// If you want to use only voice and/or verification api, you can init sinch client without providing
/// necessary credentials. But be aware that when you try to use other services which depends on the common credentials you will get an exception.
/// unified credentials. But be aware that when you try to use other services which depends on the common credentials you will get an exception.
/// </summary>
public void Example()
public async Task Example()
{
var sinchVoiceClient = new SinchClient(null, null, null).Voice("appKey", "appSecret");
var sinchVerificationClient = new SinchClient(null, null, null).Verification("appKey", "appSecret");
var sinch = new SinchClient(new SinchClientConfiguration()
{
VerificationConfiguration = new SinchVerificationConfiguration()
{
AppKey = "verificationAppKey",
AppSecret = "verificationAppSecret",
},
VoiceConfiguration = new SinchVoiceConfiguration()
{
AppKey = "voiceAppKey",
AppSecret = "voiceAppSecret",
}
});
// example of voice use
var voiceClient = sinch.Voice;
var call = await voiceClient.Calls.Get("1");
// do smth with call response

// example of verification use
var verificationClient = sinch.Verification;
var verificationStart = await verificationClient.Verification.StartSms(new StartSmsVerificationRequest()
{
Identity = Identity.Number("+481123123123")
});
// do smth with verification start response
}
}
}
13 changes: 7 additions & 6 deletions examples/Console/UseServicePlanIdForSms.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Sinch;
using Sinch.Numbers;
using Sinch.SMS;
using Sinch.SMS.Batches.Send;

Expand All @@ -8,12 +9,12 @@ 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")!, SmsServicePlanIdRegion.Ca);
});
var sinchClient = new SinchClient(new SinchClientConfiguration()
{
SmsConfiguration = SinchSmsConfiguration.WithServicePlanId(
Environment.GetEnvironmentVariable("SINCH_SERVICE_PLAN_ID")!,
Environment.GetEnvironmentVariable("SINCH_API_TOKEN")!, SmsServicePlanIdRegion.Ca)
});
sinchClient.Sms.Batches.Send(new SendTextBatchRequest()
{
Body = "Hello, World!",
Expand Down
19 changes: 12 additions & 7 deletions examples/Console/UsingSinchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ public class UsingSinchOptions
{
public void Example()
{
var sinch = new SinchClient(Environment.GetEnvironmentVariable("SINCH_PROJECT_ID")!,
Environment.GetEnvironmentVariable("SINCH_KEY_ID")!,
Environment.GetEnvironmentVariable("SINCH_KEY_SECRET")!,
options =>
var sinch = new SinchClient(new SinchClientConfiguration()
{
SinchUnifiedCredentials = new SinchUnifiedCredentials()
{
options.SmsRegion = Sinch.SMS.SmsRegion.Eu;
options.ConversationRegion = Sinch.Conversation.ConversationRegion.Eu;
});
ProjectId = Environment.GetEnvironmentVariable("SINCH_PROJECT_ID")!,
KeyId = Environment.GetEnvironmentVariable("SINCH_KEY_ID")!,
KeySecret = Environment.GetEnvironmentVariable("SINCH_KEY_SECRET")!
},
SinchOptions = new SinchOptions()
{
HttpClient = new HttpClient()
}
});
}
}
19 changes: 12 additions & 7 deletions examples/WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@
builder.Services.AddSwaggerGen();



builder.Services.AddSingleton<ISinchClient>(_ => new SinchClient(
builder.Configuration["Sinch:ProjectId"],
builder.Configuration["Sinch:KeyId"]!,
builder.Configuration["Sinch:KeySecret"]!,
options =>
new SinchClientConfiguration()
{
options.LoggerFactory = LoggerFactory.Create(config => { config.AddConsole(); });
options.HttpClient = new HttpClient();
SinchUnifiedCredentials = new SinchUnifiedCredentials()
{
KeySecret = builder.Configuration["Sinch:KeySecret"]!,
KeyId = builder.Configuration["Sinch:KeyId"]!,
ProjectId = builder.Configuration["Sinch:ProjectId"]!,
},
SinchOptions = new SinchOptions()
{
LoggerFactory = LoggerFactory.Create(config => { config.AddConsole(); }),
HttpClient = new HttpClient()
}
}));

var app = builder.Build();
Expand Down
12 changes: 11 additions & 1 deletion src/Sinch/Auth/OAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@

namespace Sinch.Auth
{
public sealed class SinchOAuthConfiguration
{
public string? UrlOverride { get; init; }

internal Uri ResolveUrl()
{
if (!string.IsNullOrEmpty(UrlOverride)) return new Uri(UrlOverride);
return new Uri("https://auth.sinch.com");
}
}

internal sealed class OAuth : ISinchAuth
{
private readonly HttpClient _httpClient;
Expand Down Expand Up @@ -85,7 +96,6 @@ private sealed class AuthResponse
public required string AccessToken { get; set; }



/// <summary>
/// In seconds
/// </summary>
Expand Down
14 changes: 7 additions & 7 deletions src/Sinch/Conversation/Apps/Apps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ public interface ISinchConversationApps
internal sealed class Apps : ISinchConversationApps
{
private readonly Uri _baseAddress;
private readonly IHttp _http;
private readonly Lazy<IHttp> _http;
private readonly ILoggerAdapter<Apps>? _logger;
private readonly string _projectId;

public Apps(string projectId, Uri baseAddress, ILoggerAdapter<Apps>? logger, IHttp http)
public Apps(string projectId, Uri baseAddress, ILoggerAdapter<Apps>? logger, Lazy<IHttp> http)
{
_projectId = projectId;
_baseAddress = baseAddress;
Expand All @@ -100,7 +100,7 @@ public Task<App> Create(CreateAppRequest request, CancellationToken cancellation
{
var uri = new Uri(_baseAddress, $"v1/projects/{_projectId}/apps");
_logger?.LogDebug("Creating an app...");
return _http.Send<CreateAppRequest, App>(uri, HttpMethod.Post, request,
return _http.Value.Send<CreateAppRequest, App>(uri, HttpMethod.Post, request,
cancellationToken: cancellationToken);
}

Expand All @@ -110,7 +110,7 @@ public async Task<IEnumerable<App>> List(CancellationToken cancellationToken = d
var uri = new Uri(_baseAddress, $"v1/projects/{_projectId}/apps");
_logger?.LogDebug("Listing apps for a {projectId}", _projectId);
// flatten the response
var response = await _http.Send<ListResponse>(uri, HttpMethod.Get, cancellationToken: cancellationToken);
var response = await _http.Value.Send<ListResponse>(uri, HttpMethod.Get, cancellationToken: cancellationToken);
return response.Apps ?? new List<App>();
}

Expand All @@ -126,7 +126,7 @@ public Task<App> Get(string appId, CancellationToken cancellationToken = default

var uri = new Uri(_baseAddress, $"v1/projects/{_projectId}/apps/{appId}");
_logger?.LogDebug("Getting an app for a {projectId} with {appId}", _projectId, appId);
return _http.Send<App>(uri, HttpMethod.Get, cancellationToken: cancellationToken);
return _http.Value.Send<App>(uri, HttpMethod.Get, cancellationToken: cancellationToken);
}

/// <inheritdoc />
Expand All @@ -141,7 +141,7 @@ public Task Delete(string appId, CancellationToken cancellationToken = default)

var uri = new Uri(_baseAddress, $"v1/projects/{_projectId}/apps/{appId}");
_logger?.LogDebug("Deleting an app for a {projectId} with {appId}", _projectId, appId);
return _http.Send<EmptyResponse>(uri, HttpMethod.Delete, cancellationToken: cancellationToken);
return _http.Value.Send<EmptyResponse>(uri, HttpMethod.Delete, cancellationToken: cancellationToken);
}

/// <inheritdoc />
Expand All @@ -161,7 +161,7 @@ public Task<App> Update(string appId, UpdateAppRequest request, CancellationToke

var uri = new Uri(_baseAddress, $"v1/projects/{_projectId}/apps/{appId}{query}");
_logger?.LogDebug("Updating an app for a {projectId} with {appId}", _projectId, appId);
return _http.Send<UpdateAppRequest, App>(uri, HttpMethod.Patch, request,
return _http.Value.Send<UpdateAppRequest, App>(uri, HttpMethod.Patch, request,
cancellationToken: cancellationToken);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Sinch/Conversation/Capability/Capabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ Task<LookupCapabilityResponse> Lookup(LookupCapabilityRequest request,
internal sealed class Capabilities : ISinchConversationCapabilities
{
private readonly Uri _baseAddress;
private readonly IHttp _http;
private readonly Lazy<IHttp> _http;
private readonly ILoggerAdapter<ISinchConversationCapabilities>? _logger;
private readonly string _projectId;

public Capabilities(string projectId, Uri baseAddress, ILoggerAdapter<ISinchConversationCapabilities>? logger,
IHttp http)
Lazy<IHttp> http)
{
_projectId = projectId;
_baseAddress = baseAddress;
Expand All @@ -51,7 +51,7 @@ public Task<LookupCapabilityResponse> Lookup(LookupCapabilityRequest request,
{
var uri = new Uri(_baseAddress, $"v1/projects/{_projectId}/capability:query");
_logger?.LogDebug("Looking up for a capability...");
return _http.Send<LookupCapabilityRequest, LookupCapabilityResponse>(uri, HttpMethod.Post, request,
return _http.Value.Send<LookupCapabilityRequest, LookupCapabilityResponse>(uri, HttpMethod.Post, request,
cancellationToken);
}
}
Expand Down
Loading