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

chore: add linking in README.md #54

Merged
merged 7 commits into from
Apr 17, 2024
Merged
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
80 changes: 7 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,15 @@ Once the SDK is installed, you must start by initializing the main client class.

To initialize communication with the Sinch servers, credentials obtained from the Sinch dashboard must be provided to the main client class of this SDK. It's highly recommended to not hardcode these credentials and to load them from environment variables instead or any key-secret storage (for example, [app-secrets](https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-7.0)).

```csharp
using Sinch;

var sinch = new SinchClient(configuration["Sinch:KeyId"], configuration["Sinch:KeySecret"], configuration["Sinch:ProjectId"]);
```
https://github.com/sinch/sinch-sdk-dotnet/blob/9f69eb2c5da48d5678d0f28ec4c039dd816f36d7/examples/Console/Program.cs#L8-L10

With ASP.NET dependency injection:

```csharp
// SinchClient is thread safe so it's okay to add it as a singleton
builder.Services.AddSingleton<ISinch>(x => new SinchClient(
builder.Configuration["Sinch:KeyId"],
builder.Configuration["Sinch:KeySecret"],
builder.Configuration["Sinch:ProjectId"]));
```
https://github.com/sinch/sinch-sdk-dotnet/blob/9f69eb2c5da48d5678d0f28ec4c039dd816f36d7/examples/WebApi/Program.cs#L17-L25

To configure Conversation or Sms hosting regions, and any other additional parameters, use [`SinchOptions`](https://github.com/sinch/sinch-sdk-dotnet/blob/main/src/Sinch/SinchOptions.cs):

```csharp
var sinch = new SinchClient(
configuration["Sinch:KeyId"],
configuration["Sinch:KeySecret"],
configuration["Sinch:ProjectId"],
options =>
{
options.SmsHostingRegion = Sinch.SMS.SmsHostingRegion.Eu;
options.ConversationRegion = Sinch.Conversation.ConversationRegion.Eu;
});
```
https://github.com/sinch/sinch-sdk-dotnet/blob/4ca70fc3df975f213c822a66a0e6775d3ddee23d/examples/Console/UsingSinchOptions.cs#L9-L16

## Supported Sinch Products

Expand All @@ -80,66 +60,20 @@ Sinch client provides access to the following Sinch products:
- additional products coming soon!

Usage example of the `numbers` product, assuming `sinch` is a type of `ISinchClient`:
```csharp
using Sinch.Numbers.Active.List;

ListActiveNumbersResponse response = await sinch.Numbers.Active.List(new ListActiveNumbersRequest
{
RegionCode = "US",
Type = Types.Mobile
});
```

https://github.com/sinch/sinch-sdk-dotnet/blob/63752849bb3277b464754f8ad9544cfae2d51d35/examples/Console/ListActiveNumbers.cs#L15-L19

## Logging, HttpClient, and additional options

To configure a logger, provide your own `HttpClient`, or any additional options utilize `SinchOptions` action within the constructor:

```csharp
using Sinch;
using Sinch.SMS;

var sinch = new SinchClient(
configuration["Sinch:KeyId"],
configuration["Sinch:KeySecret"],
configuration["Sinch:ProjectId"],
options =>
{
// provide any logger factory which satisfies Microsoft.Extensions.Logging.ILoggerFactory
options.LoggerFactory = LoggerFactory.Create(config => {
// add log output to console
config.AddConsole();
});
// Provide your http client here
options.HttpClient = new HttpClient();
// Set a hosting region for Sms
options.SmsHostingRegion = SmsHostingRegion.Eu;
});
```
https://github.com/sinch/sinch-sdk-dotnet/blob/9f69eb2c5da48d5678d0f28ec4c039dd816f36d7/examples/WebApi/Program.cs#L17-L25

## Handling exceptions

For an unsuccessful API calls `SinchApiException` will be thrown:

```csharp
using Sinch;
using Sinch.SMS.Batches.Send;

try {
var batch = await sinch.Sms.Batches.Send(new SendBatchRequest
{
Body = "Hello, World!",
DeliveryReport = DeliveryReport.None,
To = new List<string>()
{
123456789
}
});
}
catch(SinchApiException e)
{
logger.LogError("Api Exception. Status: {status}. Detailed message: {message}", e.Status, e.DetailedMessage);
}
```
https://github.com/sinch/sinch-sdk-dotnet/blob/3ad0f84dc19968d1d708fab59fd84e134c76f066/examples/Console/HandlingExceptions.cs#L19-L35

## Sample apps

Expand Down
38 changes: 38 additions & 0 deletions examples/Console/HandlingExceptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.Extensions.Logging;
using Sinch;
using Sinch.SMS;
using Sinch.SMS.Batches.Send;

namespace Examples
{
public class HandlingExceptions
{
public async Task Example()
{
// for the sake of example, no real logger is created.
var logger = LoggerFactory.Create(x => { }).CreateLogger("example");
var sinch = new SinchClient(Environment.GetEnvironmentVariable("SINCH_PROJECT_ID")!,
Environment.GetEnvironmentVariable("SINCH_KEY_ID")!,
Environment.GetEnvironmentVariable("SINCH_KEY_SECRET")!
);

try
{
var batch = await sinch.Sms.Batches.Send(new SendTextBatchRequest()
{
Body = "Hello, World!",
DeliveryReport = DeliveryReport.None,
To = new List<string>()
{
"+48000000"
}
});
}
catch (SinchApiException e)
{
logger.LogError("Api Exception. Status: {status}. Detailed message: {message}", e.Status,
e.DetailedMessage);
}
}
}
}
21 changes: 21 additions & 0 deletions examples/Console/ListActiveNumbers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Sinch;
using Sinch.Numbers;
using Sinch.Numbers.Active.List;

namespace Examples;

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")!
);
ListActiveNumbersResponse response = await sinch.Numbers.Active.List(new ListActiveNumbersRequest
{
RegionCode = "US",
Type = Types.Mobile
});
}
}
7 changes: 4 additions & 3 deletions examples/Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
// Assume .env file is present in your output directory
Env.Load();

var sinch = new SinchClient(Environment.GetEnvironmentVariable("SINCH_KEY_ID")!,
Environment.GetEnvironmentVariable("SINCH_KEY_SECRET")!,
Environment.GetEnvironmentVariable("SINCH_PROJECT_ID")!);
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")!);
Expand Down
18 changes: 18 additions & 0 deletions examples/Console/UsingSinchOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Sinch;

namespace Examples;

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 =>
{
options.SmsHostingRegion = Sinch.SMS.SmsHostingRegion.Eu;
options.ConversationRegion = Sinch.Conversation.ConversationRegion.Eu;
});
}
}
Loading