Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from superagent to native fetch #222

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading