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.
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", {
method: 'POST',
body: new URLSearchParams({
@ -23,7 +23,7 @@ async function refreshApiToken(tokens: TokenPair): Promise<boolean> {
})
}).then(async (res: FetchResponse) => {
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>);
log(LogLevel.DEBUG, "Returned data:")
log(LogLevel.DEBUG, data)
@ -31,7 +31,7 @@ async function refreshApiToken(tokens: TokenPair): Promise<boolean> {
tokens.refresh_token = data.refresh_token;
return true;
} 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));
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,
// attempt to refresh the API token and retry
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 = {
"Authorization": "Bearer " + tokens.access_token,
"Client-ID": config.twitchClientId
@ -51,7 +51,7 @@ export async function apiRequest(tokens: TokenPair, endpoint: string): Promise <
if (res.status == 200) {
return res.json();
} 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,"Headers:");
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 })
.then(async (res: FetchResponse) => {
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();
} else {
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) {
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 tokenpair = streamer.tokenpair;
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
// the API token.
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", {
headers: {'Authorization': `OAuth ${tokens.access_token}`}
}).then((res: FetchResponse) => {