| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | using Microsoft.Extensions.Logging.Abstractions; |
| | | 5 | | using OzonOrdersWeb.Authorization; |
| | | 6 | | using Services.ApiServices.ApiSupplierIntegration; |
| | | 7 | | |
| | | 8 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories; |
| | | 9 | | |
| | | 10 | | /// <summary>Общие read-only действия для всех API-поставщиков из реестра.</summary> |
| | | 11 | | [Area("Studio2")] |
| | | 12 | | [ServiceEndpoint] |
| | | 13 | | [Authorize(Roles = "User,Admin")] |
| | | 14 | | [Route("ApiSupplier/[action]")] |
| | | 15 | | public sealed class ApiSupplierController : Controller |
| | | 16 | | { |
| | | 17 | | private readonly ApiSupplierConnectorRegistry _registry; |
| | | 18 | | private readonly ILogger<ApiSupplierController> _logger; |
| | | 19 | | |
| | 5 | 20 | | public ApiSupplierController( |
| | 5 | 21 | | ApiSupplierConnectorRegistry registry, |
| | 5 | 22 | | ILogger<ApiSupplierController>? logger = null) |
| | | 23 | | { |
| | 5 | 24 | | _registry = registry; |
| | 5 | 25 | | _logger = logger ?? NullLogger<ApiSupplierController>.Instance; |
| | 5 | 26 | | } |
| | | 27 | | |
| | | 28 | | [HttpGet, ServiceEndpoint] |
| | | 29 | | public async Task<IActionResult> Ping(string providerKey, CancellationToken cancellationToken) |
| | | 30 | | { |
| | 5 | 31 | | if (!_registry.TryGet(providerKey, out var connector) || connector == null) |
| | 1 | 32 | | return BadRequest(new { error = true, message = "Неизвестный API-поставщик." }); |
| | | 33 | | |
| | | 34 | | try |
| | | 35 | | { |
| | 4 | 36 | | var result = await connector.PingAsync(cancellationToken); |
| | 2 | 37 | | return Ok(new |
| | 2 | 38 | | { |
| | 2 | 39 | | isAvailable = result.IsAvailable, |
| | 2 | 40 | | providerKey = connector.ProviderKey, |
| | 2 | 41 | | message = result.Message ?? (result.IsAvailable ? null : "API недоступно.") |
| | 2 | 42 | | }); |
| | | 43 | | } |
| | 1 | 44 | | catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) |
| | | 45 | | { |
| | 1 | 46 | | throw; |
| | | 47 | | } |
| | 1 | 48 | | catch (Exception ex) |
| | | 49 | | { |
| | 1 | 50 | | _logger.LogInformation(ex, "Сбой проверки доступности внешнего поставщика {ProviderKey}", providerKey); |
| | 1 | 51 | | return Ok(new |
| | 1 | 52 | | { |
| | 1 | 53 | | isAvailable = false, |
| | 1 | 54 | | providerKey = connector.ProviderKey, |
| | 1 | 55 | | message = "API недоступно." |
| | 1 | 56 | | }); |
| | | 57 | | } |
| | 4 | 58 | | } |
| | | 59 | | } |