diff --git a/server/src/lib/score-import/import-types/common/api-kai/iidx/class-handler.ts b/server/src/lib/score-import/import-types/common/api-kai/iidx/class-handler.ts index e5ae16ecc..eb0b3a2f7 100644 --- a/server/src/lib/score-import/import-types/common/api-kai/iidx/class-handler.ts +++ b/server/src/lib/score-import/import-types/common/api-kai/iidx/class-handler.ts @@ -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 { 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; diff --git a/server/src/lib/score-import/import-types/common/api-kai/iidx/parser.ts b/server/src/lib/score-import/import-types/common/api-kai/iidx/parser.ts index 12fce6a53..b97a19a7d 100644 --- a/server/src/lib/score-import/import-types/common/api-kai/iidx/parser.ts +++ b/server/src/lib/score-import/import-types/common/api-kai/iidx/parser.ts @@ -16,19 +16,21 @@ export async function ParseKaiIIDX( ): Promise> { 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", }; } diff --git a/server/src/lib/score-import/import-types/common/api-kai/sdvx/class-handler.ts b/server/src/lib/score-import/import-types/common/api-kai/sdvx/class-handler.ts index 926f9c08b..d41daae01 100644 --- a/server/src/lib/score-import/import-types/common/api-kai/sdvx/class-handler.ts +++ b/server/src/lib/score-import/import-types/common/api-kai/sdvx/class-handler.ts @@ -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 { 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; diff --git a/server/src/lib/score-import/import-types/common/api-kai/sdvx/parser.ts b/server/src/lib/score-import/import-types/common/api-kai/sdvx/parser.ts index e34a28edf..f85c177ac 100644 --- a/server/src/lib/score-import/import-types/common/api-kai/sdvx/parser.ts +++ b/server/src/lib/score-import/import-types/common/api-kai/sdvx/parser.ts @@ -16,19 +16,21 @@ export async function ParseKaiSDVX( ): Promise> { 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", }; }