| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | using Newtonsoft.Json.Linq; |
| | | 3 | | using Services.ApiServices.ApiSupplierRequestLog; |
| | | 4 | | |
| | | 5 | | namespace Services.ApiServices.MoskvorechieApi; |
| | | 6 | | |
| | | 7 | | /// <summary>JSON API портала Москворечье. Все вызовы принудительно используют UTF-8.</summary> |
| | | 8 | | public sealed class MoskvorechieIstraHttpClient |
| | | 9 | | { |
| | | 10 | | public const string ProviderKey = "MoskvorechieIstra"; |
| | | 11 | | private readonly IHttpClientFactory _factory; |
| | | 12 | | private readonly IOptionsMonitor<MoskvorechieIstraConfig> _config; |
| | | 13 | | private readonly ApiSupplierRequestLogStore _logs; |
| | | 14 | | |
| | 94 | 15 | | public MoskvorechieIstraHttpClient(IHttpClientFactory factory, IOptionsMonitor<MoskvorechieIstraConfig> config, ApiS |
| | 94 | 16 | | => (_factory, _config, _logs) = (factory, config, logs); |
| | | 17 | | |
| | | 18 | | public Task<JToken> GetAsync(string action, IReadOnlyDictionary<string, string?>? parameters = null, CancellationTok |
| | 34 | 19 | | SendAsync(HttpMethod.Get, action, parameters, ct); |
| | | 20 | | |
| | | 21 | | public Task<JToken> PostAsync(string action, IReadOnlyDictionary<string, string?>? parameters = null, CancellationTo |
| | 53 | 22 | | SendAsync(HttpMethod.Post, action, parameters, ct); |
| | | 23 | | |
| | | 24 | | private async Task<JToken> SendAsync(HttpMethod method, string action, IReadOnlyDictionary<string, string?>? paramet |
| | | 25 | | { |
| | 87 | 26 | | var config = _config.CurrentValue; |
| | 87 | 27 | | if (string.IsNullOrWhiteSpace(config.Login) || string.IsNullOrWhiteSpace(config.ApiKey)) |
| | 0 | 28 | | throw new MoskvorechieApiException("Не настроены MoskvorechieIstra:Login или MoskvorechieIstra:ApiKey."); |
| | | 29 | | |
| | 87 | 30 | | var values = new Dictionary<string, string?> |
| | 87 | 31 | | { |
| | 87 | 32 | | ["l"] = config.Login, ["p"] = config.ApiKey, ["cs"] = "utf8", ["act"] = action |
| | 87 | 33 | | }; |
| | 87 | 34 | | if (parameters is not null) |
| | 330 | 35 | | foreach (var pair in parameters.Where(x => x.Value is not null)) values[pair.Key] = pair.Value; |
| | | 36 | | |
| | 87 | 37 | | var timer = System.Diagnostics.Stopwatch.StartNew(); |
| | | 38 | | try |
| | | 39 | | { |
| | 87 | 40 | | using var request = new HttpRequestMessage(method, config.BaseUrl); |
| | 87 | 41 | | if (method == HttpMethod.Get) |
| | | 42 | | { |
| | 170 | 43 | | var uri = new UriBuilder(config.BaseUrl) { Query = string.Join("&", values.Select(x => $"{Uri.EscapeData |
| | 34 | 44 | | request.RequestUri = uri.Uri; |
| | | 45 | | } |
| | 595 | 46 | | else request.Content = new FormUrlEncodedContent(values.Where(x => x.Value is not null).Select(x => new KeyV |
| | | 47 | | |
| | 87 | 48 | | using var response = await _factory.CreateClient("MoskvorechieIstraApi").SendAsync(request, ct); |
| | 87 | 49 | | var text = await response.Content.ReadAsStringAsync(ct); |
| | 87 | 50 | | _logs.Add(ProviderKey, method.Method, action, (int)response.StatusCode, timer, action, text); |
| | 90 | 51 | | if (!response.IsSuccessStatusCode) throw new MoskvorechieApiException($"Москворечье вернул HTTP {(int)respon |
| | 84 | 52 | | var token = JToken.Parse(text); |
| | 85 | 53 | | if (token is JObject error && error["error"] is not null) throw new MoskvorechieApiException(error["error"]? |
| | 83 | 54 | | return token; |
| | | 55 | | } |
| | 8 | 56 | | catch (MoskvorechieApiException) { throw; } |
| | 0 | 57 | | catch (Exception ex) when (ex is HttpRequestException or TaskCanceledException or Newtonsoft.Json.JsonException) |
| | | 58 | | { |
| | 0 | 59 | | _logs.Add(ProviderKey, method.Method, action, null, timer, action, ex.Message); |
| | 0 | 60 | | throw new MoskvorechieApiException("Не удалось выполнить запрос к API Москворечья.", innerException: ex); |
| | | 61 | | } |
| | 83 | 62 | | } |
| | | 63 | | } |