AccessionService.ts 12 KB
Newer Older
1
2
3

import * as UrlTemplate from 'url-template';
import * as QueryString from 'query-string';
4
import { axiosBackend } from 'utilities/requestUtils';
5

6
7
8
9
import Accession from 'model/accession/Accession';
import AccessionDetails from 'model/accession/AccessionDetails';
import AccessionFilter from 'model/accession/AccessionFilter';
import AccessionMapInfo from 'model/accession/AccessionMapInfo';
10
import FilteredPage, { IPageRequest } from 'model/FilteredPage';
11
import {AccessionRef} from 'model/accession/AccessionRef';
12
import AccessionAuditLog from 'model/accession/AccessionAuditLog';
13
import AccessionSuggestionPage from 'model/accession/AccessionSuggestionPage';
Maxym Borodenko's avatar
Maxym Borodenko committed
14
import LabelValue from 'model/LabelValue';
15

16
const URL_GET_BY_DOI = `/api/v1/acn/{doi}`; // UrlTemplate doesn't like the / in DOI
17
18
const URL_UUIDS_FROM_ACCE_NUMBERS = `/api/v1/acn/acce-number`;
const URL_UUID_FROM_ACCE_NUMBER = UrlTemplate.parse(`/api/v1/acn/acce-number/{acceNumber}`);
19
const URL_GET_BY_UUID = UrlTemplate.parse(`/api/v1/acn/{uuid}`);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
20
const URL_GEO_JSON = `/api/v1/acn/geoJson`;
Maxym Borodenko's avatar
Maxym Borodenko committed
21
const URL_AUTOCOMPLETE = UrlTemplate.parse(`/api/v1/acn/autocomplete/{field}`);
22
const URL_UUID_FROM_IDS = `/api/v1/acn/id`;
23
const URL_LIST_BY_UUID = `/api/v1/acn/for-uuid`;
24
25
const URL_GET_ACCESSION_AUDIT_LOG_BY_DOI = `/api/v1/acn/auditlog/{doi}`; // UrlTemplate doesn't like the / in DOI
const URL_GET_ACCESSION_AUDIT_LOG_BY_UUID = UrlTemplate.parse(`/api/v1/acn/auditlog/{uuid}`);
26
const URL_GET_DETAILS_BY_DOI = `/api/v1/acn/details/{doi}`; // UrlTemplate doesn't like the / in DOI
27
const URL_GET_DETAILS_BY_UUID = UrlTemplate.parse(`/api/v1/acn/details/{uuid}`);
28
29
const URL_TO_UUID = `/api/v1/acn/toUUID`;
const URL_LIST = `/api/v1/acn/list`;
30
const URL_LIST_SUGGESTIONS = `/api/v1/acn/filter`;
31
32
const URL_LIST_OVERVIEW = `/api/v1/acn/overview`;
const URL_MAPINFO = `/api/v1/acn/mapinfo`;
33
34
35
36
37
38
39
40
41
42
43
44

/*
* Defined in Swagger as 'accession'
*/
class AccessionService {


  /**
   * getByDoi at /api/v1/acn/10.{dummy}/**}
   *
   * @param authToken Authorization token
   */
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
45
  public static getByDoi(doi: string, xhrConfig?): Promise<Accession> {
46

47
    const apiUrl = URL_GET_BY_DOI.replace(/{doi}/, doi);
48
49
50
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

51
    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
52
      ...xhrConfig,
53
54
55
56
57
58
59
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as Accession);
  }

  /**
60
   * getByUuid at /api/v1/acn/{uuid}
61
62
   *
   * @param authToken Authorization token
63
   * @param uuid UUID
64
   */
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
65
  public static getByUuid(uuid: string, xhrConfig?): Promise<Accession> {
66

67
    const apiUrl = URL_GET_BY_UUID.expand({ uuid });
68
69
70
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

71
    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
72
      ...xhrConfig,
73
74
75
76
77
78
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as Accession);
  }

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
79
80
81
82
83
84
  /**
   * geoJson at /api/v1/acn/explore/geoJson
   *
   * @param filter filter
   * @param limit limit
   */
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
85
  public static geoJson(filter: AccessionFilter, limit?: number, xhrConfig?): Promise<any> {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
86
87
88
89
90
91
92
93
94

    const qs = QueryString.stringify({
      limit: limit || undefined,
    }, {});
    const apiUrl = URL_GEO_JSON + (qs ? `?${qs}` : '');
    // console.log(`Fetching from ${apiUrl}`);
    const content = { data: filter };

    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
95
      ...xhrConfig,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
96
97
98
99
100
101
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as any);
  }

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
  /**
   * uuidsFromAcceNumbers at /api/v1/acn/acce-number
   *
   * @param acceNumbers acceNumbers
   * @param instCode instCode
   * @param xhrConfig additional xhr config
   */
  public static uuidsFromAcceNumbers(acceNumbers: string[], instCode?: string, xhrConfig?: any): Promise<string[]> {

    const qs = QueryString.stringify({
      instCode: instCode || undefined,
    }, {});
    const apiUrl = URL_UUIDS_FROM_ACCE_NUMBERS + (qs ? `?${qs}` : '');
    // console.log(`Fetching from ${apiUrl}`);
    const content = { data: acceNumbers };

    return axiosBackend.request({
      ...xhrConfig,
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as string[]);
  }

  /**
   * uuidFromAcceNumber at /api/v1/acn/acce-number/{acceNumber}
   *
   * @param acceNumber acceNumber
   * @param instCode instCode
   * @param xhrConfig additional xhr config
   */
  public static uuidFromAcceNumber(acceNumber: string, instCode?: string, xhrConfig?: any): Promise<string> {

    const qs = QueryString.stringify({
      instCode: instCode || undefined,
    }, {});
    const apiUrl = URL_UUID_FROM_ACCE_NUMBER.expand({ acceNumber }) + (qs ? `?${qs}` : '');
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

    return axiosBackend.request({
      ...xhrConfig,
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as string);
  }
149
150
151
152
153
  /**
   * uuidFromIds at /api/v1/acn/id
   *
   * @param ids ids
   */
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
154
  public static uuidFromIds(ids: number[], xhrConfig?): Promise<string[]> {
155
156
157
158
159
160

    const apiUrl = URL_UUID_FROM_IDS;
    // console.log(`Fetching from ${apiUrl}`);
    const content = { data: ids };

    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
161
      ...xhrConfig,
162
163
164
165
166
167
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as string[]);
  }

168
169
170
171
172
  /**
   * getAccessionAuditLogByDoi at /api/v1/acn/auditlog/{doi}
   *
   * @param doi doi
   */
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
173
  public static getAccessionAuditLogByDoi(doi: string, xhrConfig?): Promise<AccessionAuditLog> {
174
175
176
177
178
179
180

    const apiUrl = URL_GET_ACCESSION_AUDIT_LOG_BY_DOI.replace(/{doi}/, doi);

    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
181
      ...xhrConfig,
182
183
184
185
186
187
188
189
190
191
192
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as AccessionAuditLog);
  }

  /**
   * getAccessionAuditLogByUUID at /api/v1/acn/auditlog/{uuid}
   *
   * @param uuid uuid
   */
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
193
  public static getAccessionAuditLogByUUID(uuid: string, xhrConfig?): Promise<AccessionAuditLog> {
194
195
196
197
198
199

    const apiUrl = URL_GET_ACCESSION_AUDIT_LOG_BY_UUID.expand({uuid});
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

    return axiosBackend({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
200
      ...xhrConfig,
201
202
203
204
205
206
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as AccessionAuditLog);
  }

207
208
209
210
211
  /**
   * getByDoi at /api/v1/acn/10.{dummy}/**}
   *
   * @param authToken Authorization token
   */
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
212
  public static getDetailsByDoi(doi: string, xhrConfig?): Promise<AccessionDetails> {
213
214
215
216
217

    const apiUrl = URL_GET_DETAILS_BY_DOI.replace(/{doi}/, doi);
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

218
    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
219
      ...xhrConfig,
220
221
222
223
224
225
226
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as AccessionDetails);
  }

  /**
227
   * getByUuid at /api/v1/acn/{uuid}
228
229
   *
   * @param authToken Authorization token
230
   * @param uuid UUID
231
   */
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
232
  public static getDetailsByUuid(uuid: string, xhrConfig?): Promise<AccessionDetails> {
233

234
    const apiUrl = URL_GET_DETAILS_BY_UUID.expand({ uuid });
235
236
237
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

238
    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
239
      ...xhrConfig,
240
241
242
243
244
245
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as AccessionDetails);
  }

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
246
  public static toUUID(identifiers: AccessionRef[], xhrConfig?) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
247
248
249
    const apiUrl = URL_TO_UUID;
    const content = { data: identifiers };

250
    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
251
      ...xhrConfig,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
252
253
254
      url: apiUrl,
      method: 'POST',
      ...content,
255
    }).then(({ data }) => data as Map<string, AccessionRef>);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
256
257
  }

258
259
260
261
262
263
264
265
266
267
268
  /**
   * list at /api/v1/acn/list
   *
   * @param authToken Authorization token
   * @param filter filter
   * @param d d
   * @param f f
   * @param l l
   * @param p p
   * @param s s
   */
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
269
  public static list(filter: string | AccessionFilter, page: IPageRequest, xhrConfig?): Promise<FilteredPage<Accession>> {
270
271
272
273
274

    const qs = QueryString.stringify({
      f: typeof filter === 'string' ? filter : undefined,
      p: page.page || undefined,
      l: page.size || 100,
Matija Obreza's avatar
Matija Obreza committed
275
      d: page && page.direction ? page.direction : Accession.DEFAULT_SORT.direction,
276
277
278
279
280
281
      s: page.properties || Accession.DEFAULT_SORT.property,
    }, {});
    const apiUrl = URL_LIST + (qs ? `?${qs}` : '');
    // console.log(`Fetching from ${apiUrl}`);
    const content = { data: typeof filter === 'string' ? null : { ...filter } };

282
    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
283
      ...xhrConfig,
284
285
286
287
288
289
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as FilteredPage<Accession>);
  }

290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
  /**
   * listSuggestions at /api/v1/acn/list-suggestions
   *
   * @param filter filter
   * @param f f
   * @param page undefined
   * @param xhrConfig additional xhr config
   */
  public static listSuggestions(filter: string | AccessionFilter, page: IPageRequest, xhrConfig?): Promise<AccessionSuggestionPage> {

    const qs = QueryString.stringify({
      f: typeof filter === 'string' ? filter : undefined,
      p: page.page || undefined,
      l: page.size || 100,
      d: page && page.direction ? page.direction : Accession.DEFAULT_SORT.direction,
      s: page.properties || Accession.DEFAULT_SORT.property,
    }, {});
    const apiUrl = URL_LIST_SUGGESTIONS + (qs ? `?${qs}` : '');
    // console.log(`Fetching from ${apiUrl}`);
    const content = { data: typeof filter === 'string' ? null : { ...filter } };


    return axiosBackend.request({
      ...xhrConfig,
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as AccessionSuggestionPage);
  }

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
320
  public static listAllByUuid(UUIDs: string[], xhrConfig?): Promise<Accession[]> {
321
322
323
324
325
326

    const apiUrl = URL_LIST_BY_UUID;
    // console.log(`Fetching from ${apiUrl}`);
    const content = { data: UUIDs } || {};

    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
327
      ...xhrConfig,
328
329
330
331
332
333
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as Accession[]);
  }

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
334
  public static listOverview(filter: string | AccessionFilter, xhrConfig?) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
335
336
337
338
339
340
    const qs = QueryString.stringify({
      f: typeof filter === 'string' ? filter : undefined,
    }, {});
    const apiUrl = URL_LIST_OVERVIEW + (qs ? `?${qs}` : '');
    const content = { data: typeof filter === 'string' ? null : { ...filter } };

341
    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
342
      ...xhrConfig,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
343
344
345
346
347
348
      url: apiUrl,
      method: 'POST',
      headers: {'Content-Type': 'application/json'},
      ...content,
    }).then(({ data }) => data);
  }
349
350
351
352
353
354
355

  /**
   * get map information
   *
   * @param authToken Authorization token
   * @param filter filter or AccessionFilter
   */
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
356
  public static mapInfo(filter: string | AccessionFilter, xhrConfig?): Promise<AccessionMapInfo> {
357
358
359
360
361
362
    const qs = QueryString.stringify({
      f: typeof filter === 'string' ? filter : undefined,
    }, {});
    const apiUrl = URL_MAPINFO + (qs ? `?${qs}` : '');
    const content = { data: typeof filter === 'string' ? null : { ...filter } };

363
    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
364
      ...xhrConfig,
365
366
367
368
369
370
      url: apiUrl,
      method: 'POST',
      headers: {'Content-Type': 'application/json'},
      ...content,
    }).then(({ data }) => data);
  }
Maxym Borodenko's avatar
Maxym Borodenko committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394

  /**
   * autocomplete at /api/v1/acn/autocomplete/{field}
   *
   * @param field field
   * @param filter filter
   * @param term term
   */
  public static autocomplete(field: string, term: string, filter: string | AccessionFilter, xhrConfig?): Promise<Array<LabelValue<string>>> {

    const qs = QueryString.stringify({
      f: typeof filter === 'string' ? filter : undefined,
      term: term || undefined,
    }, {});
    const apiUrl = URL_AUTOCOMPLETE.expand({ field }) + (qs ? `?${qs}` : '');
    const content = { data: typeof filter === 'string' ? null : { ...filter } };

    return axiosBackend.request({
      ...xhrConfig,
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as Array<LabelValue<string>>);
  }
395
396
397
}

export default AccessionService;