-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_api.ts
More file actions
214 lines (198 loc) · 5.72 KB
/
client_api.ts
File metadata and controls
214 lines (198 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* client_api.ts provides the browser side API for interactive dataset API via the COLD web service.
* That in turn wraps the datasetd JSON API via ts_dataset.ts.
*/
/**
* ClientAPI wraps the browser facing web service and handles retrieving information about peoples, groups, etc.
*/
export class ClientAPI {
baseUrl: string = "../";
constructor(baseUrl?: string) {
(baseUrl === undefined) ? "" : this.baseUrl = baseUrl;
}
joinUrlPath(baseUrl: string | URL, path: string): string {
// Convert baseUrl to a URL object if it's a string
const url = typeof baseUrl === "string" ? new URL(baseUrl) : baseUrl;
// Remove leading slashes from the path to avoid double slashes
const normalizedPath = path.replace(/^\/+/, "");
// Combine the base URL's pathname with the normalized path
const combinedPath = `${url.pathname}/${normalizedPath}`.replace(
/\/\/+/g,
"/",
);
// Construct the new URL
const newUrl = new URL(url.origin + combinedPath);
return newUrl.toString();
}
async getStringList(
c_name: string,
query_name: string,
params?: URLSearchParams,
): Promise<string[]> {
const base_url = this.joinUrlPath(
this.baseUrl,
`/api/${c_name}/${query_name}`,
);
let uri: string = base_url;
let resp: Response;
if (params !== undefined) {
uri = `${base_url}?${params}`;
}
try {
resp = await fetch(uri, {
headers: { "Content-Type": "application/json" },
method: "GET",
});
} catch (err) {
return [];
}
if (resp.ok) {
const src = await resp.text();
if (src !== undefined && src !== "") {
let l: string[] = [];
try {
l = JSON.parse(src);
} catch (err) {
return [];
}
return l;
}
}
return [];
}
async getList(
c_name: string,
query_name: string,
params?: URLSearchParams,
): Promise<{ [key: string]: any }[]> {
const base_url = this.joinUrlPath(
this.baseUrl,
`/api/${c_name}/${query_name}`,
);
let uri: string = base_url;
let resp: Response;
if (params !== undefined) {
uri = `${base_url}?${params}`;
}
try {
resp = await fetch(uri, {
headers: { "Content-Type": "application/json" },
method: "GET",
});
} catch (err) {
return [];
}
if (resp.ok) {
const src = await resp.text();
if (src !== undefined && src !== "") {
let l: { clgid: string; group_name: string }[] = [];
try {
l = JSON.parse(src);
} catch (err) {
return [];
}
return l;
}
}
return [];
}
/**
* getGroupsList returns an array of clgid and group names. If list can't be retrieved
* then an empty list is return.
* @returns an array of objects consisting of clgid and group name.
*/
async getGroupsList(): Promise<{ clgid: string; group_name: string }[]> {
const c_name = "groups";
const query_name = "group_names";
return await this.getList(c_name, query_name) as {
clgid: string;
group_name: string;
}[];
}
/**
* getPeopleList returns an array of clpid and group names. If list can't be retrieved
* then an empty list is return.
* @returns an array of objects consisting of clgid and group name.
*/
async getPeopleList(): Promise<
{ clpid: string; family_name: string; given_name: string; orcid: string }[]
> {
const c_name = "people";
const query_name = "people_names";
return await this.getList(c_name, query_name) as {
clpid: string;
family_name: string;
given_name: string;
orcid: string;
}[];
}
async lookupGroupName(name: string): Promise<
{ clgid: string; name: string; ok: boolean; msg: string }[]
> {
const c_name = "groups.ds";
const query_name = "lookup_name";
let params = new URLSearchParams();
params.append("q", name);
//params.append("clgid", name);
//params.append("alternative", name);
return await this.getList(c_name, query_name, params) as {
clgid: string;
name: string;
ok: boolean;
msg: string;
}[];
}
async lookupGroupMembership(clgid: string): Promise<
{ clgid: string; name: string; ok: boolean; msg: string }[]
> {
const c_name = "people.ds";
const query_name = "lookup_clgid";
let params = new URLSearchParams();
params.append("q", clgid);
return await this.getList(c_name, query_name, params) as {
clgid: string;
name: string;
ok: boolean;
msg: string;
}[];
}
async getROR(ror: string): Promise<{ [key: string]: any }> {
const c_name = "ror.ds";
const query_name = "get_ror";
let params = new URLSearchParams();
params.append("q", ror);
return await this.getList(c_name, query_name, params) as {
obj: { [key: string]: any };
ok: boolean;
msg: string;
}[];
}
async lookupRORByName(
funder_name: string,
): Promise<{ ror: string; name: string; ok: boolean; msg: string }[]> {
const c_name = "ror.ds";
const query_name = "lookup_ror_by_name";
let params = new URLSearchParams();
params.append("q", funder_name);
return await this.getList(c_name, query_name, params) as {
ror: string;
name: string;
ok: boolean;
msg: string;
}[];
}
async lookupRORByAcronym(
acronym: string,
): Promise<{ ror: string; name: string; ok: boolean; msg: string }[]> {
const c_name = "ror.ds";
const query_name = "lookup_ror_by_acronym";
let params = new URLSearchParams();
params.append("q", acronym);
return await this.getList(c_name, query_name, params) as {
ror: string;
name: string;
ok: boolean;
msg: string;
}[];
}
}