Created
April 6, 2024 19:57
-
-
Save sampletext32/81369b10f06608ce1893dadbd312c758 to your computer and use it in GitHub Desktop.
Needs Microsoft.AspNetCore.WebUtilities and FluentResults nuget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using FluentResults; | |
using Microsoft.AspNetCore.WebUtilities; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.Extensions.Options; | |
using Newtonsoft.Json; | |
namespace Auth.BLL.Smsc; | |
public class SmscHttpClient | |
{ | |
private readonly HttpClient _client; | |
private readonly SmscConfig _config; | |
private readonly ILogger<SmscHttpClient> _logger; | |
public SmscHttpClient(HttpClient client, IOptions<SmscConfig> config, ILogger<SmscHttpClient> logger) | |
{ | |
_client = client; | |
_logger = logger; | |
_config = config.Value; | |
} | |
public async Task<Result<SmscResult>> SendCodeViaCall(string recipient, CancellationToken cancellationToken) | |
{ | |
const string action = "send.php"; | |
var url = action; | |
// fmt = 0 - OK - 1 SMS, ID - 1234 | |
// fmt = 1 - 1234,1,1.40,100.50 или 0,-3 | |
// fmt = 2 - xml | |
// fmt = 3 - json | |
// cost = 3 means that smsc will return the cost and new balance | |
var parameters = new Dictionary<string, string?>() | |
{ | |
{"login", _config.Login}, | |
{"psw", _config.Password}, | |
{"charset", "utf-8"}, | |
{"fmt", "3"}, | |
{"cost", "3"}, | |
{"phones", recipient}, | |
{"mes", "code"}, | |
{"call", "1"} | |
}; | |
url = QueryHelpers.AddQueryString(url, parameters); | |
var request = new HttpRequestMessage( | |
HttpMethod.Get, | |
url | |
); | |
var response = await _client.SendAsync(request, cancellationToken); | |
if (!response.IsSuccessStatusCode) | |
{ | |
return new SmscError(response.ReasonPhrase ?? $"Неизвестная ошибка при вызове SMSC. Status: {response.StatusCode}"); | |
} | |
var content = await response.Content.ReadAsStringAsync(cancellationToken); | |
_logger.LogInformation("Received SMSC Response: {req} {res}", url, content); | |
var smscResult = JsonConvert.DeserializeObject<SmscResult>(content) ?? new SmscResult {Error = "Failed to parse response"}; | |
return Result.Ok(smscResult); | |
} | |
public async Task<Result<SmscResult>> SendCodeViaSms(string message, string recipient, CancellationToken cancellationToken) | |
{ | |
const string action = "send.php"; | |
var url = action; | |
// fmt = 0 - OK - 1 SMS, ID - 1234 | |
// fmt = 1 - 1234,1,1.40,100.50 или 0,-3 | |
// fmt = 2 - xml | |
// fmt = 3 - json | |
// cost = 3 means that smsc will return the cost and new balance | |
var parameters = new Dictionary<string, string?>() | |
{ | |
{"login", _config.Login}, | |
{"psw", _config.Password}, | |
{"charset", "utf-8"}, | |
{"fmt", "3"}, | |
{"cost", "3"}, | |
{"phones", recipient}, | |
{"mes", message} | |
}; | |
url = QueryHelpers.AddQueryString(url, parameters); | |
var request = new HttpRequestMessage( | |
HttpMethod.Get, | |
url | |
); | |
var response = await _client.SendAsync(request, cancellationToken); | |
if (!response.IsSuccessStatusCode) | |
{ | |
return new SmscError(response.ReasonPhrase ?? $"Неизвестная ошибка при вызове SMSC. Status: {response.StatusCode}"); | |
} | |
var content = await response.Content.ReadAsStringAsync(cancellationToken); | |
_logger.LogInformation("Received SMSC Response: {req} {res}", url, content); | |
var smscResult = JsonConvert.DeserializeObject<SmscResult>(content) ?? new SmscResult {Error = "Failed to parse response"}; | |
return Result.Ok(smscResult); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment