diff options
| author | Ariel Costas Guerrero <ariel@costas.dev> | 2026-03-21 00:36:25 +0100 |
|---|---|---|
| committer | Ariel Costas Guerrero <ariel@costas.dev> | 2026-03-21 00:36:25 +0100 |
| commit | f1b0b5f7ceaf6d23ae347e12cf29eef617c7dc9b (patch) | |
| tree | d92c6075d019d87fd03482774050d7fc8981a5aa /src/Enmarcha.Backend/Services/Geocoding | |
| parent | 2ef155c0c68208eccf968919fea12133698b50a9 (diff) | |
feat: enhance geocoding and stop search with performance metrics and improved response handling
Diffstat (limited to 'src/Enmarcha.Backend/Services/Geocoding')
| -rw-r--r-- | src/Enmarcha.Backend/Services/Geocoding/GeoapifyGeocodingService.cs | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/src/Enmarcha.Backend/Services/Geocoding/GeoapifyGeocodingService.cs b/src/Enmarcha.Backend/Services/Geocoding/GeoapifyGeocodingService.cs index ce86c49..515613a 100644 --- a/src/Enmarcha.Backend/Services/Geocoding/GeoapifyGeocodingService.cs +++ b/src/Enmarcha.Backend/Services/Geocoding/GeoapifyGeocodingService.cs @@ -53,9 +53,17 @@ public class GeoapifyGeocodingService : IGeocodingService try { - var response = await _httpClient.GetFromJsonAsync<GeoapifyResult>(url + $"&apiKey={_config.GeoapifyApiKey}"); - + var httpResponse = await _httpClient.GetAsync(url + $"&apiKey={_config.GeoapifyApiKey}"); + if (!httpResponse.IsSuccessStatusCode) + { + var body = await httpResponse.Content.ReadAsStringAsync(); + _logger.LogWarning("Geoapify autocomplete returned {StatusCode} for query '{Query}': {Body}", + (int)httpResponse.StatusCode, query, body); + activity?.SetTag("http.status_code", (int)httpResponse.StatusCode); + return []; + } + var response = await httpResponse.Content.ReadFromJsonAsync<GeoapifyResult>(); var results = response?.results .Where(x => !ForbiddenResultTypes.Contains(x.result_type)) .Select(MapToPlannerSearchResult) @@ -69,7 +77,7 @@ public class GeoapifyGeocodingService : IGeocodingService { activity?.SetStatus(System.Diagnostics.ActivityStatusCode.Error, ex.Message); _logger.LogError(ex, "Error fetching Geoapify autocomplete results from {Url}", url); - return new List<PlannerSearchResult>(); + return []; } } @@ -90,8 +98,17 @@ public class GeoapifyGeocodingService : IGeocodingService $"https://api.geoapify.com/v1/geocode/reverse?lat={lat.ToString(CultureInfo.InvariantCulture)}&lon={lon.ToString(CultureInfo.InvariantCulture)}&lang=gl&format=json"; try { - var response = await _httpClient.GetFromJsonAsync<GeoapifyResult>(url + $"&apiKey={_config.GeoapifyApiKey}"); + var httpResponse = await _httpClient.GetAsync(url + $"&apiKey={_config.GeoapifyApiKey}"); + if (!httpResponse.IsSuccessStatusCode) + { + var body = await httpResponse.Content.ReadAsStringAsync(); + _logger.LogWarning("Geoapify reverse geocode returned {StatusCode} for ({Lat},{Lon}): {Body}", + (int)httpResponse.StatusCode, lat, lon, body); + activity?.SetTag("http.status_code", (int)httpResponse.StatusCode); + return null; + } + var response = await httpResponse.Content.ReadFromJsonAsync<GeoapifyResult>(); if (response == null) return null; var result = MapToPlannerSearchResult(response.results[0]); |
