Skip to content

Commit 3d0108c

Browse files
committed
addProvider(provider) proposal
1 parent 1dbe0f4 commit 3d0108c

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/index.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,56 @@ export class Optimade {
1515
this.providersUrl = this.wrapUrl(providersUrl);
1616
}
1717

18+
async addProvider(provider: Types.Provider) {
19+
if (!this.apis[provider.id]) this.apis[provider.id] = [];
20+
21+
try {
22+
const ver = provider.attributes
23+
&& provider.attributes.api_version ?
24+
provider.attributes.api_version.charAt(0) : '';
25+
const api = await this.getApis(provider, ver ? `v${ver}` : '');
26+
27+
if (api.attributes.available_endpoints.includes('structures')) {
28+
this.apis[provider.id].push(api);
29+
}
30+
31+
} catch (ignore) { }
32+
33+
if (!provider.attributes.query_limits) {
34+
const formula = `chemical_formula_anonymous="A2B"`;
35+
const url = `${provider.attributes.base_url}/v1/structures?filter=${formula}&page_limit=1000`;
36+
37+
try {
38+
const res = await fetch(url).then(res => res.json());
39+
const version = res.meta && res.meta.api_version || this.apis[provider.id][0].attributes.api_version;
40+
const detail = (errors: { detail: any; }[]) => {
41+
return errors
42+
? errors.length
43+
? errors[0].detail
44+
: errors.detail
45+
: '10';
46+
};
47+
const limits = detail(res.errors)
48+
.match(/\d+/g)
49+
.filter((number: string) => +number < 1000)
50+
.map((number: string) => +number);
51+
52+
provider.attributes = {
53+
...provider.attributes,
54+
api_version: version,
55+
query_limits: limits
56+
}
57+
58+
} catch (error) {
59+
console.log(error);
60+
}
61+
}
62+
63+
this.providers[provider.id] = provider
64+
65+
return this.providers;
66+
}
67+
1868
async getProviders(api?: Types.Api): Promise<Types.ProvidersMap | null> {
1969
const providers: Types.ProvidersResponse | null = await (api ?
2070
this.followLinks(api).catch(() => null) :

0 commit comments

Comments
 (0)