Skip to content

Commit 75a9f01

Browse files
committed
fix: 🐛 Fixed authenticated fetches for sennet and hubmap
1 parent 1b05914 commit 75a9f01

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

src/hubmap/downloader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ export class Downloader extends XConsortiaDownloader {
2424
async getMetadataLookup(ids) {
2525
const organMetadata = await OrganMetadataCollection.load(this.config);
2626
const metadata = await getMetadata(ids, this.searchUrl, this.token, ID_KEYWORD, METADATA_FIELDS);
27-
return await metadataToLookup(metadata, organMetadata);
27+
return await metadataToLookup(metadata, organMetadata, this.token);
2828
}
2929
}

src/hubmap/metadata.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ export const METADATA_FIELDS = [
4242
* @param {object} result Raw metadata
4343
* @param {OrganMetadataCollection} organMetadata Organ metadata
4444
*/
45-
export async function metadataToLookup(result, organMetadata) {
45+
export async function metadataToLookup(result, organMetadata, token = undefined) {
46+
// Headers to use in authenticated fetch requests
47+
const headers = {};
48+
if (token) {
49+
headers['Authorization'] = `Bearer ${token}`;
50+
}
51+
4652
/** @type {Map<string, HubmapMetadata>} */
4753
const lookup = new Map();
4854
for (const hit of result.hits.hits) {
@@ -66,7 +72,13 @@ export async function metadataToLookup(result, organMetadata) {
6672
},
6773
},
6874
} = hit;
69-
const ancestors = await fetch(`${HUBMAP_ANCESTORS_ENDPOINT}${uuid}`).then((r) => r.json());
75+
76+
const ancestors = await fetch(`${HUBMAP_ANCESTORS_ENDPOINT}${uuid}`, { headers }).then((r) => r.json());
77+
if (ancestors.error) {
78+
console.error(`Error getting ancestors for ${uuid}: ${ancestors.error}`);
79+
throw new Error(ancestors.error);
80+
}
81+
7082
const mapped_organ = organMetadata.resolve(ORGAN_MAPPING[organ.toUpperCase()]?.organ_id ?? '');
7183
const { block_id, rui_location } = getSampleBlockId(ancestors, HUBMAP_ENTITY_ENDPOINT);
7284
lookup.set(hubmap_id, {

src/sennet/downloader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ export class Downloader extends XConsortiaDownloader {
2424
async getMetadataLookup(ids) {
2525
const organMetadata = await OrganMetadataCollection.load(this.config);
2626
const metadata = await getMetadata(ids, this.searchUrl, this.token, ID_KEYWORD, METADATA_FIELDS);
27-
return await toLookup(metadata, organMetadata);
27+
return await toLookup(metadata, organMetadata, this.token);
2828
}
2929
}

src/sennet/metadata.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ export const METADATA_FIELDS = [
3939
* @param {object} result Raw metadata
4040
* @param {OrganMetadataCollection} organMetadata Organ metadata
4141
*/
42-
export async function toLookup(result, organMetadata) {
42+
export async function toLookup(result, organMetadata, token = undefined) {
43+
// Headers to use in authenticated fetch requests
44+
const headers = {};
45+
if (token) {
46+
headers['Authorization'] = `Bearer ${token}`;
47+
}
48+
4349
/** @type {Map<string, SennetMetadata>} */
4450
const lookup = new Map();
4551
for (const hit of result.hits.hits) {
@@ -54,18 +60,23 @@ export async function toLookup(result, organMetadata) {
5460
sources: [{ uuid: donor_uuid }],
5561
},
5662
} = hit;
57-
const source = await fetch(`${SENNET_ENTITY_ENDPOINT}${donor_uuid}`).then((r) => r.json());
63+
const source = await fetch(`${SENNET_ENTITY_ENDPOINT}${donor_uuid}`, { headers }).then((r) => r.json());
5864
const {
5965
source_mapped_metadata: {
60-
age: { value: [donor_age] = [''] },
61-
race: { value: [donor_race] = [''] },
62-
sex: { value: [donor_sex] = [''] },
63-
body_mass_index: { value: [donor_bmi] = [''] },
64-
},
66+
age: { value: [donor_age] = [''] } = {},
67+
race: { value: [donor_race] = [''] } = {},
68+
sex: { value: [donor_sex] = [''] } = {},
69+
body_mass_index: { value: [donor_bmi] = [''] } = {},
70+
} = {},
6571
} = source;
66-
const ancestors = await fetch(`${SENNET_ANCESTORS_ENDPOINT}${uuid}`).then((r) => r.json());
67-
const mapped_organ = organMetadata.resolve(ORGAN_MAPPING[organ.toUpperCase()]?.organ_id ?? '');
6872

73+
const ancestors = await fetch(`${SENNET_ANCESTORS_ENDPOINT}${uuid}`, { headers }).then((r) => r.json());
74+
if (ancestors.error) {
75+
console.error(`Error getting ancestors for ${uuid}: ${ancestors.error}`);
76+
throw new Error(ancestors.error);
77+
}
78+
79+
const mapped_organ = organMetadata.resolve(ORGAN_MAPPING[organ.toUpperCase()]?.organ_id ?? '');
6980
const { block_id, rui_location } = getSampleBlockId(ancestors, SENNET_ENTITY_ENDPOINT);
7081
lookup.set(sennet_id, {
7182
organ: mapped_organ,

0 commit comments

Comments
 (0)