< Summary

Information
Class: PartsInfo.HttpUtils.ProxyHttpClientService
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/WebServices/HttpUtils/ProxyHttpClientService.cs
Line coverage
94%
Covered lines: 36
Uncovered lines: 2
Coverable lines: 38
Total lines: 84
Line coverage: 94.7%
Branch coverage
100%
Covered branches: 16
Total branches: 16
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetJsonAsync()100%66100%
PostJsonAsync()100%6688.23%
IsBlockedInterpartsApiUrl(...)100%44100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/WebServices/HttpUtils/ProxyHttpClientService.cs

#LineLine coverage
 1using System.Text;
 2using System.Text.Json;
 3using PartsInfo.Cache;
 4
 5namespace PartsInfo.HttpUtils;
 6
 7public class ProxyHttpClientService
 8{
 9    private readonly IHttpClientFactory _httpClientFactory;
 10    private readonly UrlResponseCacheService _cacheService;
 11    private const string BlockedHost = "api.interparts.ru";
 12    private const string ClientName = "ProxyClient";
 13
 1814    public ProxyHttpClientService(IHttpClientFactory httpClientFactory, UrlResponseCacheService cacheService)
 15    {
 1816        _httpClientFactory = httpClientFactory;
 1817        _cacheService = cacheService;
 1818    }
 19
 20    public async Task<JsonDocument?> GetJsonAsync(string url)
 21    {
 1322        if (IsBlockedInterpartsApiUrl(url))
 23        {
 424            Console.WriteLine($"[BLOCKED] Outbound request to {BlockedHost} is disabled: {url}");
 425            return null;
 26        }
 27
 928        if (_cacheService.TryGet(url, out var cachedResponse))
 129            return JsonDocument.Parse(cachedResponse);
 30
 31        try
 32        {
 833            using var response = await _httpClientFactory.CreateClient(ClientName).GetAsync(url);
 534            if (!response.IsSuccessStatusCode)
 135                return null;
 36
 437            var content = await response.Content.ReadAsStringAsync();
 438            _cacheService.Set(url, content, TimeSpan.FromMinutes(5));
 439            return JsonDocument.Parse(content);
 40        }
 341        catch
 42        {
 343            return null;
 44        }
 1345    }
 46
 47    public async Task<JsonDocument?> PostJsonAsync(string url, object data)
 48    {
 549        if (IsBlockedInterpartsApiUrl(url))
 50        {
 251            Console.WriteLine($"[BLOCKED] Outbound request to {BlockedHost} is disabled: {url}");
 252            return null;
 53        }
 54
 355        string cacheKey = $"{url}_{JsonSerializer.Serialize(data)}";
 356        if (_cacheService.TryGet(cacheKey, out var cachedResponse))
 157            return JsonDocument.Parse(cachedResponse);
 58
 59        try
 60        {
 261            var json = JsonSerializer.Serialize(data);
 262            using var httpContent = new StringContent(json, Encoding.UTF8, "application/json");
 263            using var response = await _httpClientFactory.CreateClient(ClientName).PostAsync(url, httpContent);
 264            if (!response.IsSuccessStatusCode)
 165                return null;
 66
 167            var responseContent = await response.Content.ReadAsStringAsync();
 168            _cacheService.Set(cacheKey, responseContent, TimeSpan.FromMinutes(5));
 169            return JsonDocument.Parse(responseContent);
 70        }
 071        catch
 72        {
 073            return null;
 74        }
 575    }
 76
 77    private static bool IsBlockedInterpartsApiUrl(string? url)
 78    {
 1879        if (string.IsNullOrWhiteSpace(url) || !Uri.TryCreate(url, UriKind.Absolute, out var uri))
 380            return false;
 81
 1582        return string.Equals(uri.Host, BlockedHost, StringComparison.OrdinalIgnoreCase);
 83    }
 84}