Skip to content

Commit

Permalink
fix: 🐛 Fixed authenticated fetches for sennet and hubmap
Browse files Browse the repository at this point in the history
  • Loading branch information
bherr2 committed Sep 20, 2024
1 parent 1b05914 commit 75a9f01
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/hubmap/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ export class Downloader extends XConsortiaDownloader {
async getMetadataLookup(ids) {
const organMetadata = await OrganMetadataCollection.load(this.config);
const metadata = await getMetadata(ids, this.searchUrl, this.token, ID_KEYWORD, METADATA_FIELDS);
return await metadataToLookup(metadata, organMetadata);
return await metadataToLookup(metadata, organMetadata, this.token);
}
}
16 changes: 14 additions & 2 deletions src/hubmap/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ export const METADATA_FIELDS = [
* @param {object} result Raw metadata
* @param {OrganMetadataCollection} organMetadata Organ metadata
*/
export async function metadataToLookup(result, organMetadata) {
export async function metadataToLookup(result, organMetadata, token = undefined) {
// Headers to use in authenticated fetch requests
const headers = {};
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}

/** @type {Map<string, HubmapMetadata>} */
const lookup = new Map();
for (const hit of result.hits.hits) {
Expand All @@ -66,7 +72,13 @@ export async function metadataToLookup(result, organMetadata) {
},
},
} = hit;
const ancestors = await fetch(`${HUBMAP_ANCESTORS_ENDPOINT}${uuid}`).then((r) => r.json());

const ancestors = await fetch(`${HUBMAP_ANCESTORS_ENDPOINT}${uuid}`, { headers }).then((r) => r.json());
if (ancestors.error) {
console.error(`Error getting ancestors for ${uuid}: ${ancestors.error}`);
throw new Error(ancestors.error);
}

const mapped_organ = organMetadata.resolve(ORGAN_MAPPING[organ.toUpperCase()]?.organ_id ?? '');
const { block_id, rui_location } = getSampleBlockId(ancestors, HUBMAP_ENTITY_ENDPOINT);
lookup.set(hubmap_id, {
Expand Down
2 changes: 1 addition & 1 deletion src/sennet/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ export class Downloader extends XConsortiaDownloader {
async getMetadataLookup(ids) {
const organMetadata = await OrganMetadataCollection.load(this.config);
const metadata = await getMetadata(ids, this.searchUrl, this.token, ID_KEYWORD, METADATA_FIELDS);
return await toLookup(metadata, organMetadata);
return await toLookup(metadata, organMetadata, this.token);
}
}
29 changes: 20 additions & 9 deletions src/sennet/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ export const METADATA_FIELDS = [
* @param {object} result Raw metadata
* @param {OrganMetadataCollection} organMetadata Organ metadata
*/
export async function toLookup(result, organMetadata) {
export async function toLookup(result, organMetadata, token = undefined) {
// Headers to use in authenticated fetch requests
const headers = {};
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}

/** @type {Map<string, SennetMetadata>} */
const lookup = new Map();
for (const hit of result.hits.hits) {
Expand All @@ -54,18 +60,23 @@ export async function toLookup(result, organMetadata) {
sources: [{ uuid: donor_uuid }],
},
} = hit;
const source = await fetch(`${SENNET_ENTITY_ENDPOINT}${donor_uuid}`).then((r) => r.json());
const source = await fetch(`${SENNET_ENTITY_ENDPOINT}${donor_uuid}`, { headers }).then((r) => r.json());
const {
source_mapped_metadata: {
age: { value: [donor_age] = [''] },
race: { value: [donor_race] = [''] },
sex: { value: [donor_sex] = [''] },
body_mass_index: { value: [donor_bmi] = [''] },
},
age: { value: [donor_age] = [''] } = {},
race: { value: [donor_race] = [''] } = {},
sex: { value: [donor_sex] = [''] } = {},
body_mass_index: { value: [donor_bmi] = [''] } = {},
} = {},
} = source;
const ancestors = await fetch(`${SENNET_ANCESTORS_ENDPOINT}${uuid}`).then((r) => r.json());
const mapped_organ = organMetadata.resolve(ORGAN_MAPPING[organ.toUpperCase()]?.organ_id ?? '');

const ancestors = await fetch(`${SENNET_ANCESTORS_ENDPOINT}${uuid}`, { headers }).then((r) => r.json());
if (ancestors.error) {
console.error(`Error getting ancestors for ${uuid}: ${ancestors.error}`);
throw new Error(ancestors.error);
}

const mapped_organ = organMetadata.resolve(ORGAN_MAPPING[organ.toUpperCase()]?.organ_id ?? '');
const { block_id, rui_location } = getSampleBlockId(ancestors, SENNET_ENTITY_ENDPOINT);
lookup.set(sennet_id, {
organ: mapped_organ,
Expand Down

0 comments on commit 75a9f01

Please sign in to comment.