Skip to content

Commit

Permalink
refactor: clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
miles-grant-ibigroup committed Feb 9, 2023
1 parent 6abadd1 commit d6aba06
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
55 changes: 28 additions & 27 deletions handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
const BugsnagPluginAwsLambda = require('@bugsnag/plugin-aws-lambda')
const {
BUGSNAG_NOTIFIER_KEY,
CUSTOM_PELIAS_URL,
CSV_ENABLED,
GEOCODE_EARTH_URL,
GEOCODER,
GEOCODER_API_KEY,
Expand All @@ -51,17 +51,20 @@ if (redis) redis.on('error', (err) => console.log('Redis Client Error', err))

// Ensure env variables have been set
if (
typeof TRANSIT_GEOCODER !== 'string' ||
typeof TRANSIT_BASE_URL !== 'string' ||
typeof GEOCODER_API_KEY !== 'string' ||
typeof BUGSNAG_NOTIFIER_KEY !== 'string' ||
typeof GEOCODER !== 'string'
) {
throw new Error(
'Error: configuration variables not found! Ensure env.yml has been decrypted'
'Error: required configuration variables not found! Ensure env.yml has been decrypted'
)
}

if (CSV_ENABLED === "true" && TRANSIT_GEOCODER === 'OTP') {
throw new Error("Error: Invalid configuration. OTP Geocoder does not support CSV_ENABLED.")
}

Bugsnag.start({
apiKey: BUGSNAG_NOTIFIER_KEY,
appType: 'pelias-stitcher-lambda-function',
Expand All @@ -87,10 +90,14 @@ const getPrimaryGeocoder = () => {
}

const getTransitGeocoder = () => {
return getGeocoder({
baseUrl: TRANSIT_BASE_URL,
type: TRANSIT_GEOCODER
})
if (TRANSIT_GEOCODER === 'OTP') {
return getGeocoder({
baseUrl: TRANSIT_BASE_URL,
type: TRANSIT_GEOCODER
})
}

return null
}

const getSecondaryGeocoder = () => {
Expand Down Expand Up @@ -141,8 +148,7 @@ export const makeGeocoderRequests = async (
delete peliasQSP.layers

// Run both requests in parallel
let [primaryResponse, transitResponse, customResponse]: [
FeatureCollection,
let [primaryResponse, customResponse]: [
FeatureCollection,
FeatureCollection
] = await Promise.all([
Expand All @@ -153,20 +159,18 @@ export const makeGeocoderRequests = async (
// @ts-expect-error Redis Typescript types are not friendly
redis
),
cachedGeocoderRequest(
getTransitGeocoder(),
'autocomplete',
convertQSPToGeocoderArgs(event.queryStringParameters),
// @ts-expect-error Redis Typescript types are not friendly
redis
),
// Should the custom Pelias instance need to be replaced with something different
// this is where it should be replaced
fetchPelias(
CUSTOM_PELIAS_URL,
apiMethod,
`${new URLSearchParams(peliasQSP).toString()}&sources=pelias`
)
// Custom request is either through geocoder package or "old" pelias method
getTransitGeocoder() ? cachedGeocoderRequest(
getTransitGeocoder(),
'autocomplete',
convertQSPToGeocoderArgs(event.queryStringParameters),
null
) : fetchPelias(
TRANSIT_BASE_URL,
apiMethod,
`${new URLSearchParams(peliasQSP).toString()}&sources=transit${
CSV_ENABLED && CSV_ENABLED === 'true' ? ',pelias' : ''
}`)
])

// If the primary response doesn't contain responses or the responses are not satisfactory,
Expand All @@ -187,10 +191,7 @@ export const makeGeocoderRequests = async (

const mergedResponse = mergeResponses({
customResponse,
primaryResponse: mergeResponses({
customResponse: transitResponse,
primaryResponse
})
primaryResponse
})
return {
body: JSON.stringify(mergedResponse),
Expand Down
6 changes: 3 additions & 3 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ provider:
- ${self:custom.secrets.LAMBDA_EXEC_SUBNET}
environment:
GEOCODER: ${self:custom.secrets.GEOCODER}
TRANSIT_GEOCODER: ${self:custom.secrets.TRANSIT_GEOCODER}
TRANSIT_GEOCODER: ${self:custom.secrets.TRANSIT_GEOCODER, null}
TRANSIT_BASE_URL: ${self:custom.secrets.TRANSIT_BASE_URL}
# Pelias instance of Geocode.Earth, with street and landmarks
GEOCODE_EARTH_URL: ${self:custom.secrets.GEOCODE_EARTH_URL, null}
GEOCODER_API_KEY: ${self:custom.secrets.GEOCODER_API_KEY, null}
# Pelias instance that is self-hosted, with GTFS stops and CSV POIs
CUSTOM_PELIAS_URL: ${self:custom.secrets.CUSTOM_PELIAS_URL, null}
# Used to logging to Bugsnag
BUGSNAG_NOTIFIER_KEY: ${self:custom.secrets.BUGSNAG_NOTIFIER_KEY}
REDIS_HOST: ${self:custom.secrets.REDIS_HOST, null}
REDIS_KEY: ${self:custom.secrets.REDIS_KEY, null}
# Used to enable CSV source
CSV_ENABLED: ${self:custom.secrets.CSV_ENABLED, false}
# Secondary Geocoder config
SECONDARY_GEOCODER: ${self:custom.secrets.SECONDARY_GEOCODER, null}
SECONDARY_GEOCODER_API_KEY: ${self:custom.secrets.SECONDARY_GEOCODER_API_KEY, null}
Expand Down

0 comments on commit d6aba06

Please sign in to comment.