| | | 1 | | using System.Text; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using PartsInfo.Cache; |
| | | 4 | | |
| | | 5 | | namespace PartsInfo.HttpUtils; |
| | | 6 | | |
| | | 7 | | public 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 | | |
| | 18 | 14 | | public ProxyHttpClientService(IHttpClientFactory httpClientFactory, UrlResponseCacheService cacheService) |
| | | 15 | | { |
| | 18 | 16 | | _httpClientFactory = httpClientFactory; |
| | 18 | 17 | | _cacheService = cacheService; |
| | 18 | 18 | | } |
| | | 19 | | |
| | | 20 | | public async Task<JsonDocument?> GetJsonAsync(string url) |
| | | 21 | | { |
| | 13 | 22 | | if (IsBlockedInterpartsApiUrl(url)) |
| | | 23 | | { |
| | 4 | 24 | | Console.WriteLine($"[BLOCKED] Outbound request to {BlockedHost} is disabled: {url}"); |
| | 4 | 25 | | return null; |
| | | 26 | | } |
| | | 27 | | |
| | 9 | 28 | | if (_cacheService.TryGet(url, out var cachedResponse)) |
| | 1 | 29 | | return JsonDocument.Parse(cachedResponse); |
| | | 30 | | |
| | | 31 | | try |
| | | 32 | | { |
| | 8 | 33 | | using var response = await _httpClientFactory.CreateClient(ClientName).GetAsync(url); |
| | 5 | 34 | | if (!response.IsSuccessStatusCode) |
| | 1 | 35 | | return null; |
| | | 36 | | |
| | 4 | 37 | | var content = await response.Content.ReadAsStringAsync(); |
| | 4 | 38 | | _cacheService.Set(url, content, TimeSpan.FromMinutes(5)); |
| | 4 | 39 | | return JsonDocument.Parse(content); |
| | | 40 | | } |
| | 3 | 41 | | catch |
| | | 42 | | { |
| | 3 | 43 | | return null; |
| | | 44 | | } |
| | 13 | 45 | | } |
| | | 46 | | |
| | | 47 | | public async Task<JsonDocument?> PostJsonAsync(string url, object data) |
| | | 48 | | { |
| | 5 | 49 | | if (IsBlockedInterpartsApiUrl(url)) |
| | | 50 | | { |
| | 2 | 51 | | Console.WriteLine($"[BLOCKED] Outbound request to {BlockedHost} is disabled: {url}"); |
| | 2 | 52 | | return null; |
| | | 53 | | } |
| | | 54 | | |
| | 3 | 55 | | string cacheKey = $"{url}_{JsonSerializer.Serialize(data)}"; |
| | 3 | 56 | | if (_cacheService.TryGet(cacheKey, out var cachedResponse)) |
| | 1 | 57 | | return JsonDocument.Parse(cachedResponse); |
| | | 58 | | |
| | | 59 | | try |
| | | 60 | | { |
| | 2 | 61 | | var json = JsonSerializer.Serialize(data); |
| | 2 | 62 | | using var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); |
| | 2 | 63 | | using var response = await _httpClientFactory.CreateClient(ClientName).PostAsync(url, httpContent); |
| | 2 | 64 | | if (!response.IsSuccessStatusCode) |
| | 1 | 65 | | return null; |
| | | 66 | | |
| | 1 | 67 | | var responseContent = await response.Content.ReadAsStringAsync(); |
| | 1 | 68 | | _cacheService.Set(cacheKey, responseContent, TimeSpan.FromMinutes(5)); |
| | 1 | 69 | | return JsonDocument.Parse(responseContent); |
| | | 70 | | } |
| | 0 | 71 | | catch |
| | | 72 | | { |
| | 0 | 73 | | return null; |
| | | 74 | | } |
| | 5 | 75 | | } |
| | | 76 | | |
| | | 77 | | private static bool IsBlockedInterpartsApiUrl(string? url) |
| | | 78 | | { |
| | 18 | 79 | | if (string.IsNullOrWhiteSpace(url) || !Uri.TryCreate(url, UriKind.Absolute, out var uri)) |
| | 3 | 80 | | return false; |
| | | 81 | | |
| | 15 | 82 | | return string.Equals(uri.Host, BlockedHost, StringComparison.OrdinalIgnoreCase); |
| | | 83 | | } |
| | | 84 | | } |