-
Notifications
You must be signed in to change notification settings - Fork 0
/
SMSSender.cs
36 lines (32 loc) · 1.17 KB
/
SMSSender.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
namespace SMSSender
{
public class SMSSender : ISMSSender
{
private readonly SMSSenderOptions _options;
private readonly HttpClient _httpClient;
public SMSSender(IOptions<SMSSenderOptions> options, HttpClient httpClient)
{
_options = options.Value;
_httpClient = httpClient;
}
public async Task SendAsync(string destination, string message)
{
if (_options.UsePostMethod)
{
var httpContent = _options.HttpContent(destination, message);
var response = await _httpClient.PostAsync(_options.AppUrl, httpContent);
if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError)
{
throw new Exception("post method send sms failed, try later.");
}
response.EnsureSuccessStatusCode();
}
var requestUri = _options.RequestUri(destination, message);
await _httpClient.GetStringAsync(requestUri: requestUri);
}
}
}