Example #1
0
        /// <inheritdoc />
        public async Task SendAsync(string destination, string subject, string body)
        {
            var phoneNumbers        = GetRecipientsFromDestination(destination);
            var requestBody         = SendRequest.CreateSms(phoneNumbers, Settings.Sender ?? Settings.SenderName, body, Settings.Validity);
            var jsonData            = JsonSerializer.Serialize(requestBody, GetJsonSerializerOptions());
            var data                = new StringContent(jsonData, Encoding.UTF8, "application/json");
            var httpResponseMessage = await HttpClient.PostAsync("Send", data);

            if (!httpResponseMessage.IsSuccessStatusCode)
            {
                var errorMessage = "SMS Delivery failed.\n ";
                if (httpResponseMessage.Content != null)
                {
                    errorMessage += await httpResponseMessage.Content.ReadAsStringAsync();
                }
                Logger.LogError(errorMessage);
                throw new SmsServiceException(errorMessage);
            }
            var responseContent = await httpResponseMessage.Content.ReadAsStringAsync();

            var response = JsonSerializer.Deserialize <SendResponse>(responseContent);

            if (!response.IsSuccess)
            {
                var errorMessage = $"SMS Delivery failed.\n {response.ErrorCode} - {response.ErrorMessage}.\n {JsonSerializer.Serialize(response.Messages)}";
                Logger.LogError(errorMessage);
                throw new SmsServiceException(errorMessage);
            }
            Logger.LogInformation("SMS message successfully sent: \n", JsonSerializer.Serialize(response.Messages));
        }