Skip to content

Commit

Permalink
Replace single API key with multiple to choose from
Browse files Browse the repository at this point in the history
  • Loading branch information
LArkema committed Dec 2, 2023
1 parent 725aff6 commit 7d41ca1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
14 changes: 10 additions & 4 deletions .github/workflows/compile-sketches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ jobs:
echo "version=$next_version" >> $GITHUB_OUTPUT
- name: Inject API Key
run: |
sed -i -E "/SECRET_WMATA_API_KEY/s/[0-9a-f]{32}/${{ secrets.WMATA_API_KEY }}/" ${CONFIG_FILE}
sed -i -E "/SECRET_WMATA_API_KEY/s/[0-9a-f]{32}/${{ secrets.WMATA_API_KEY }}/" ${BI_CONFIG_FILE}
for config in ${CONFIG_FILE} ${BI_CONFIG_FILE}; do
sed -i -E "/SECRET_WMATA_API_KEY_0/s/[0-9a-f]{32}/${{ secrets.WMATA_API_KEY_0 }}/" ${config}
sed -i -E "/SECRET_WMATA_API_KEY_1/s/[0-9a-f]{32}/${{ secrets.WMATA_API_KEY_1 }}/" ${config}
sed -i -E "/SECRET_WMATA_API_KEY_2/s/[0-9a-f]{32}/${{ secrets.WMATA_API_KEY_2 }}/" ${config};
done
- name: Install Arduino Cli
uses: arduino/[email protected]
- name: Install boards and libraries
Expand All @@ -83,8 +86,11 @@ jobs:
gzip -k -f ${BI_BIN_NAME}
- name: Remove WMATA API Key
run: |
sed -i "s/${{ secrets.WMATA_API_KEY }}/0123456789abcdef0123456789abcdef/" ${CONFIG_FILE}
sed -i "s/${{ secrets.WMATA_API_KEY }}/0123456789abcdef0123456789abcdef/" ${BI_CONFIG_FILE}
for config in ${CONFIG_FILE} ${BI_CONFIG_FILE}; do
sed -i "s/${{ secrets.WMATA_API_KEY_0 }}/0123456789abcdef0123456789abcdef/" ${config}
sed -i "s/${{ secrets.WMATA_API_KEY_1 }}/123456789abcdef0123456789abcdef0/" ${config}
sed -i "s/${{ secrets.WMATA_API_KEY_2 }}/2123456789abcdef0123456789abcdef/" ${config};
done
- name: Commit changes to repo
run: |
if [[ $(git status) != *"nothing to commit, working tree clean"* ]]; then
Expand Down
5 changes: 4 additions & 1 deletion DCTransistor-Bidirectional/DCTransistor-Bidirectional.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ uint8 data_failure_count; //Count failures getting live data
StaticJsonDocument<JSON_FILTER_SIZE> train_pos_filter;
const uint16_t json_size = JSON_DOC_SIZE; //space to allocate for data FROM WMATA API. Change to 3072 without Line Code, 2048 for just CircuitIDs

//Array of different API keys associated with different "default tier" products on the WMATA developer page.
String wmata_api_keys[3] = {SECRET_WMATA_API_KEY_0, SECRET_WMATA_API_KEY_1, SECRET_WMATA_API_KEY_2};

//Create objects representing each line
TrainLine* redline = new TrainLine(NUM_RD_STATIONS, rstations_0, rstations_1, RD_HEX_COLOR, rd_led_array_0, rd_led_array_1);
Expand Down Expand Up @@ -142,7 +144,8 @@ void loop() {
#endif
}

https.addHeader("api_key", SECRET_WMATA_API_KEY);
//Use one of three WMATA API Keys to stay under usage quota. Actuall randomness not important, just variance in key usage.
https.addHeader("api_key", wmata_api_keys[random(3)]); /* Flawfinder: ignore */

//Request train data from server. If unsuccessful, set LED red. If successful, deserialize the JSON data returned by the API
int httpCode = https.GET();
Expand Down
8 changes: 5 additions & 3 deletions DCTransistor-Bidirectional/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
#include <ESP8266httpUpdate.h>

//Version string. Changes with every software version
#define VERSION "1.1.2"
#define VERSION "1.1.3"

/*
* USER CONFIGURATION VALUES
*/

//If using own WMATA API key, enter here. Otherwise, one built into downloaded binary
#define SECRET_WMATA_API_KEY "0123456789abcdef0123456789abcdef"
//If using own WMATA API key, enter here (can set same key for every value). Otherwise, one built into downloaded binary
#define SECRET_WMATA_API_KEY_0 "0123456789abcdef0123456789abcdef"
#define SECRET_WMATA_API_KEY_1 "123456789abcdef0123456789abcdef0"
#define SECRET_WMATA_API_KEY_2 "2123456789abcdef0123456789abcdef"

//Whether or not to check for automatic updates every time board powers on (turning to false may break board when web TLS certificates expire)
#define AUTOUPDATE true
Expand Down
5 changes: 4 additions & 1 deletion DCTransistor/DCTransistor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ uint8_t data_failure_count; //Count API failures / empty responses
StaticJsonDocument<JSON_FILTER_SIZE> train_pos_filter;
const uint16_t json_size = JSON_DOC_SIZE; //space to allocate for data FROM WMATA API. Change to 3072 without Line Code, 2048 for just CircuitIDs

//Array of different API keys associated with different "default tier" products on the WMATA developer page.
String wmata_api_keys[3] = {SECRET_WMATA_API_KEY_0, SECRET_WMATA_API_KEY_1, SECRET_WMATA_API_KEY_2};

//Create objects representing each line
TrainLine* redline = new TrainLine(NUM_RD_STATIONS, rstations_0, rstations_1, RD_HEX_COLOR, rd_led_array);
Expand Down Expand Up @@ -127,7 +129,8 @@ void loop() {
#endif
}

https.addHeader("api_key", SECRET_WMATA_API_KEY);
//Use one of three WMATA API Keys to stay under usage quota. Actuall randomness not important, just variance in key usage.
https.addHeader("api_key", wmata_api_keys[random(3)]); /* Flawfinder: ignore */

//Request train data from server. If unsuccessful, set LED red. If successful, deserialize the JSON data returned by the API
int httpCode = https.GET();
Expand Down
8 changes: 5 additions & 3 deletions DCTransistor/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
#include <ESP8266httpUpdate.h>

//Version string. Changes with every software version
#define VERSION "1.1.2"
#define VERSION "1.1.3"

/*
* USER CONFIGURATION VALUES
*/

//If using own WMATA API key, enter here. Otherwise, one built into downloaded binary
#define SECRET_WMATA_API_KEY "0123456789abcdef0123456789abcdef"
//If using own WMATA API key, enter here (can set same key for every value). Otherwise, one built into downloaded binary
#define SECRET_WMATA_API_KEY_0 "0123456789abcdef0123456789abcdef"
#define SECRET_WMATA_API_KEY_1 "123456789abcdef0123456789abcdef0"
#define SECRET_WMATA_API_KEY_2 "2123456789abcdef0123456789abcdef"

//Whether or not to check for automatic updates every time board powers on (turning to false may break board when web TLS certificates expire)
#define AUTOUPDATE true
Expand Down

0 comments on commit 7d41ca1

Please sign in to comment.