From 18c9082e4408d0240e0a42652ada6bf75aec5d04 Mon Sep 17 00:00:00 2001 From: gregor <gregor.schulz@holi.social> Date: Fri, 21 Mar 2025 13:34:42 +0100 Subject: [PATCH] resolve city names --- app/providers/geo/GeoApiClient.test.ts | 8 ++++++++ app/providers/geo/GeoApiClient.ts | 19 +++++++++++++++++-- app/providers/geo/GeoApiClient.types.ts | 1 + 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/app/providers/geo/GeoApiClient.test.ts b/app/providers/geo/GeoApiClient.test.ts index d5f4211..a9fa64b 100644 --- a/app/providers/geo/GeoApiClient.test.ts +++ b/app/providers/geo/GeoApiClient.test.ts @@ -40,4 +40,12 @@ describe('GeoAPIClient', () => { lon: 10.000654, }) }) + + it('fetches city name of place id', async () => { + using _ = fakeFetch(aPlaceDetailsReponse) + + const response = await client.resolveCityName('somePlaceId') + + assertEquals(response, 'Hamburg') + }) }) diff --git a/app/providers/geo/GeoApiClient.ts b/app/providers/geo/GeoApiClient.ts index ed5d564..8eadc1c 100644 --- a/app/providers/geo/GeoApiClient.ts +++ b/app/providers/geo/GeoApiClient.ts @@ -5,7 +5,8 @@ export interface ResolvesCoordinates { } export class GeoApiClient implements ResolvesCoordinates { - constructor(private readonly endpointUrl: string) {} + constructor(private readonly endpointUrl: string) { + } async resolveCoordinates( geolocationId: string, @@ -16,7 +17,21 @@ export class GeoApiClient implements ResolvesCoordinates { return { lat, lon } } else { throw new Deno.errors.NotCapable( - `Resolution of lat/lon failed (no data included in response for geolocationId=${geolocationId})`, + `Resolution of lat/lon failed (no data in response for geolocationId=${geolocationId})`, + ) + } + } + + async resolveCityName( + geolocationId: string, + ): Promise<string> { + const response = await this.fetchPlaceDetails(geolocationId) + const { city } = response.data?.placeDetails?.geolocation?.properties || {} + if (city) { + return city + } else { + throw new Deno.errors.NotCapable( + `Resolution of city name failed (no data in response for geolocationId=${geolocationId})`, ) } } diff --git a/app/providers/geo/GeoApiClient.types.ts b/app/providers/geo/GeoApiClient.types.ts index d8153f7..2832290 100644 --- a/app/providers/geo/GeoApiClient.types.ts +++ b/app/providers/geo/GeoApiClient.types.ts @@ -10,6 +10,7 @@ export type GeolocationCoordinates = { export type GeolocationProperties = { formatted: string + city: string } & GeolocationCoordinates export type GeoAPIResponse = { -- GitLab