-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GeocoderWidget support for LDS-based gecoding.
- Loading branch information
Showing
11 changed files
with
285 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { API_VERSIONS } from '@deck.gl/carto'; | ||
import { ldsGeocode } from '../../src/api/lds'; | ||
|
||
const sampleCredentialsV3 = { | ||
apiVersion: API_VERSIONS.V3, | ||
accessToken: 'thisIsTheTestToken', | ||
apiBaseUrl: 'https://api.com/' | ||
}; | ||
|
||
const someCoordinates = { | ||
latitude: 42.360278, | ||
longitude: -71.057778 | ||
}; | ||
|
||
describe('lds', () => { | ||
describe('ldsDecode', () => { | ||
test('should send proper requests ', async () => { | ||
const fetchMock = (global.fetch = jest.fn().mockImplementation(async () => { | ||
return { | ||
ok: true, | ||
json: async () => [{ value: [someCoordinates] }] | ||
}; | ||
})); | ||
|
||
const abortController = new AbortController(); | ||
expect( | ||
await ldsGeocode({ | ||
credentials: sampleCredentialsV3, | ||
address: 'boston', | ||
country: 'US', | ||
limit: 4, | ||
opts: { | ||
abortController: abortController | ||
} | ||
}) | ||
).toEqual([someCoordinates]); | ||
|
||
expect(fetchMock).toBeCalledWith( | ||
'https://api.com//v3/lds/geocoding/geocode?address=boston&country=US&limit=4', | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${sampleCredentialsV3.accessToken}` | ||
}, | ||
signal: abortController.signal | ||
} | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { dealWithApiError } from './common'; | ||
|
||
/** | ||
* Execute a LDS geocoding service geocode request. | ||
* | ||
* @param { object } props | ||
* @param { string } props.address - searched address to be executed | ||
* @param { string= } props.country - optional, limit search scope to country as ISO-3166 alpha-2 code, example, ES, DE | ||
* @param { number= } props.limit - optional, limit of ansewers | ||
* @param { Object } props.credentials - CARTO user credentials | ||
* @param { string } props.credentials.accessToken - CARTO 3 access token | ||
* @param { string } props.credentials.apiBaseUrl - CARTO 3 api server URL | ||
* @param { Object= } props.opts - Additional options for the HTTP request | ||
*/ | ||
export async function ldsGeocode({ credentials, address, country, limit, opts }) { | ||
let response; | ||
|
||
if (!credentials || !credentials.apiBaseUrl || !credentials.accessToken) { | ||
throw new Error('ldsGeocode: Missing or bad credentials provided'); | ||
} | ||
if (!address) { | ||
throw new Error('ldsGeocode: No address provided'); | ||
} | ||
|
||
const url = new URL(`${credentials.apiBaseUrl}/v3/lds/geocoding/geocode`); | ||
url.searchParams.set('address', address); | ||
if (country) { | ||
url.searchParams.set('country', country); | ||
} | ||
if (limit) { | ||
url.searchParams.set('limit', String(limit)); | ||
} | ||
|
||
const { abortController, ...otherOptions } = opts || {}; | ||
|
||
try { | ||
response = await fetch(url.toString(), { | ||
headers: { | ||
Authorization: `Bearer ${credentials.accessToken}` | ||
}, | ||
signal: abortController?.signal, | ||
...otherOptions | ||
}); | ||
} catch (error) { | ||
if (error.name === 'AbortError') throw error; | ||
|
||
throw new Error(`Failed to connect to LDS API: ${error}`); | ||
} | ||
|
||
let data = await response.json(); | ||
|
||
if (Array.isArray(data)) { | ||
data = data[0]; | ||
} | ||
if (!response.ok) { | ||
dealWithApiError({ credentials, response, data }); | ||
} | ||
|
||
return data.value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/react-widgets/__tests__/models/GeocodingModel.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { geocodeStreetPoint } from '../../src/models/GeocodingModel'; | ||
import { API_VERSIONS } from '@deck.gl/carto'; | ||
import { ldsGeocode, executeSQL } from '@carto/react-api/'; | ||
// import * as cartoReactApi from '@carto/react-api'; | ||
|
||
const sampleCredentialsV2 = { | ||
apiVersion: API_VERSIONS.V2 | ||
}; | ||
const sampleCredentialsV3 = { | ||
apiVersion: API_VERSIONS.V3, | ||
accessToken: 'token', | ||
apiBaseUrl: 'https://api.com/' | ||
}; | ||
|
||
// const executeSqlMock = jest.spyOn(cartoReactApi, 'executeSQL'); | ||
// const ldsGeocodeMock = jest.spyOn(cartoReactApi, 'ldsGeocode'); | ||
|
||
const bostonCoordinates = { | ||
latitude: 42.360278, | ||
longitude: -71.057778 | ||
}; | ||
|
||
jest.mock('@carto/react-api', () => ({ | ||
executeSQL: jest.fn().mockImplementation(async () => global.executeSqlMockResult), | ||
ldsGeocode: jest.fn().mockImplementation(async () => global.ldsGeocodeMockResult) | ||
})); | ||
|
||
describe('geocodeStreetPoint', () => { | ||
test('executes correct SQL for V2 credentials', async () => { | ||
global.executeSqlMockResult = [ | ||
{ | ||
geometry: JSON.stringify({ | ||
coordinates: [bostonCoordinates.longitude, bostonCoordinates.latitude] | ||
}) | ||
} | ||
]; | ||
|
||
const result = await geocodeStreetPoint(sampleCredentialsV2, { | ||
searchText: 'boston', | ||
country: 'US' | ||
}); | ||
|
||
expect(result).toEqual(bostonCoordinates); | ||
|
||
expect(executeSQL).toHaveBeenCalledWith({ | ||
credentials: sampleCredentialsV2, | ||
query: | ||
"SELECT ST_AsGeoJSON(cdb_geocode_street_point('boston', '', '', 'US')) AS geometry", | ||
opts: {} | ||
}); | ||
}); | ||
|
||
test('uses ldsDecode for V3 credentials', async () => { | ||
global.ldsGeocodeMockResult = [bostonCoordinates]; | ||
const result = await geocodeStreetPoint(sampleCredentialsV3, { | ||
searchText: 'boston' | ||
}); | ||
|
||
expect(result).toEqual(bostonCoordinates); | ||
|
||
expect(ldsGeocode).toHaveBeenCalledWith({ | ||
credentials: sampleCredentialsV3, | ||
address: 'boston', | ||
limit: 1, | ||
opts: {} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.