| | | 1 | | using System.Net.Http.Json; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using LifeChart.Application.DTOs; |
| | | 4 | | using LifeChart.Application.Interfaces; |
| | | 5 | | |
| | | 6 | | namespace LifeChart.Infrastructure.CrisisResources; |
| | | 7 | | |
| | | 8 | | // API-Dokumentation: https://findahelpline.com |
| | | 9 | | // Endpunkt wird bei Bedarf angepasst wenn offizielle Docs verfügbar sind. |
| | | 10 | | public class FindAHelplineService : ICrisisResourceService |
| | | 11 | | { |
| | | 12 | | private readonly HttpClient _httpClient; |
| | | 13 | | |
| | 0 | 14 | | public FindAHelplineService(HttpClient httpClient) |
| | | 15 | | { |
| | 0 | 16 | | _httpClient = httpClient; |
| | 0 | 17 | | _httpClient.BaseAddress = new Uri("https://findahelpline.com"); |
| | 0 | 18 | | _httpClient.Timeout = TimeSpan.FromSeconds(5); |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | public async Task<IEnumerable<CrisisResourceDto>> GetForRegionAsync(string regionCode) |
| | | 22 | | { |
| | 0 | 23 | | var response = await _httpClient.GetFromJsonAsync<FindAHelplineResponse>( |
| | 0 | 24 | | $"/api/v1/organizations?countryCode={regionCode}"); |
| | | 25 | | |
| | 0 | 26 | | if (response?.Organizations is null) |
| | 0 | 27 | | return []; |
| | | 28 | | |
| | 0 | 29 | | return response.Organizations.Select(o => new CrisisResourceDto( |
| | 0 | 30 | | o.Name ?? string.Empty, |
| | 0 | 31 | | o.PhoneNumber ?? string.Empty, |
| | 0 | 32 | | o.Url, |
| | 0 | 33 | | o.Available24h)); |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | private sealed class FindAHelplineResponse |
| | | 37 | | { |
| | | 38 | | [JsonPropertyName("organizations")] |
| | 0 | 39 | | public List<FindAHelplineOrganization>? Organizations { get; set; } |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | private sealed class FindAHelplineOrganization |
| | | 43 | | { |
| | | 44 | | [JsonPropertyName("name")] |
| | 0 | 45 | | public string? Name { get; set; } |
| | | 46 | | |
| | | 47 | | [JsonPropertyName("phone")] |
| | 0 | 48 | | public string? PhoneNumber { get; set; } |
| | | 49 | | |
| | | 50 | | [JsonPropertyName("url")] |
| | 0 | 51 | | public string? Url { get; set; } |
| | | 52 | | |
| | | 53 | | [JsonPropertyName("available_24h")] |
| | 0 | 54 | | public bool Available24h { get; set; } |
| | | 55 | | } |
| | | 56 | | } |