From 96c320e18945166208372fa6abd13db01128c3b7 Mon Sep 17 00:00:00 2001 From: emallson Date: Tue, 24 Dec 2024 12:46:18 -0500 Subject: [PATCH] allow retrieving classic guild reports --- src/route/wcl/guild-reports.ts | 14 ++++++++++---- src/wcl/api.ts | 33 +++++++++++++++++++++++++++------ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/route/wcl/guild-reports.ts b/src/route/wcl/guild-reports.ts index a751a6a..10deb49 100644 --- a/src/route/wcl/guild-reports.ts +++ b/src/route/wcl/guild-reports.ts @@ -1,6 +1,7 @@ import { wrapEndpoint } from "./common"; import * as api from "../../wcl/api"; import { gql } from "graphql-request"; +import { GameType } from "../../wcl/api"; interface Input { id: string; @@ -69,14 +70,19 @@ const guildReports = wrapEndpoint<{ translate?: string; _?: string; start?: string; + game?: string; }>( "/i/v1/reports/guild/:name/:server/:region", "wcl-reports", async (req) => { - const rawData: QueryData = await api.query(query, { - ...req.params, - start: Number(req.query.start), - }); + const rawData: QueryData = await api.query( + query, + { + ...req.params, + start: Number(req.query.start), + }, + req.query.game === "classic" ? GameType.Classic : GameType.Retail, + ); return rawData.reportData.reports.data.map(mapReportData); }, diff --git a/src/wcl/api.ts b/src/wcl/api.ts index 45f6d66..1a587eb 100644 --- a/src/wcl/api.ts +++ b/src/wcl/api.ts @@ -6,7 +6,7 @@ async function fetchToken(): Promise { `${process.env.WCL_CLIENT_ID}:${process.env.WCL_CLIENT_SECRET}`, ).toString("base64"); const response = await axios.postForm( - `${process.env.WCL_HOST}/oauth/token`, + `https://www.${process.env.WCL_PRIMARY_DOMAIN}/oauth/token`, { grant_type: "client_credentials", }, @@ -47,17 +47,38 @@ export class ApiError extends Error { } } +export enum GameType { + Retail, + Classic, +} + +function subdomain(gameType: GameType): string { + if (gameType === GameType.Classic) { + return "classic"; + } + + return "www"; +} + export async function query( gql: string, variables: V, + gameType: GameType = GameType.Retail, ): Promise { let token = await getToken(); const run = () => - request(`${process.env.WCL_HOST}/api/v2/client`, gql, variables, { - Authorization: `Bearer ${token}`, - "Content-Type": "application/json", - "Accept-Encoding": "deflate,gzip", - }); + request( + `https://${subdomain(gameType)}.${ + process.env.WCL_PRIMARY_DOMAIN + }/api/v2/client`, + gql, + variables, + { + Authorization: `Bearer ${token}`, + "Content-Type": "application/json", + "Accept-Encoding": "deflate,gzip", + }, + ); let data; try { data = await run();