learn-request-queue/src/twitch.ts

115 lines
4.3 KiB
TypeScript
Raw Normal View History

2020-07-05 18:46:41 +00:00
import * as config from "./config";
2020-09-19 18:24:39 +00:00
import * as queries from "./queries";
2020-09-19 18:23:44 +00:00
import { log, LogLevel } from "./logging"
2020-07-05 18:46:41 +00:00
import fetch, { Response as FetchResponse } from "node-fetch";
2020-09-19 18:24:39 +00:00
import pg from "pg";
import db from "./db";
2020-07-05 18:46:41 +00:00
export interface TokenPair {
2020-07-05 18:46:41 +00:00
access_token: string;
refresh_token: string;
}
// Refresh the API token. Returns true on success and false on failure.
async function refreshApiToken(tokens: TokenPair): Promise<boolean> {
2020-09-19 18:23:44 +00:00
log(LogLevel.DEBUG,`Call: refreshApiToken(${JSON.stringify(tokens,null,2)})`);
return fetch("https://id.twitch.tv/oauth2/token", {
method: 'POST',
body: new URLSearchParams({
client_id: config.twitchClientId,
client_secret: config.twitchSecret,
grant_type: "refresh_token",
refresh_token: tokens.refresh_token
})
}).then(async (res: FetchResponse) => {
if (res.status == 200) {
2020-09-19 18:23:44 +00:00
log(LogLevel.INFO,"Refresh returned success.");
var data = await (res.json() as Promise<TokenPair>);
2020-09-19 18:23:44 +00:00
log(LogLevel.DEBUG, "Returned data:")
log(LogLevel.DEBUG, data)
tokens.access_token = data.access_token;
tokens.refresh_token = data.refresh_token;
return true;
} else {
2020-09-19 18:23:44 +00:00
log(LogLevel.ERROR,"Refresh returned failure. Response object:");
log(LogLevel.ERROR,JSON.stringify(await res.json(),null,2));
return false;
}
})
2020-07-05 18:46:41 +00:00
}
// Send an API request. On success, return the specified data. On failure,
// attempt to refresh the API token and retry
export async function apiRequest(tokens: TokenPair, endpoint: string): Promise <any> {
2020-09-19 18:23:44 +00:00
log(LogLevel.DEBUG,`Call: apiRequest(${JSON.stringify(tokens,null,2)},${endpoint})`);
2020-07-05 18:46:41 +00:00
var headers = {
"Authorization": "Bearer " + tokens.access_token,
"Client-ID": config.twitchClientId
};
return fetch("https://api.twitch.tv/helix" + endpoint, { headers: headers })
2020-09-19 18:24:39 +00:00
.then(async (res: FetchResponse) => {
if (res.status == 200) {
return res.json();
} else {
2020-09-19 18:23:44 +00:00
log(LogLevel.WARNING,"Failed API request (pre-refresh):");
log(LogLevel.WARNING,"Request URL: https://api.twitch.tv/helix" + endpoint);
log(LogLevel.WARNING,"Headers:");
log(LogLevel.WARNING,JSON.stringify(headers,null,2));
log(LogLevel.WARNING,"Response:");
log(LogLevel.WARNING,JSON.stringify(await res.json(),null,2));
log(LogLevel.WARNING,"Attempting refresh");
2020-09-19 18:24:39 +00:00
if (await refreshApiToken(tokens)) {
headers = {
"Authorization": "Bearer " + tokens.access_token,
"Client-ID": config.twitchClientId
};
return fetch("https://api.twitch.tv/helix" + endpoint, { headers: headers })
2020-09-15 05:34:17 +00:00
.then(async (res: FetchResponse) => {
if (res.status == 200) {
2020-09-19 18:23:44 +00:00
log(LogLevel.WARNING,"API call succeeded after token refresh.")
return res.json();
} else {
2020-09-19 18:23:44 +00:00
log(LogLevel.ERROR,"Failed API request:");
log(LogLevel.ERROR,"Request URL: https://api.twitch.tv/helix" + endpoint);
log(LogLevel.ERROR,"Headers:");
log(LogLevel.ERROR,JSON.stringify(headers,null,2));
log(LogLevel.ERROR,"Response:");
log(LogLevel.ERROR,JSON.stringify(await res.json(),null,2));
return false;
}
})
} else {
return false;
}
}
})
}
2020-09-19 18:24:39 +00:00
export async function streamerApiRequest(endpoint: string) {
log(LogLevel.DEBUG,`Call: streamerApiRequest(${endpoint})`);
var streamer = await db.query(queries.getStreamerIdToken).then((result: pg.QueryResult) => result.rows[0]);
var tokenpair = streamer.tokenpair;
var originaltoken = tokenpair.access_token;
log(LogLevel.DEBUG,"Original token: " + tokenpair.access_token);
var response = await apiRequest(tokenpair,endpoint);
log(LogLevel.DEBUG,"New token: " + tokenpair.access_token);
if (tokenpair.access_token != originaltoken) await db.query(Object.assign(queries.updateStreamer,{ values: [streamer.userid,tokenpair] }))
return response;
}
// Check if API token is valid. Checks Twitch's OAuth validation endpoint. If
// success, return true. If failure, return the result of attempting to refresh
// the API token.
export async function isApiTokenValid(tokens: TokenPair) {
2020-09-19 18:23:44 +00:00
log(LogLevel.DEBUG,`Call: isApiTokenValid(${JSON.stringify(tokens,null,2)})`);
return fetch("https://id.twitch.tv/oauth2/validate", {
headers: {'Authorization': `OAuth ${tokens.access_token}`}
}).then((res: FetchResponse) => {
if (res.status == 200) {
return true;
} else {
return refreshApiToken(tokens);
}
})
2020-07-05 18:46:41 +00:00
}