| | | 1 | | using System.Net.Http.Json; |
| | | 2 | | using Chronicis.Shared.DTOs; |
| | | 3 | | |
| | | 4 | | namespace Chronicis.Client.Services; |
| | | 5 | | |
| | | 6 | | public class ExternalLinkApiService : IExternalLinkApiService |
| | | 7 | | { |
| | | 8 | | private readonly HttpClient _http; |
| | | 9 | | private readonly ILogger<ExternalLinkApiService> _logger; |
| | | 10 | | |
| | 0 | 11 | | public ExternalLinkApiService(HttpClient http, ILogger<ExternalLinkApiService> logger) |
| | | 12 | | { |
| | 0 | 13 | | _http = http; |
| | 0 | 14 | | _logger = logger; |
| | 0 | 15 | | } |
| | | 16 | | |
| | | 17 | | public async Task<List<ExternalLinkSuggestionDto>> GetSuggestionsAsync( |
| | | 18 | | Guid? worldId, |
| | | 19 | | string source, |
| | | 20 | | string query, |
| | | 21 | | CancellationToken ct) |
| | | 22 | | { |
| | 0 | 23 | | if (string.IsNullOrWhiteSpace(source)) |
| | | 24 | | { |
| | 0 | 25 | | return new List<ExternalLinkSuggestionDto>(); |
| | | 26 | | } |
| | | 27 | | |
| | 0 | 28 | | var url = $"external-links/suggestions?source={Uri.EscapeDataString(source)}&query={Uri.EscapeDataString(query ? |
| | | 29 | | |
| | 0 | 30 | | if (worldId.HasValue) |
| | | 31 | | { |
| | 0 | 32 | | url += $"&worldId={worldId.Value}"; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | try |
| | | 36 | | { |
| | 0 | 37 | | var results = await _http.GetFromJsonAsync<List<ExternalLinkSuggestionDto>>(url, ct); |
| | 0 | 38 | | return results ?? new List<ExternalLinkSuggestionDto>(); |
| | | 39 | | } |
| | 0 | 40 | | catch (OperationCanceledException) |
| | | 41 | | { |
| | 0 | 42 | | return new List<ExternalLinkSuggestionDto>(); |
| | | 43 | | } |
| | 0 | 44 | | catch (Exception ex) |
| | | 45 | | { |
| | 0 | 46 | | _logger.LogWarning(ex, "Failed to fetch external link suggestions for source {Source}", source); |
| | 0 | 47 | | return new List<ExternalLinkSuggestionDto>(); |
| | | 48 | | } |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | public async Task<ExternalLinkContentDto?> GetContentAsync( |
| | | 52 | | string source, |
| | | 53 | | string id, |
| | | 54 | | CancellationToken ct) |
| | | 55 | | { |
| | 0 | 56 | | if (string.IsNullOrWhiteSpace(source) || string.IsNullOrWhiteSpace(id)) |
| | | 57 | | { |
| | 0 | 58 | | return null; |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | var url = |
| | 0 | 62 | | $"external-links/content?source={Uri.EscapeDataString(source)}&id={Uri.EscapeDataString(id)}"; |
| | | 63 | | |
| | | 64 | | try |
| | | 65 | | { |
| | 0 | 66 | | return await _http.GetFromJsonAsync<ExternalLinkContentDto>(url, ct); |
| | | 67 | | } |
| | 0 | 68 | | catch (OperationCanceledException) |
| | | 69 | | { |
| | 0 | 70 | | return null; |
| | | 71 | | } |
| | 0 | 72 | | catch (Exception ex) |
| | | 73 | | { |
| | 0 | 74 | | _logger.LogWarning(ex, "Failed to fetch external link content for source {Source} and id {Id}", source, id); |
| | 0 | 75 | | return null; |
| | | 76 | | } |
| | 0 | 77 | | } |
| | | 78 | | } |