| | | 1 | | using Microsoft.Extensions.Http; |
| | | 2 | | using SlqStudio.Application.Services.EmailService; |
| | | 3 | | using Services.ApiServices; |
| | | 4 | | |
| | | 5 | | namespace Services.ApiServices.InterpartsPriceApi; |
| | | 6 | | |
| | | 7 | | public class InterpartsPriceApiClient : BaseApiClient |
| | | 8 | | { |
| | | 9 | | public InterpartsPriceApiClient(IHttpClientFactory httpClientFactory, IEmailService? emailService) |
| | | 10 | | : base(httpClientFactory, emailService, "InterpartsPriceApi", TimeSpan.FromSeconds(15)) |
| | | 11 | | { |
| | | 12 | | } |
| | | 13 | | |
| | | 14 | | public async Task<InterpartsPriceHttpResult> GetXmlAsync(string requestUrl, CancellationToken cancellationToken = de |
| | | 15 | | { |
| | | 16 | | try |
| | | 17 | | { |
| | | 18 | | var client = _httpClientFactory?.CreateClient(_clientName) ?? new HttpClient(); |
| | | 19 | | client.Timeout = _timeout; |
| | | 20 | | SetHttpClientHeaders(client); |
| | | 21 | | using var response = await client.GetAsync(requestUrl, cancellationToken); |
| | | 22 | | var body = await response.Content.ReadAsStringAsync(cancellationToken); |
| | | 23 | | return new InterpartsPriceHttpResult((int)response.StatusCode, body, |
| | | 24 | | response.IsSuccessStatusCode ? null : $"Interparts Price вернул HTTP {(int)response.StatusCode} ({respon |
| | | 25 | | } |
| | | 26 | | catch (TaskCanceledException ex) |
| | | 27 | | { |
| | | 28 | | return new InterpartsPriceHttpResult(null, null, $"Таймаут Interparts Price: {ex.Message}"); |
| | | 29 | | } |
| | | 30 | | catch (HttpRequestException ex) |
| | | 31 | | { |
| | | 32 | | return new InterpartsPriceHttpResult(ex.StatusCode is null ? null : (int)ex.StatusCode, null, $"Ошибка сети |
| | | 33 | | } |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | public override Task<bool> GetTestRequest(string businessId, string apiKey) |
| | | 37 | | { |
| | | 38 | | // Здесь нет полноценного тестового эндпоинта, считаем что клиент "работает", |
| | | 39 | | // пока запросы не падают в менеджере. |
| | | 40 | | return Task.FromResult(true); |
| | | 41 | | } |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | public sealed record InterpartsPriceHttpResult(int? StatusCode, string? Body, string? ErrorMessage); |