< Summary

Information
Class: Services.ApiServices.MoskvorechieApi.MoskvorechieIstraHttpClient
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/ApiServices/MoskvorechieApi/MoskvorechieIstraHttpClient.cs
Line coverage
87%
Covered lines: 27
Uncovered lines: 4
Coverable lines: 31
Total lines: 63
Line coverage: 87%
Branch coverage
77%
Covered branches: 14
Total branches: 18
Branch coverage: 77.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetAsync(...)100%11100%
PostAsync(...)100%11100%
SendAsync()80%222083.33%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/ApiServices/MoskvorechieApi/MoskvorechieIstraHttpClient.cs

#LineLine coverage
 1using Microsoft.Extensions.Options;
 2using Newtonsoft.Json.Linq;
 3using Services.ApiServices.ApiSupplierRequestLog;
 4
 5namespace Services.ApiServices.MoskvorechieApi;
 6
 7/// <summary>JSON API портала Москворечье. Все вызовы принудительно используют UTF-8.</summary>
 8public 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
 9415    public MoskvorechieIstraHttpClient(IHttpClientFactory factory, IOptionsMonitor<MoskvorechieIstraConfig> config, ApiS
 9416        => (_factory, _config, _logs) = (factory, config, logs);
 17
 18    public Task<JToken> GetAsync(string action, IReadOnlyDictionary<string, string?>? parameters = null, CancellationTok
 3419        SendAsync(HttpMethod.Get, action, parameters, ct);
 20
 21    public Task<JToken> PostAsync(string action, IReadOnlyDictionary<string, string?>? parameters = null, CancellationTo
 5322        SendAsync(HttpMethod.Post, action, parameters, ct);
 23
 24    private async Task<JToken> SendAsync(HttpMethod method, string action, IReadOnlyDictionary<string, string?>? paramet
 25    {
 8726        var config = _config.CurrentValue;
 8727        if (string.IsNullOrWhiteSpace(config.Login) || string.IsNullOrWhiteSpace(config.ApiKey))
 028            throw new MoskvorechieApiException("Не настроены MoskvorechieIstra:Login или MoskvorechieIstra:ApiKey.");
 29
 8730        var values = new Dictionary<string, string?>
 8731        {
 8732            ["l"] = config.Login, ["p"] = config.ApiKey, ["cs"] = "utf8", ["act"] = action
 8733        };
 8734        if (parameters is not null)
 33035            foreach (var pair in parameters.Where(x => x.Value is not null)) values[pair.Key] = pair.Value;
 36
 8737        var timer = System.Diagnostics.Stopwatch.StartNew();
 38        try
 39        {
 8740            using var request = new HttpRequestMessage(method, config.BaseUrl);
 8741            if (method == HttpMethod.Get)
 42            {
 17043                var uri = new UriBuilder(config.BaseUrl) { Query = string.Join("&", values.Select(x => $"{Uri.EscapeData
 3444                request.RequestUri = uri.Uri;
 45            }
 59546            else request.Content = new FormUrlEncodedContent(values.Where(x => x.Value is not null).Select(x => new KeyV
 47
 8748            using var response = await _factory.CreateClient("MoskvorechieIstraApi").SendAsync(request, ct);
 8749            var text = await response.Content.ReadAsStringAsync(ct);
 8750            _logs.Add(ProviderKey, method.Method, action, (int)response.StatusCode, timer, action, text);
 9051            if (!response.IsSuccessStatusCode) throw new MoskvorechieApiException($"Москворечье вернул HTTP {(int)respon
 8452            var token = JToken.Parse(text);
 8553            if (token is JObject error && error["error"] is not null) throw new MoskvorechieApiException(error["error"]?
 8354            return token;
 55        }
 856        catch (MoskvorechieApiException) { throw; }
 057        catch (Exception ex) when (ex is HttpRequestException or TaskCanceledException or Newtonsoft.Json.JsonException)
 58        {
 059            _logs.Add(ProviderKey, method.Method, action, null, timer, action, ex.Message);
 060            throw new MoskvorechieApiException("Не удалось выполнить запрос к API Москворечья.", innerException: ex);
 61        }
 8362    }
 63}