From cf940a9abcf90a0d423579204c85d92712702b63 Mon Sep 17 00:00:00 2001 From: Luke Blaney Date: Thu, 5 Jan 2023 21:39:17 +0000 Subject: [PATCH] TFL have removed `app_id` from their API calls. Therefore, we can simplify the environment varibles needed here and just have a single one for `app_key`. --- .gitignore | 3 ++- README.md | 6 +++--- docker-compose.yaml | 3 +-- src/fetchers/tfl.js | 5 ++--- src/sources/tfl-unified.js | 4 +--- 5 files changed, 9 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 4c2696d..ce9c065 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules npm-debug.log bin -reports \ No newline at end of file +reports +.env \ No newline at end of file diff --git a/README.md b/README.md index 4d52ed8..92684d6 100644 --- a/README.md +++ b/README.md @@ -34,11 +34,11 @@ To run unit tests using [ava](https://github.com/avajs/ava), run: * ```npm start``` You should also set the following environment variables: -TFLAPPID and TFLAPPKEY - information about TFL's API can be found at https://api-portal.tfl.gov.uk/docs -The app appears to run fine without these being set, but registering for an ID and key helps to keep track of which apps are making which requests to the API. +TFL_KEY - generated here: https://api-portal.tfl.gov.uk/profile +The app appears to run fine without these being set, but registering for a key helps to keep track of which apps are making which requests to the API and increases the limit of requests that can be made. ## Running using docker compose -`TFLAPPID= TFLAPPKEY= nice -19 docker-compose up -d --no-build` +`TFL_APP= nice -19 docker-compose up -d --no-build` ## Building The build is configured to run in Dockerhub when a commit is pushed to the master branch in github. \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 81e2b91..35f24d3 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -6,7 +6,6 @@ services: ports: - "3000:3000" environment: - - TFLAPPID - - TFLAPPKEY + - TFL_KEY image: lucas42/tfluke_app restart: always \ No newline at end of file diff --git a/src/fetchers/tfl.js b/src/fetchers/tfl.js index 8b09440..1969aad 100644 --- a/src/fetchers/tfl.js +++ b/src/fetchers/tfl.js @@ -16,11 +16,10 @@ function tflapireq(path) { } else { url += '?'; } - url += "app_id=" - if (process.env.TFLAPPID) url += encodeURIComponent(process.env.TFLAPPID); url += "&app_key="; - if (process.env.TFLAPPKEY) url += encodeURIComponent(process.env.TFLAPPKEY); + if (process.env.TFL_KEY) url += encodeURIComponent(process.env.TFL_KEY); return fetch(url, {timeout: 800}).then(response => { + if (response.status == 429) throw new Error(`TFL API Rate Limited`); if (response.status != 200) throw new Error(`Unexpected status code ${response.status}`); return response.json().then(data => { return { diff --git a/src/sources/tfl-unified.js b/src/sources/tfl-unified.js index 6ade971..d6d4729 100644 --- a/src/sources/tfl-unified.js +++ b/src/sources/tfl-unified.js @@ -20,10 +20,8 @@ function tflapireq(path, callback) { } else { url += '?'; } - url += "app_id=" - if (process.env.TFLAPPID) url += encodeURIComponent(process.env.TFLAPPID); url += "&app_key="; - if (process.env.TFLAPPKEY) url += encodeURIComponent(process.env.TFLAPPKEY); + if (process.env.TFL_KEY) url += encodeURIComponent(process.env.TFLAPPKEY); req(url, function (err, resp, rawbody) { if (err) { console.error(err);