Specify source of log messages in twitch.ts

master
Dessa Simpson 2020-11-09 16:24:34 -07:00
parent 6071cf0d8f
commit c3079a198d
1 changed files with 8 additions and 8 deletions

View File

@ -12,7 +12,7 @@ export interface TokenPair {
// Refresh the API token. Returns true on success and false on failure. // Refresh the API token. Returns true on success and false on failure.
async function refreshApiToken(tokens: TokenPair): Promise<boolean> { async function refreshApiToken(tokens: TokenPair): Promise<boolean> {
log(LogLevel.DEBUG,`Call: refreshApiToken(${JSON.stringify(tokens,null,2)})`); log(LogLevel.DEBUG,`Call: twitch.refreshApiToken(${JSON.stringify(tokens,null,2)})`);
return fetch("https://id.twitch.tv/oauth2/token", { return fetch("https://id.twitch.tv/oauth2/token", {
method: 'POST', method: 'POST',
body: new URLSearchParams({ body: new URLSearchParams({
@ -23,7 +23,7 @@ async function refreshApiToken(tokens: TokenPair): Promise<boolean> {
}) })
}).then(async (res: FetchResponse) => { }).then(async (res: FetchResponse) => {
if (res.status == 200) { if (res.status == 200) {
log(LogLevel.INFO,"Refresh returned success."); log(LogLevel.INFO,"twitch.refreshApiToken: Refresh returned success.");
var data = await (res.json() as Promise<TokenPair>); var data = await (res.json() as Promise<TokenPair>);
log(LogLevel.DEBUG, "Returned data:") log(LogLevel.DEBUG, "Returned data:")
log(LogLevel.DEBUG, data) log(LogLevel.DEBUG, data)
@ -31,7 +31,7 @@ async function refreshApiToken(tokens: TokenPair): Promise<boolean> {
tokens.refresh_token = data.refresh_token; tokens.refresh_token = data.refresh_token;
return true; return true;
} else { } else {
log(LogLevel.ERROR,"Refresh returned failure. Response object:"); log(LogLevel.ERROR,"twitch.refreshApiToken: Refresh returned failure. Response object:");
log(LogLevel.ERROR,JSON.stringify(await res.json(),null,2)); log(LogLevel.ERROR,JSON.stringify(await res.json(),null,2));
return false; return false;
} }
@ -41,7 +41,7 @@ async function refreshApiToken(tokens: TokenPair): Promise<boolean> {
// Send an API request. On success, return the specified data. On failure, // Send an API request. On success, return the specified data. On failure,
// attempt to refresh the API token and retry // attempt to refresh the API token and retry
export async function apiRequest(tokens: TokenPair, endpoint: string): Promise <any> { export async function apiRequest(tokens: TokenPair, endpoint: string): Promise <any> {
log(LogLevel.DEBUG,`Call: apiRequest(${JSON.stringify(tokens,null,2)},${endpoint})`); log(LogLevel.DEBUG,`Call: twitch.apiRequest(${JSON.stringify(tokens,null,2)},${endpoint})`);
var headers = { var headers = {
"Authorization": "Bearer " + tokens.access_token, "Authorization": "Bearer " + tokens.access_token,
"Client-ID": config.twitchClientId "Client-ID": config.twitchClientId
@ -51,7 +51,7 @@ export async function apiRequest(tokens: TokenPair, endpoint: string): Promise <
if (res.status == 200) { if (res.status == 200) {
return res.json(); return res.json();
} else { } else {
log(LogLevel.WARNING,"Failed API request (pre-refresh):"); log(LogLevel.WARNING,"twitch.apiRequest: Failed API request (pre-refresh):");
log(LogLevel.WARNING,"Request URL: https://api.twitch.tv/helix" + endpoint); log(LogLevel.WARNING,"Request URL: https://api.twitch.tv/helix" + endpoint);
log(LogLevel.WARNING,"Headers:"); log(LogLevel.WARNING,"Headers:");
log(LogLevel.WARNING,JSON.stringify(headers,null,2)); log(LogLevel.WARNING,JSON.stringify(headers,null,2));
@ -66,7 +66,7 @@ export async function apiRequest(tokens: TokenPair, endpoint: string): Promise <
return fetch("https://api.twitch.tv/helix" + endpoint, { headers: headers }) return fetch("https://api.twitch.tv/helix" + endpoint, { headers: headers })
.then(async (res: FetchResponse) => { .then(async (res: FetchResponse) => {
if (res.status == 200) { if (res.status == 200) {
log(LogLevel.WARNING,"API call succeeded after token refresh.") log(LogLevel.WARNING,"twitch.apiRequest: API call succeeded after token refresh.")
return res.json(); return res.json();
} else { } else {
log(LogLevel.ERROR,"Failed API request:"); log(LogLevel.ERROR,"Failed API request:");
@ -86,7 +86,7 @@ export async function apiRequest(tokens: TokenPair, endpoint: string): Promise <
} }
export async function streamerApiRequest(endpoint: string) { export async function streamerApiRequest(endpoint: string) {
log(LogLevel.DEBUG,`Call: streamerApiRequest(${endpoint})`); log(LogLevel.DEBUG,`Call: twitch.streamerApiRequest(${endpoint})`);
var streamer = await db.query(queries.getStreamerIdToken).then((result: pg.QueryResult) => result.rows[0]); var streamer = await db.query(queries.getStreamerIdToken).then((result: pg.QueryResult) => result.rows[0]);
var tokenpair = streamer.tokenpair; var tokenpair = streamer.tokenpair;
var originaltoken = tokenpair.access_token; var originaltoken = tokenpair.access_token;
@ -101,7 +101,7 @@ export async function streamerApiRequest(endpoint: string) {
// success, return true. If failure, return the result of attempting to refresh // success, return true. If failure, return the result of attempting to refresh
// the API token. // the API token.
export async function isApiTokenValid(tokens: TokenPair) { export async function isApiTokenValid(tokens: TokenPair) {
log(LogLevel.DEBUG,`Call: isApiTokenValid(${JSON.stringify(tokens,null,2)})`); log(LogLevel.DEBUG,`Call: twitch.isApiTokenValid(${JSON.stringify(tokens,null,2)})`);
return fetch("https://id.twitch.tv/oauth2/validate", { return fetch("https://id.twitch.tv/oauth2/validate", {
headers: {'Authorization': `OAuth ${tokens.access_token}`} headers: {'Authorization': `OAuth ${tokens.access_token}`}
}).then((res: FetchResponse) => { }).then((res: FetchResponse) => {