Skip to content

Commit

Permalink
Switch from superagent to native fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
bhollis committed Aug 31, 2024
1 parent be17d34 commit d2435ae
Show file tree
Hide file tree
Showing 5 changed files with 302 additions and 332 deletions.
49 changes: 30 additions & 19 deletions api/routes/auth-token.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as Sentry from '@sentry/node';
import { ServerResponse, UserMembershipData } from 'bungie-api-ts/user';
import asyncHandler from 'express-async-handler';
import superagent from 'superagent';
import util from 'util';
import { AuthTokenRequest, AuthTokenResponse } from '../shapes/auth.js';

Expand Down Expand Up @@ -30,12 +29,34 @@ export const authTokenHandler = asyncHandler(async (req, res) => {

// make request to bungie
try {
const bungieResponse = await superagent
.get('https://www.bungie.net/Platform/User/GetMembershipsForCurrentUser/')
.set('X-API-Key', apiApp.bungieApiKey)
.set('Authorization', `Bearer ${bungieAccessToken}`);
const bungieResponse = await fetch(
'https://www.bungie.net/Platform/User/GetMembershipsForCurrentUser/',
{
headers: {
'X-API-Key': apiApp.bungieApiKey,
Authorization: `Bearer ${bungieAccessToken}`,
},
},
);

if (!bungieResponse.ok) {
// TODO: try/catch
const errorBody = await bungieResponse.json();
if (errorBody.ErrorStatus == 'WebAuthRequired') {
metrics.increment('authToken.webAuthRequired.count');
res.status(401).send({
error: 'WebAuthRequired',
message: `Bungie.net token is not valid`,
});
return;
} else {
throw new Error(
`Error from Bungie.net while verifying token: ${errorBody.ErrorStatus}: ${errorBody.Message}`,
);
}
}

const responseData = bungieResponse.body as ServerResponse<UserMembershipData>;
const responseData = (await bungieResponse.json()) as ServerResponse<UserMembershipData>;

const serverMembershipId = responseData.Response.bungieNetUser.membershipId;
if (serverMembershipId === membershipId) {
Expand Down Expand Up @@ -96,18 +117,8 @@ export const authTokenHandler = asyncHandler(async (req, res) => {
});
}
} catch (e) {
if (e.response && e.response.body.ErrorStatus == 'WebAuthRequired') {
metrics.increment('authToken.webAuthRequired.count');
res.status(401).send({
error: 'WebAuthRequired',
message: `Bungie.net token is not valid`,
});
} else {
Sentry.captureException(e);
console.error('Error issuing auth token', e);
throw new Error(
`Error from Bungie.net while verifying token: ${e.response?.body.ErrorStatus}: ${e.response?.body.Message}`,
);
}
Sentry.captureException(e);
console.error('Error issuing auth token', e);
throw e;
}
});
9 changes: 4 additions & 5 deletions api/routes/donate.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import asyncHandler from 'express-async-handler';
import superagent from 'superagent';

const donationUrl = 'https://bungiefoundation.donordrive.com/api/1.3/participants/22881';

// Temporary proxy for donor drive API
export const donateHandler = asyncHandler(async (_req, res) => {
try {
const response = await superagent.get(donationUrl);
if (response.statusCode >= 400) {
throw new Error(`Got status code ${response.statusCode}`);
const response = await fetch(donationUrl);
if (response.status >= 400) {
throw new Error(`Got status code ${response.status}`);
}
// Cache successful responses for 15 minutes in CF
res.set('Cache-Control', 'max-age=900');
res.send(response.body);
res.send(await response.text());
} catch (e) {
res.set('Cache-Control', 'max-age=60');
res.status(500);
Expand Down
Loading

0 comments on commit d2435ae

Please sign in to comment.