Skip to content

Commit

Permalink
fix: Third fix is the charm?
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimple committed Oct 13, 2022
1 parent 9700810 commit 83b35eb
Showing 1 changed file with 43 additions and 47 deletions.
90 changes: 43 additions & 47 deletions src/scripts/destiny2/apiClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,31 @@ export class DestinyApiClient {

this.trackedGoals = [];

async function callUrl(
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE",
url: string,
body: any | null = null,
extraHeaders: any | null = null
) {
let headers = {
"X-User-Agent": "Destiny 2 Goal Tracker AppId/41664 ([email protected])",
"X-API-Key": apiToken,
};

if (extraHeaders !== null) {
headers = {
...headers,
...extraHeaders,
};
}

return await fetch(url, {
method: method,
headers: headers,
body: body,
});
}

this.checkManifestVersion = async function () {
log("D2API", "Checking manifest version");
return new Promise(async function (resolve, reject) {
Expand Down Expand Up @@ -198,10 +223,10 @@ export class DestinyApiClient {
}
}

let manifestRequest = await callUrl("GET", `${destinyApiUrl}/Destiny2/Manifest/`);
let manifestRequest = await pluginClient.GET(`${destinyApiUrl}/Destiny2/Manifest/`);

if (manifestRequest.status === 200) {
let manifest = await manifestRequest.json();
if (manifestRequest.Result.statusCode === 200) {
let manifest = JSON.parse(manifestRequest.Result.content);
if (manifest.ErrorStatus == "Success") {
db.setItem("lastManifestUpdate", Date.now());
db.setItem("manifest", JSON.stringify(manifest.Response));
Expand All @@ -215,7 +240,7 @@ export class DestinyApiClient {
return null;
}
} else {
let responseText = await manifestRequest.text();
let responseText = manifestRequest.Result.content;
log("D2API", "Error when fetching Manifest");
log("D2API", responseText);

Expand Down Expand Up @@ -299,47 +324,21 @@ export class DestinyApiClient {

eventEmitter.emit("loading-text", `Loading ${dataType.replace("Destiny", "")}`);

const contentTypeDownload = await callUrl(
"GET",
const contentTypeDownload = await pluginClient.GET(
`${destinyBaseUrl}${manifest.jsonWorldComponentContentPaths.en[dataType]}`
);

if (!contentTypeDownload.ok) {
log("Manifest download error", await contentTypeDownload.text());
if (contentTypeDownload.Result.statusCode !== 200) {
log("Manifest download error", contentTypeDownload.Result.content);
return;
}

const contentTypeJson = await contentTypeDownload.json();
const contentTypeJson = JSON.parse(contentTypeDownload.Result.content);

self.destinyDataDefinition[dataType] = contentTypeJson;
db.setItem(`destinyContent-${dataType}`, JSON.stringify(contentTypeJson));
}

async function callUrl(
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE",
url: string,
body: any | null = null,
extraHeaders: any | null = null
) {
let headers = {
"X-User-Agent": "Destiny 2 Goal Tracker AppId/41664 ([email protected])",
"X-API-Key": apiToken,
};

if (extraHeaders !== null) {
headers = {
...headers,
...extraHeaders,
};
}

return await fetch(url, {
method: method,
headers: headers,
body: body,
});
}

/**
* Returns the authentication URL needed to authenticate with Bungie.net.
* @returns {string} Authentication URL that the user needs to visit to authenticated with Bungie.net.
Expand All @@ -366,12 +365,13 @@ export class DestinyApiClient {
return null;
}

let tokenRequest = await callUrl("POST", `${authGatewayUrl}/token/destiny2`, JSON.stringify({ code: code }), {
"Content-Type": "application/json",
});
let tokenRequest = await pluginClient.POSTJson(
`${authGatewayUrl}/token/destiny2`,
JSON.stringify({ code: code })
);

if (tokenRequest.status === 200) {
let tokenResponse = await tokenRequest.json();
if (tokenRequest.Result.statusCode === 200) {
let tokenResponse = JSON.parse(tokenRequest.Result.content);

handleTokenResponse(tokenResponse);
return tokenResponse;
Expand All @@ -387,19 +387,15 @@ export class DestinyApiClient {
return null;
}

let tokenRequest = await callUrl(
"POST",
let tokenRequest = await pluginClient.POSTJson(
`${authGatewayUrl}/refresh/destiny2`,
JSON.stringify({
refresh_token: refreshToken,
}),
{
"Content-Type": "application/json;charset=UTF-8",
}
})
);

if (tokenRequest.status === 200) {
let tokenResponse = await tokenRequest.json();
if (tokenRequest.Result.statusCode === 200) {
let tokenResponse = JSON.parse(tokenRequest.Result.content);

handleTokenResponse(tokenResponse);
eventEmitter.emit("destiny2-auth-refreshed");
Expand Down

0 comments on commit 83b35eb

Please sign in to comment.