| | | 1 | | using System.Net; |
| | | 2 | | using Microsoft.Extensions.Http; |
| | | 3 | | using Newtonsoft.Json; |
| | | 4 | | using Newtonsoft.Json.Linq; |
| | | 5 | | using Services.ApiServices; |
| | | 6 | | using SlqStudio.Application.Services.EmailService; |
| | | 7 | | |
| | | 8 | | #pragma warning disable SYSLIB0014 // HttpWebRequest overrides — legacy GET paths, migrate with BaseApiClient |
| | | 9 | | |
| | | 10 | | namespace Services.ApiServices.TecDocApi |
| | | 11 | | { |
| | | 12 | | public class TecDocApiClient : BaseApiClient |
| | | 13 | | { |
| | | 14 | | private readonly string _baseUri; |
| | | 15 | | |
| | | 16 | | public TecDocApiClient( |
| | | 17 | | IHttpClientFactory? httpClientFactory, |
| | | 18 | | IEmailService? emailService, |
| | | 19 | | string baseUri = "http://localhost:8000/") |
| | 4 | 20 | | : base(httpClientFactory, emailService, "TecDocApi", TimeSpan.FromSeconds(15)) |
| | | 21 | | { |
| | 4 | 22 | | _baseUri = baseUri; |
| | 4 | 23 | | } |
| | | 24 | | |
| | | 25 | | // Конструктор для обратной совместимости |
| | | 26 | | public TecDocApiClient(string baseUri = "http://localhost:8000/") |
| | 4 | 27 | | : this(null, null, baseUri) |
| | | 28 | | { |
| | 4 | 29 | | } |
| | | 30 | | |
| | | 31 | | public override JObject MakeRequestPost<T>(T requestModel, string requestUri) |
| | | 32 | | { |
| | | 33 | | // TecDoc использует GET вместо POST в оригинальной реализации |
| | | 34 | | try |
| | | 35 | | { |
| | 0 | 36 | | var request = (HttpWebRequest)WebRequest.Create(requestUri); |
| | 0 | 37 | | request.Method = "GET"; |
| | 0 | 38 | | request.ContentType = "application/json"; |
| | 0 | 39 | | request.Timeout = (int)_timeout.TotalMilliseconds; |
| | | 40 | | |
| | 0 | 41 | | using (var response = (HttpWebResponse)request.GetResponse()) |
| | | 42 | | { |
| | 0 | 43 | | using (var streamReader = new StreamReader(response.GetResponseStream())) |
| | | 44 | | { |
| | 0 | 45 | | var jsonResponse = streamReader.ReadToEnd(); |
| | 0 | 46 | | if (string.IsNullOrWhiteSpace(jsonResponse)) |
| | | 47 | | { |
| | 0 | 48 | | return new JObject(); |
| | | 49 | | } |
| | 0 | 50 | | return JObject.Parse(jsonResponse); |
| | | 51 | | } |
| | | 52 | | } |
| | | 53 | | } |
| | 0 | 54 | | catch (WebException ex) when (ex.Response is HttpWebResponse httpResponse) |
| | | 55 | | { |
| | 0 | 56 | | return HandleError($"HTTP ошибка при запросе к {_clientName}", ex, requestUri); |
| | | 57 | | } |
| | 0 | 58 | | catch (System.Net.Sockets.SocketException ex) |
| | | 59 | | { |
| | 0 | 60 | | return HandleError($"Ошибка соединения с {_clientName}", ex, requestUri); |
| | | 61 | | } |
| | 0 | 62 | | catch (JsonException ex) |
| | | 63 | | { |
| | 0 | 64 | | return HandleError($"Ошибка парсинга JSON от {_clientName}", ex, requestUri); |
| | | 65 | | } |
| | 0 | 66 | | catch (Exception ex) |
| | | 67 | | { |
| | 0 | 68 | | return HandleError($"Неожиданная ошибка при запросе к {_clientName}", ex, requestUri); |
| | | 69 | | } |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | public override async Task<bool> GetTestRequest(string businessId, string apiKey) |
| | | 73 | | { |
| | | 74 | | try |
| | | 75 | | { |
| | | 76 | | // Простая проверка доступности через GET запрос к базовому URL |
| | 0 | 77 | | var testUrl = _baseUri.TrimEnd('/'); |
| | | 78 | | |
| | 0 | 79 | | var request = (HttpWebRequest)WebRequest.Create(testUrl); |
| | 0 | 80 | | request.Method = "GET"; |
| | 0 | 81 | | request.ContentType = "application/json"; |
| | 0 | 82 | | request.Timeout = (int)_timeout.TotalMilliseconds; |
| | | 83 | | |
| | 0 | 84 | | using (var response = (HttpWebResponse)await request.GetResponseAsync()) |
| | | 85 | | { |
| | | 86 | | // Если получили ответ (даже если не 200), значит сервер доступен |
| | 0 | 87 | | return response.StatusCode == HttpStatusCode.OK || |
| | 0 | 88 | | response.StatusCode == HttpStatusCode.NotFound || |
| | 0 | 89 | | response.StatusCode == HttpStatusCode.MethodNotAllowed; |
| | | 90 | | } |
| | | 91 | | } |
| | 0 | 92 | | catch (WebException ex) when (ex.Response is HttpWebResponse httpResponse) |
| | | 93 | | { |
| | | 94 | | // Если получили HTTP ответ (даже ошибку), значит сервер доступен |
| | 0 | 95 | | return httpResponse.StatusCode != HttpStatusCode.ServiceUnavailable; |
| | | 96 | | } |
| | 0 | 97 | | catch |
| | | 98 | | { |
| | 0 | 99 | | return false; |
| | | 100 | | } |
| | 0 | 101 | | } |
| | | 102 | | } |
| | | 103 | | } |