| | | 1 | | using System.Net.Http.Json; |
| | | 2 | | using Chronicis.Shared.Routing; |
| | | 3 | | |
| | | 4 | | namespace Chronicis.Client.Services; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Client service for the unified path-resolution endpoint. |
| | | 8 | | /// </summary> |
| | | 9 | | public class PathApiService : IPathApiService |
| | | 10 | | { |
| | | 11 | | private readonly HttpClient _http; |
| | | 12 | | private readonly ILogger<PathApiService> _logger; |
| | | 13 | | |
| | | 14 | | public PathApiService(HttpClient http, ILogger<PathApiService> logger) |
| | | 15 | | { |
| | 8 | 16 | | _http = http; |
| | 8 | 17 | | _logger = logger; |
| | 8 | 18 | | } |
| | | 19 | | |
| | | 20 | | public async Task<SlugPathResolution?> ResolveAsync(string path, CancellationToken cancellationToken = default) |
| | | 21 | | { |
| | | 22 | | var encodedPath = string.Join("/", path.Trim('/').Split('/').Select(Uri.EscapeDataString)); |
| | | 23 | | |
| | | 24 | | try |
| | | 25 | | { |
| | | 26 | | var response = await _http.GetAsync($"paths/resolve/{encodedPath}", cancellationToken); |
| | | 27 | | |
| | | 28 | | if (response.StatusCode == System.Net.HttpStatusCode.NotFound) |
| | | 29 | | return null; |
| | | 30 | | |
| | | 31 | | if (!response.IsSuccessStatusCode) |
| | | 32 | | { |
| | | 33 | | _logger.LogWarning("Path resolution failed for '{Path}': {StatusCode}", path, response.StatusCode); |
| | | 34 | | return null; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | return await response.Content.ReadFromJsonAsync<SlugPathResolution>(cancellationToken: cancellationToken); |
| | | 38 | | } |
| | | 39 | | catch (HttpRequestException ex) |
| | | 40 | | { |
| | | 41 | | _logger.LogError(ex, "Error resolving path '{Path}'", path); |
| | | 42 | | return null; |
| | | 43 | | } |
| | | 44 | | } |
| | | 45 | | } |