Exemplo n.º 1
0
        public async Task <Response <SendSmsResponse> > SendAsync(string @from, IEnumerable <string> to, string message, SendSmsOptions sendSmsOptions = null, CancellationToken cancellationToken = default)
        {
            if (@from == null)
            {
                throw new ArgumentNullException(nameof(@from));
            }
            if (to == null)
            {
                throw new ArgumentNullException(nameof(to));
            }
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            using var message0 = CreateSendRequest(@from, to, message, sendSmsOptions);
            await _pipeline.SendAsync(message0, cancellationToken).ConfigureAwait(false);

            switch (message0.Response.Status)
            {
            case 200:
            {
                SendSmsResponse value = default;
                using var document = await JsonDocument.ParseAsync(message0.Response.ContentStream, default, cancellationToken).ConfigureAwait(false);

                value = SendSmsResponse.DeserializeSendSmsResponse(document.RootElement);
                return(Response.FromValue(value, message0.Response));
            }
Exemplo n.º 2
0
        internal HttpMessage CreateSendRequest(string @from, IEnumerable <string> to, string message, SendSmsOptions sendSmsOptions)
        {
            var message0 = _pipeline.CreateMessage();
            var request  = message0.Request;

            request.Method = RequestMethod.Post;
            var uri = new RawRequestUriBuilder();

            uri.AppendRaw(endpoint, false);
            uri.AppendPath("/sms", false);
            uri.AppendQuery("api-version", apiVersion, true);
            request.Uri = uri;
            request.Headers.Add("Accept", "application/json");
            request.Headers.Add("Content-Type", "application/json");
            var model = new SendMessageRequest(@from, to, message)
            {
                SendSmsOptions = sendSmsOptions
            };
            var content = new Utf8JsonRequestContent();

            content.JsonWriter.WriteObjectValue(model);
            request.Content = content;
            return(message0);
        }
 /// <summary>
 /// Sends a SMS <paramref name="from"/> a phone number that is acquired by the authenticated account, <paramref name="to"/> another phone number.
 /// </summary>
 /// <param name="from">The sender's phone number that is owned by the authenticated account.</param>
 /// <param name="to">The recipient's phone number.</param>
 /// <param name="message">The contents of the message that will be sent to the recipient. The allowable content is defined by RFC 5724. If the message has more than 160 characters, the server will split it into multiple SMSs automatically.</param>
 /// <param name="sendSmsOptions">Optional configuration for sending SMS messages.</param>
 /// <param name="cancellationToken">The cancellation token for the underlying request.</param>
 /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> for details returned from the server.</exception>
 /// <exception cref="ArgumentNullException"><paramref name="from"/> is null.</exception>
 /// <exception cref="ArgumentNullException"><paramref name="to"/> is null.</exception>
 /// <exception cref="ArgumentNullException"><paramref name="message"/> is null.</exception>
 public virtual Response <SendSmsResponse> Send(PhoneNumberIdentifier from, PhoneNumberIdentifier to, string message, SendSmsOptions sendSmsOptions = null, CancellationToken cancellationToken = default)
 {
     Argument.AssertNotNullOrEmpty(from.PhoneNumber, nameof(from));
     Argument.AssertNotNullOrEmpty(to.PhoneNumber, nameof(to));
     return(Send(from, new[] { to }, message, sendSmsOptions, cancellationToken));
 }
 /// <summary> Sends an SMS message from a phone number that belongs to the authenticated account. </summary>
 /// <param name="from"> The sender&apos;s phone number in E.164 format that is owned by the authenticated account. </param>
 /// <param name="to"> The recipient&apos;s phone number in E.164 format. In this version, only one recipient in the list is supported. </param>
 /// <param name="message"> The contents of the message that will be sent to the recipient. The allowable content is defined by RFC 5724. </param>
 /// <param name="sendSmsOptions"> Optional configuration for sending SMS messages. </param>
 /// <param name="cancellationToken"> The cancellation token to use. </param>
 /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> for details returned from the server.</exception>
 /// <exception cref="ArgumentNullException"><paramref name="from"/> is null.</exception>
 /// <exception cref="ArgumentNullException"><paramref name="to"/> is null.</exception>
 /// <exception cref="ArgumentNullException"><paramref name="message"/> is null.</exception>
 public virtual Response <SendSmsResponse> Send(PhoneNumberIdentifier from, IEnumerable <PhoneNumberIdentifier> to, string message, SendSmsOptions sendSmsOptions = null, CancellationToken cancellationToken = default)
 {
     using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SmsClient)}.{nameof(Send)}");
     scope.Start();
     try
     {
         Argument.AssertNotNullOrEmpty(from.PhoneNumber, nameof(from));
         return(RestClient.Send(from.PhoneNumber, to.Select(x => AssertNotNullOrEmpty(x.PhoneNumber, nameof(to))), message, sendSmsOptions, cancellationToken));
     }
     catch (Exception ex)
     {
         scope.Failed(ex);
         throw;
     }
 }
 /// <summary>
 /// Sends a SMS <paramref name="from"/> a phone number that is acquired by the authenticated account, <paramref name="to"/> another phone number.
 /// </summary>
 /// <param name="from">The sender's phone number that is owned by the authenticated account.</param>
 /// <param name="to">The recipient's phone number.</param>
 /// <param name="message">The contents of the message that will be sent to the recipient. The allowable content is defined by RFC 5724. If the message has more than 160 characters, the server will split it into multiple SMSs automatically.</param>
 /// <param name="sendSmsOptions">Optional configuration for sending SMS messages.</param>
 /// <param name="cancellationToken">The cancellation token for the task.</param>
 /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> for details returned from the server.</exception>
 /// <exception cref="ArgumentNullException"><paramref name="from"/> is null.</exception>
 /// <exception cref="ArgumentNullException"><paramref name="to"/> is null.</exception>
 /// <exception cref="ArgumentNullException"><paramref name="message"/> is null.</exception>
 public virtual async Task <Response <SendSmsResponse> > SendAsync(PhoneNumberIdentifier from, PhoneNumberIdentifier to, string message, SendSmsOptions sendSmsOptions = null, CancellationToken cancellationToken = default)
 {
     Argument.AssertNotNullOrEmpty(from.PhoneNumber, nameof(from));
     Argument.AssertNotNullOrEmpty(to.PhoneNumber, nameof(to));
     return(await SendAsync(from, new[] { to }, message, sendSmsOptions, cancellationToken).ConfigureAwait(false));
 }