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

[Communication] - SMS - Managed Identity Support for SMS Client #17867

Merged
merged 10 commits into from
Jan 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions sdk/communication/Azure.Communication.Sms/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 1.0.0-beta.4 (Unreleased)

###Added
- Support for creating PhoneNumberAdministrationClient with TokenCredential
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant sms here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch!! Thank you, I've fixed this


## 1.0.0-beta.3 (2020-11-16)

Expand Down
8 changes: 8 additions & 0 deletions sdk/communication/Azure.Communication.Sms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ string connectionString = "YOUR_CONNECTION_STRING"; // Find your Communication S
SmsClient client = new SmsClient(connectionString);
```

Alternatively, SMS clients can also be authenticated using a valid token.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we tell users to have their client id, tenant and client secret as env variables for DefaultAzureCredential to work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks for the suggestion Jorge!


```C# Snippet:Azure_Communication_Sms_Tests_Samples_CreateSmsClientWithToken
string endpoint = "<endpoint_url>";
TokenCredential tokenCredential = new DefaultAzureCredential();
SmsClient client = new SmsClient(new Uri(endpoint), tokenCredential);
```

## Examples
### Send a SMS Message
To send a SMS message, call the `Send` or `SendAsync` function from the `SmsClient`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public partial class SmsClient
{
protected SmsClient() { }
public SmsClient(string connectionString, Azure.Communication.Sms.SmsClientOptions? options = null) { }
public SmsClient(System.Uri endpoint, Azure.Core.TokenCredential tokenCredential, Azure.Communication.Sms.SmsClientOptions? options = null) { }
public virtual Azure.Response<Azure.Communication.Sms.SendSmsResponse> Send(Azure.Communication.PhoneNumberIdentifier from, Azure.Communication.PhoneNumberIdentifier to, string message, Azure.Communication.Sms.SendSmsOptions? sendSmsOptions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response<Azure.Communication.Sms.SendSmsResponse> Send(Azure.Communication.PhoneNumberIdentifier from, System.Collections.Generic.IEnumerable<Azure.Communication.PhoneNumberIdentifier> to, string message, Azure.Communication.Sms.SendSmsOptions? sendSmsOptions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Communication.Sms.SendSmsResponse>> SendAsync(Azure.Communication.PhoneNumberIdentifier from, Azure.Communication.PhoneNumberIdentifier to, string message, Azure.Communication.Sms.SendSmsOptions? sendSmsOptions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
Expand Down
23 changes: 23 additions & 0 deletions sdk/communication/Azure.Communication.Sms/src/SmsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ public SmsClient(string connectionString, SmsClientOptions? options = default)
ConnectionString.Parse(AssertNotNullOrEmpty(connectionString, nameof(connectionString))))
{ }

/// <summary> Initializes a new instance of <see cref="SmsClient"/>.</summary>
/// <param name="endpoint">The URI of the Azure Communication Services resource.</param>
/// <param name="tokenCredential">The TokenCredential used to authenticate requests, such as DefaultAzureCredential.</param>
/// <param name="options">Client option exposing <see cref="ClientOptions.Diagnostics"/>, <see cref="ClientOptions.Retry"/>, <see cref="ClientOptions.Transport"/>, etc.</param>
public SmsClient(Uri endpoint, TokenCredential tokenCredential, SmsClientOptions? options = default)
: this(
endpoint,
options ?? new SmsClientOptions(),
tokenCredential)
{ }

/// <summary>Initializes a new instance of <see cref="SmsClient"/> for mocking.</summary>
protected SmsClient()
{
Expand All @@ -50,6 +61,18 @@ private SmsClient(SmsClientOptions options, ConnectionString connectionString)
apiVersion: options.ApiVersion)
{ }

private SmsClient(Uri endpoint, SmsClientOptions options, TokenCredential tokenCredential)
{
Argument.AssertNotNull(endpoint, nameof(endpoint));
Argument.AssertNotNull(tokenCredential, nameof(tokenCredential));

_clientDiagnostics = new ClientDiagnostics(options);
RestClient = new SmsRestClient(
_clientDiagnostics,
options.BuildHttpPipeline(tokenCredential),
endpoint.AbsoluteUri);
}

/// <summary>
/// Sends a SMS <paramref name="from"/> a phone number that is acquired by the authenticated account, <paramref name="to"/> another phone number.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Azure.Core;
using Azure.Identity;

namespace Azure.Communication.Sms.Tests.samples
{
/// <summary>
Expand All @@ -16,5 +20,14 @@ public SmsClient CreateSmsClient()
#endregion Snippet:Azure_Communication_Sms_Tests_Samples_CreateSmsClient
return client;
}
public SmsClient CreateSmsClientWithToken()
{
#region Snippet:Azure_Communication_Sms_Tests_Samples_CreateSmsClientWithToken
string endpoint = "<endpoint_url>";
TokenCredential tokenCredential = new DefaultAzureCredential();
SmsClient client = new SmsClient(new Uri(endpoint), tokenCredential);
#endregion Snippet:Azure_Communication_Sms_Tests_Samples_CreateSmsClientWithToken
return client;
}
}
}