< Summary

Information
Class: Services.ApiServices.JapartsApi.JapartsHttpClient
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/ApiServices/JapartsApi/JapartsHttpClient.cs
Line coverage
87%
Covered lines: 21
Uncovered lines: 3
Coverable lines: 24
Total lines: 46
Line coverage: 87.5%
Branch coverage
68%
Covered branches: 11
Total branches: 16
Branch coverage: 68.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor(...)100%11100%
SendAsync()68.75%171685%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/ApiServices/JapartsApi/JapartsHttpClient.cs

#LineLine coverage
 1using System.Diagnostics;
 2using System.Globalization;
 3using System.Text;
 4using System.Text.Json;
 5using Services.ApiServices.ApiSupplierRequestLog;
 6
 7namespace Services.ApiServices.JapartsApi;
 8
 9public sealed class JapartsHttpClient
 10{
 111    private static readonly Encoding Cp1251 = Encoding.GetEncoding(1251);
 12    private readonly IHttpClientFactory _factory;
 13    private readonly JapartsConfig _config;
 14    private readonly ApiSupplierRequestLogStore _logs;
 15
 316    public JapartsHttpClient(IHttpClientFactory factory, JapartsConfig config, ApiSupplierRequestLogStore logs) =>
 317        (_factory, _config, _logs) = (factory, config, logs);
 18
 19    public async Task<JsonDocument> SendAsync(string action, IReadOnlyDictionary<string, string?>? parameters = null,
 20        CancellationToken ct = default)
 21    {
 322        if (string.IsNullOrWhiteSpace(_config.Login) || string.IsNullOrWhiteSpace(_config.Password))
 023            throw new JapartsApiException("Не заданы Japarts:Login или Japarts:Password.");
 324        var query = new Dictionary<string, string?> { ["id"] = "ws", ["action"] = action, ["login"] = _config.Login, ["p
 4525        if (parameters != null) foreach (var pair in parameters) query[pair.Key] = pair.Value;
 326        var uri = new UriBuilder(_config.BaseUrl);
 5127        uri.Query = string.Join("&", query.Where(x => x.Value != null).Select(x => $"{Uri.EscapeDataString(x.Key)}={Uri.
 328        var timer = Stopwatch.StartNew();
 329        HttpResponseMessage? response = null;
 330        string body = string.Empty;
 31        try
 32        {
 333            response = await _factory.CreateClient("JapartsApi").GetAsync(uri.Uri, ct);
 134            var bytes = await response.Content.ReadAsByteArrayAsync(ct);
 135            body = Cp1251.GetString(bytes);
 136            _logs.Add("Japarts", "GET", uri.Uri.GetLeftPart(UriPartial.Path), (int)response.StatusCode, timer, uri.Query
 137            if (!response.IsSuccessStatusCode) throw new JapartsApiException($"Japarts вернул HTTP {(int)response.Status
 138            var json = JsonDocument.Parse(body);
 139            if (json.RootElement.ValueKind == JsonValueKind.Object && json.RootElement.TryGetProperty("error", out var e
 040                throw new JapartsApiException($"Japarts: {error.GetString()}");
 141            return json;
 42        }
 043        catch (JsonException ex) { throw new JapartsApiException($"Japarts вернул некорректный JSON: {ex.Message}"); }
 344        finally { response?.Dispose(); }
 145    }
 46}