Skip to content

Commit

Permalink
#306 : refactor and typing for more genericity
Browse files Browse the repository at this point in the history
  • Loading branch information
guillermau committed Mar 3, 2025
1 parent 5bc65f5 commit 90da339
Show file tree
Hide file tree
Showing 18 changed files with 433 additions and 367 deletions.
7 changes: 3 additions & 4 deletions api/src/core/adapters/hal/HalAPI/getCodemetaSoftware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { HAL } from "../types/HAL";
import { HalFetchError } from "./type";
import { HAL } from "./types/HAL";

export async function fetchCodeMetaSoftwareByURL(url: string): Promise<HAL.SoftwareApplication | undefined> {
const res = await fetch(`${url}/codemeta`, {
Expand All @@ -9,7 +8,7 @@ export async function fetchCodeMetaSoftwareByURL(url: string): Promise<HAL.Softw
});

if (res === undefined) {
throw new HalFetchError(undefined);
throw new HAL.API.FetchError(undefined);
}

if (res.status === 429) {
Expand All @@ -18,7 +17,7 @@ export async function fetchCodeMetaSoftwareByURL(url: string): Promise<HAL.Softw
}

if (res.status === 404) {
throw new HalFetchError(res.status);
throw new HAL.API.FetchError(res.status);
}

const json = await res.json();
Expand Down
14 changes: 7 additions & 7 deletions api/src/core/adapters/hal/HalAPI/getDomains.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import fetch from "node-fetch";
import { HalAPIDomain, HalFetchError } from "./type";
import { HAL } from "./types/HAL";

export async function getAllDomains(): Promise<HalAPIDomain[]> {
export async function getAllDomains(): Promise<HAL.API.Domain[]> {
// Get all domains
const url = "http://api.archives-ouvertes.fr/ref/domain/?fl=*";

const res = await fetch(url).catch(err => {
console.error(err);
throw new HalFetchError(undefined);
throw new HAL.API.FetchError(undefined);
});

if (res.status === 429) {
Expand All @@ -16,21 +16,21 @@ export async function getAllDomains(): Promise<HalAPIDomain[]> {
}

if (res.status === 404) {
throw new HalFetchError(res.status);
throw new HAL.API.FetchError(res.status);
}

const json = await res.json();

return json.response.docs;
}

export async function getDomainByCode(code: string): Promise<HalAPIDomain> {
export async function getDomainByCode(code: string): Promise<HAL.API.Domain> {
// Get domain using code
const url = `http://api.archives-ouvertes.fr/ref/domain/?q=code_s:${code}&fl=*`;

const res = await fetch(url).catch(err => {
console.error(err);
throw new HalFetchError(undefined);
throw new HAL.API.FetchError(undefined);
});

if (res.status === 429) {
Expand All @@ -39,7 +39,7 @@ export async function getDomainByCode(code: string): Promise<HalAPIDomain> {
}

if (res.status === 404) {
throw new HalFetchError(res.status);
throw new HAL.API.FetchError(res.status);
}

const json = await res.json();
Expand Down
34 changes: 24 additions & 10 deletions api/src/core/adapters/hal/HalAPI/getHalSoftware.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fetch from "node-fetch";
import { HalFetchError, HalRawSoftware } from "./type";
import { HAL } from "./types/HAL";

// HAL documentation is here : https://api.archives-ouvertes.fr/docs/search

// Move to get data from API
const halSoftwareFieldsToReturn: (keyof HalRawSoftware)[] = [
const halSoftwareFieldsToReturn: (keyof HAL.API.Software)[] = [
"en_abstract_s",
"en_title_s",
"fr_abstract_s",
Expand All @@ -31,13 +31,13 @@ const halSoftwareFieldsToReturn: (keyof HalRawSoftware)[] = [

export const halSoftwareFieldsToReturnAsString = halSoftwareFieldsToReturn.join(",");

export async function fetchHalSoftwareById(halDocid: string): Promise<HalRawSoftware | undefined> {
export async function fetchHalSoftwareById(halDocid: string): Promise<HAL.API.Software | undefined> {
const res = await fetch(
`https://api.archives-ouvertes.fr/search/?q=docid:${halDocid}&wt=json&fl=${halSoftwareFieldsToReturnAsString}&sort=docid%20asc`
).catch(() => undefined);

if (res === undefined) {
throw new HalFetchError(undefined);
throw new HAL.API.FetchError(undefined);
}

if (res.status === 429) {
Expand All @@ -46,30 +46,44 @@ export async function fetchHalSoftwareById(halDocid: string): Promise<HalRawSoft
}

if (res.status === 404) {
throw new HalFetchError(res.status);
throw new HAL.API.FetchError(res.status);
}

const json = await res.json();

return json.response.docs[0];
}
type Props = {
queryString?: string;
SWHFilter?: boolean;
};

export async function fetchHalSoftwares(params: Props): Promise<Array<HAL.API.Software>> {
const { queryString, SWHFilter } = params;

let url = `https://api.archives-ouvertes.fr/search/?fq=docType_s:SOFTWARE&rows=10000&fl=${halSoftwareFieldsToReturnAsString}`;

if (queryString) {
url = url + `&q=${encodeURIComponent(queryString)}`;
}

export async function fetchHalSoftwares(): Promise<Array<HalRawSoftware>> {
// Filter only software who have an swhidId to filter clean data on https://hal.science, TODO remove and set it as an option to be generic
const url = `https://api.archives-ouvertes.fr/search/?q=docType_s:SOFTWARE&rows=10000&fl=${halSoftwareFieldsToReturnAsString}&fq=swhidId_s:["" TO *]`;
if (SWHFilter) {
url = url + `&fq=swhidId_s:["" TO *]`;
}

const res = await fetch(url).catch(err => {
console.error(err);
throw new HalFetchError(undefined);
throw new HAL.API.FetchError(undefined);
});

if (res.status === 429) {
await new Promise(resolve => setTimeout(resolve, 100));
return fetchHalSoftwares();
return fetchHalSoftwares(params);
}

if (res.status === 404) {
throw new HalFetchError(res.status);
throw new HAL.API.FetchError(res.status);
}

const json = await res.json();
Expand Down
18 changes: 9 additions & 9 deletions api/src/core/adapters/hal/HalAPI/getStructure.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HalFetchError, HalStructure } from "./type";
import { HAL } from "./types/HAL";

export const getHalStructureByAcronym = async (structureAcronym: string): Promise<HalStructure | undefined> => {
export const getHalStructureByAcronym = async (structureAcronym: string): Promise<HAL.API.Structure | undefined> => {
const url = `http://api.archives-ouvertes.fr/ref/structure/?fl=*&q=acronym_s:"${encodeURIComponent(
structureAcronym
)}"`;
Expand All @@ -9,7 +9,7 @@ export const getHalStructureByAcronym = async (structureAcronym: string): Promis
signal: AbortSignal.timeout(10000)
}).catch(err => {
console.error(err);
throw new HalFetchError(undefined);
throw new HAL.API.FetchError(undefined);
});

if (res.status === 429) {
Expand All @@ -18,27 +18,27 @@ export const getHalStructureByAcronym = async (structureAcronym: string): Promis
}

if (res.status === 404) {
throw new HalFetchError(res.status);
throw new HAL.API.FetchError(res.status);
}

const json = await res.json();

if (json.error) {
throw new HalFetchError(json.error);
throw new HAL.API.FetchError(json.error);
}

// What do to when multiple for one acronym while in code meta only reference to acronym => LIDILEM, EPFL
return json.response.docs?.[0]; // json.response.numFound === 1 ? : undefined;
};

export const getHalStructureById = async (docid: number): Promise<HalStructure | undefined> => {
export const getHalStructureById = async (docid: number): Promise<HAL.API.Structure | undefined> => {
const url = `http://api.archives-ouvertes.fr/ref/structure/?fl=*&q=docid:${docid}`;

const res = await fetch(url, {
signal: AbortSignal.timeout(10000)
}).catch(err => {
console.error(err);
throw new HalFetchError(undefined);
throw new HAL.API.FetchError(undefined);
});

if (res.status === 429) {
Expand All @@ -47,13 +47,13 @@ export const getHalStructureById = async (docid: number): Promise<HalStructure |
}

if (res.status === 404) {
throw new HalFetchError(res.status);
throw new HAL.API.FetchError(res.status);
}

const json = await res.json();

if (json.error) {
throw new HalFetchError(json.error);
throw new HAL.API.FetchError(json.error);
}

return json.response.numFound === 1 ? json.response.docs?.[0] : undefined;
Expand Down
Loading

0 comments on commit 90da339

Please sign in to comment.