Skip to content

Commit

Permalink
allow retrieving classic guild reports
Browse files Browse the repository at this point in the history
  • Loading branch information
emallson committed Dec 24, 2024
1 parent 41c64ca commit 96c320e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
14 changes: 10 additions & 4 deletions src/route/wcl/guild-reports.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
},
Expand Down
33 changes: 27 additions & 6 deletions src/wcl/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ async function fetchToken(): Promise<string | undefined> {
`${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",
},
Expand Down Expand Up @@ -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<T, V extends Variables>(
gql: string,
variables: V,
gameType: GameType = GameType.Retail,
): Promise<T> {
let token = await getToken();
const run = () =>
request<T>(`${process.env.WCL_HOST}/api/v2/client`, gql, variables, {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
"Accept-Encoding": "deflate,gzip",
});
request<T>(
`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();
Expand Down

0 comments on commit 96c320e

Please sign in to comment.