Skip to content

Commit

Permalink
DCJ-654: Standardize metrics token usage (#2668)
Browse files Browse the repository at this point in the history
  • Loading branch information
rushtong authored Sep 10, 2024
1 parent 647b1d1 commit da48cf9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/libs/acknowledgements.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const acknowledgementStorageKey = (ackKey) => {

export const hasAccepted = async (...acknowledgements) => {

// check if the acknowledgements are int he cache
// check if the acknowledgements are in the cache
const allAcknowledgementsInStorage = acknowledgements.every(
(ackKey) => Storage.getCurrentUserSettings(acknowledgementStorageKey(ackKey)) || false
);
Expand Down
24 changes: 14 additions & 10 deletions src/libs/ajax/Metrics.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import axios from 'axios';
import { getDefaultProperties } from '@databiosphere/bard-client';
import {getDefaultProperties} from '@databiosphere/bard-client';

import { Storage } from '../storage';
import { getBardApiUrl } from '../ajax';
import {Storage} from '../storage';
import {getBardApiUrl} from '../ajax';
import {Token} from '../config';

export const Metrics = {
captureEvent: (event, details, signal) => captureEventFn(event, details, signal).catch(() => { }),
captureEvent: (event, details, signal) => captureEventFn(event, details, signal).catch(() => {
}),
syncProfile: (signal) => syncProfile(signal),
identify: (anonId, signal) => identify(anonId, signal),
};
Expand Down Expand Up @@ -42,7 +44,7 @@ const captureEventFn = async (event, details = {}, signal) => {
method: 'POST',
url: `${await getBardApiUrl()}/api/event`,
data: body,
headers: isRegistered ? { Authorization: `Bearer ${Storage.getGoogleData()?.accessToken}` } : undefined,
headers: isRegistered ? {Authorization: `Bearer ${Token.getToken()}`} : undefined,
signal,
};

Expand All @@ -59,11 +61,12 @@ const syncProfile = async (signal) => {
const config = {
method: 'POST',
url: `${await getBardApiUrl()}/api/syncProfile`,
headers: { Authorization: `Bearer ${Storage.getGoogleData()?.accessToken}` },
headers: {Authorization: `Bearer ${Token.getToken()}`},
signal,
};

return axios(config).catch(() => { });
return axios(config).catch(() => {
});
};

/**
Expand All @@ -74,17 +77,18 @@ const syncProfile = async (signal) => {
* @returns {Promise} - A Promise that resolves when the user is identified.
*/
const identify = async (anonId, signal) => {
const body = { anonId };
const body = {anonId};

const config = {
method: 'POST',
url: `${await getBardApiUrl()}/api/identify`,
data: body,
headers: { Authorization: `Bearer ${Storage.getGoogleData()?.accessToken}` },
headers: {Authorization: `Bearer ${Token.getToken()}`},
signal,
};

return axios(config).catch(() => { });
return axios(config).catch(() => {
});
};


20 changes: 1 addition & 19 deletions src/libs/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ export const Config = {

getTag: async () => (await getConfig()).tag,

getFeatureFlag: async (featureName) => {
const feature = _.get(await getConfig(), 'features', {});
return _.get(feature, featureName, false);
},

getProject: async () => {
const env = await Config.getEnv();
switch (env) {
Expand Down Expand Up @@ -65,13 +60,6 @@ export const Config = {
},
}),

fileOpts: (token = Token.getToken()) => ({
headers: {
Authorization: `Bearer ${token}`,
Accept: 'application/json',
},
}),

jsonBody: body => ({
body: JSON.stringify(body),
headers: {'Content-Type': 'application/json'},
Expand All @@ -82,15 +70,9 @@ export const Config = {
headers: {'Content-Type': 'application/binary'}
}),

fileBody: (token = Token.getToken()) => ({
headers: {
Authorization: `Bearer ${token}`,
Accept: '*/*',
},
}),
};

const Token = {
export const Token = {
getToken: () => {
return Storage.getGoogleData() !== null ?
Storage.getGoogleData().accessToken :
Expand Down

0 comments on commit da48cf9

Please sign in to comment.