fix: make kai-type-apis not spontaneously fail to sync classes

reauthing *did not* happen in the case where class-searching
failed. It happened when the iterator is called,
which is after the classHandler is curried.
This commit is contained in:
zkldi
2022-11-07 21:01:47 +00:00
parent a8e7c23444
commit 33a3efe5b0
4 changed files with 44 additions and 6 deletions
@@ -2,11 +2,13 @@ import { KaiTypeToBaseURL } from "../utils";
import { IIDXDans } from "lib/constants/classes";
import nodeFetch from "utils/fetch";
import { IsRecord } from "utils/misc";
import type { KaiAPIReauthFunction } from "../traverse-api";
import type { ClassHandler } from "lib/score-import/framework/user-game-stats/types";
export async function CreateKaiIIDXClassHandler(
kaiType: "EAG" | "FLO",
token: string,
reauthFn: KaiAPIReauthFunction,
fetch = nodeFetch
): Promise<ClassHandler> {
let json: unknown;
@@ -16,13 +18,31 @@ export async function CreateKaiIIDXClassHandler(
// SP and DP dans are located in the same place,
// fetch once, then return a function that traverses this data.
try {
const res = await fetch(`${baseUrl}/api/iidx/v2/player_profile`, {
let res = await fetch(`${baseUrl}/api/iidx/v2/player_profile`, {
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
});
// if we failed auth wise. Try reauthing.
if (res.status === 401 || res.status === 403) {
const newToken = await reauthFn();
res = await fetch(`${baseUrl}/api/sdvx/v1/player_profile`, {
headers: {
Authorization: `Bearer ${newToken}`,
"Content-Type": "application/json",
},
});
}
if (res.status !== 200) {
const text = await res.text();
throw new Error(`Got unexpected status from ${kaiType}: ${res.status}. Body: ${text}`);
}
json = (await res.json()) as unknown;
} catch (e: unknown) {
err = e;
@@ -16,19 +16,21 @@ export async function ParseKaiIIDX(
): Promise<ParserFunctionReturns<unknown, KaiContext>> {
const baseUrl = KaiTypeToBaseURL(service);
const reauthFn = CreateKaiReauthFunction(service, authDoc, logger, fetch);
return {
iterable: TraverseKaiAPI(
baseUrl,
"/api/iidx/v2/play_history",
authDoc.token,
logger,
CreateKaiReauthFunction(service, authDoc, logger, fetch),
reauthFn,
fetch
),
context: {
service,
},
classHandler: await CreateKaiIIDXClassHandler(service, authDoc.token, fetch),
classHandler: await CreateKaiIIDXClassHandler(service, authDoc.token, reauthFn, fetch),
game: "iidx",
};
}
@@ -2,11 +2,13 @@ import { KaiTypeToBaseURL } from "../utils";
import { SDVXDans } from "lib/constants/classes";
import nodeFetch from "utils/fetch";
import { IsRecord } from "utils/misc";
import type { KaiAPIReauthFunction } from "../traverse-api";
import type { ClassHandler } from "lib/score-import/framework/user-game-stats/types";
export async function CreateKaiSDVXClassHandler(
kaiType: "EAG" | "FLO" | "MIN",
token: string,
reauthFn: KaiAPIReauthFunction,
fetch = nodeFetch
): Promise<ClassHandler> {
let json: unknown;
@@ -14,13 +16,25 @@ export async function CreateKaiSDVXClassHandler(
const baseUrl = KaiTypeToBaseURL(kaiType);
try {
const res = await fetch(`${baseUrl}/api/sdvx/v1/player_profile`, {
let res = await fetch(`${baseUrl}/api/sdvx/v1/player_profile`, {
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
});
// if we failed auth wise. Try reauthing.
if (res.status === 401 || res.status === 403) {
const newToken = await reauthFn();
res = await fetch(`${baseUrl}/api/sdvx/v1/player_profile`, {
headers: {
Authorization: `Bearer ${newToken}`,
"Content-Type": "application/json",
},
});
}
json = (await res.json()) as unknown;
} catch (e: unknown) {
err = e;
@@ -16,19 +16,21 @@ export async function ParseKaiSDVX(
): Promise<ParserFunctionReturns<unknown, KaiContext>> {
const baseUrl = KaiTypeToBaseURL(service);
const reauthFn = CreateKaiReauthFunction(service, authDoc, logger, fetch);
return {
iterable: TraverseKaiAPI(
baseUrl,
"/api/sdvx/v1/play_history",
authDoc.token,
logger,
CreateKaiReauthFunction(service, authDoc, logger, fetch),
reauthFn,
fetch
),
context: {
service,
},
classHandler: await CreateKaiSDVXClassHandler(service, authDoc.token, fetch),
classHandler: await CreateKaiSDVXClassHandler(service, authDoc.token, reauthFn, fetch),
game: "sdvx",
};
}