Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split PWS ID and API key into 2 parameters #72

Merged
merged 1 commit into from
Jul 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion routes/adjustmentMethods/AdjustmentMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export interface AdjustmentMethodResponse {
}

export interface AdjustmentOptions {
/** Information about the PWS to use in the format "pws:API_KEY@PWS_ID". */
/** The ID of the PWS to use, prefixed with "pws:". */
pws?: string;
/** The API key to use to access PWS data. */
key?: string;
}
44 changes: 20 additions & 24 deletions routes/weather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,29 @@ export const getWateringData = async function( req: express.Request, res: expres
let timeData: TimeData = getTimeData( coordinates );

// Parse the PWS information.
const pwsString: string | undefined = adjustmentOptions.pws;
let pws: PWS | undefined = undefined;
if ( pwsString ) {
try {
pws = parsePWS( pwsString );
} catch ( err ) {
res.send( `Error: ${ err }` );
if ( adjustmentOptions.pws ) {
if ( !adjustmentOptions.key ) {
res.send("Error: An API key must be provided when using a PWS.");
return;
}

const idMatch = adjustmentOptions.pws.match( /^pws:([a-zA-Z\d]+)$/ );
const pwsId = idMatch ? idMatch[ 1 ] : undefined;
const keyMatch = adjustmentOptions.key.match( /^[a-f\d]{32}$/ );
const apiKey = keyMatch ? keyMatch[ 0 ] : undefined;

// Make sure that the PWS ID and API key look valid.
if ( !pwsId ) {
res.send("Error: PWS ID does not appear to be valid.");
return;
}
if ( !apiKey ) {
res.send("Error: PWS API key does not appear to be valid.");
return;
}

pws = { id: pwsId, apiKey: apiKey };
}

const weatherProvider = pws ? PWS_WEATHER_PROVIDER : WEATHER_PROVIDER;
Expand Down Expand Up @@ -474,21 +488,3 @@ function getParameter( parameter: string | string[] ): string {
// Return an empty string if the parameter is undefined.
return parameter || "";
}

/**
* Creates a PWS object from a string.
* @param pwsString Information about the PWS in the format "pws:API_KEY@PWS_ID".
* @return The PWS specified by the string.
* @throws Throws an error message if the string is in an invalid format and cannot be parsed.
*/
function parsePWS( pwsString: string): PWS {
const match = pwsString.match( /^pws:([a-f\d]{32})@([a-zA-Z\d]+)$/ );
if ( !match ) {
throw "Invalid PWS format.";
}

return {
apiKey: match[ 1 ],
id: match[ 2 ]
};
}