AccessionService.ts 8.04 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

14
const URL_GET_BY_DOI = `/api/v1/acn/{doi}`; // UrlTemplate doesn't like the / in DOI
15
const URL_GET_BY_UUID = UrlTemplate.parse(`/api/v1/acn/{uuid}`);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
16
const URL_GEO_JSON = `/api/v1/acn/geoJson`;
17
const URL_UUID_FROM_IDS = `/api/v1/acn/id`;
18
const URL_LIST_BY_UUID = `/api/v1/acn/for-uuid`;
19
20
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}`);
21
const URL_GET_DETAILS_BY_DOI = `/api/v1/acn/details/{doi}`; // UrlTemplate doesn't like the / in DOI
22
const URL_GET_DETAILS_BY_UUID = UrlTemplate.parse(`/api/v1/acn/details/{uuid}`);
23
24
25
26
const URL_TO_UUID = `/api/v1/acn/toUUID`;
const URL_LIST = `/api/v1/acn/list`;
const URL_LIST_OVERVIEW = `/api/v1/acn/overview`;
const URL_MAPINFO = `/api/v1/acn/mapinfo`;
27
28
29
30
31
32
33
34
35
36
37
38

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


  /**
   * getByDoi at /api/v1/acn/10.{dummy}/**}
   *
   * @param authToken Authorization token
   */
39
  public static getByDoi(doi: string): Promise<Accession> {
40

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

45
    return axiosBackend.request({
46
47
48
49
50
51
52
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as Accession);
  }

  /**
53
   * getByUuid at /api/v1/acn/{uuid}
54
55
   *
   * @param authToken Authorization token
56
   * @param uuid UUID
57
   */
58
  public static getByUuid(uuid: string): Promise<Accession> {
59

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

64
    return axiosBackend.request({
65
66
67
68
69
70
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as Accession);
  }

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

    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({
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as any);
  }

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  /**
   * uuidFromIds at /api/v1/acn/id
   *
   * @param ids ids
   */
  public static uuidFromIds(ids: number[]): Promise<string[]> {

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

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

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
  /**
   * getAccessionAuditLogByDoi at /api/v1/acn/auditlog/{doi}
   *
   * @param doi doi
   */
  public static getAccessionAuditLogByDoi(doi: string): Promise<AccessionAuditLog> {

    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({
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as AccessionAuditLog);
  }

  /**
   * getAccessionAuditLogByUUID at /api/v1/acn/auditlog/{uuid}
   *
   * @param uuid uuid
   */
  public static getAccessionAuditLogByUUID(uuid: string): Promise<AccessionAuditLog> {

    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({
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as AccessionAuditLog);
  }

148
149
150
151
152
  /**
   * getByDoi at /api/v1/acn/10.{dummy}/**}
   *
   * @param authToken Authorization token
   */
153
  public static getDetailsByDoi(doi: string): Promise<AccessionDetails> {
154
155
156
157
158

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

159
    return axiosBackend.request({
160
161
162
163
164
165
166
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as AccessionDetails);
  }

  /**
167
   * getByUuid at /api/v1/acn/{uuid}
168
169
   *
   * @param authToken Authorization token
170
   * @param uuid UUID
171
   */
172
  public static getDetailsByUuid(uuid: string): Promise<AccessionDetails> {
173

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

178
    return axiosBackend.request({
179
180
181
182
183
184
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as AccessionDetails);
  }

185
  public static toUUID(identifiers: AccessionRef[]) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
186
187
188
    const apiUrl = URL_TO_UUID;
    const content = { data: identifiers };

189
    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
190
191
192
      url: apiUrl,
      method: 'POST',
      ...content,
193
    }).then(({ data }) => data as Map<string, AccessionRef>);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
194
195
  }

196
197
198
199
200
201
202
203
204
205
206
  /**
   * 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
   */
207
  public static list(filter: string | AccessionFilter, page: IPageRequest): Promise<FilteredPage<Accession>> {
208
209
210
211
212

    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
213
      d: page && page.direction ? page.direction : Accession.DEFAULT_SORT.direction,
214
215
216
217
218
219
      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 } };

220
    return axiosBackend.request({
221
222
223
224
225
226
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as FilteredPage<Accession>);
  }

227
228
229
230
231
232
233
234
235
236
237
238
239
  public static listAllByUuid(UUIDs: string[]): Promise<Accession[]> {

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

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

240
  public static listOverview(filter: string | AccessionFilter) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
241
242
243
244
245
246
    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 } };

247
    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
248
249
250
251
252
253
      url: apiUrl,
      method: 'POST',
      headers: {'Content-Type': 'application/json'},
      ...content,
    }).then(({ data }) => data);
  }
254
255
256
257
258
259
260

  /**
   * get map information
   *
   * @param authToken Authorization token
   * @param filter filter or AccessionFilter
   */
261
  public static mapInfo(filter: string | AccessionFilter): Promise<AccessionMapInfo> {
262
263
264
265
266
267
    const qs = QueryString.stringify({
      f: typeof filter === 'string' ? filter : undefined,
    }, {});
    const apiUrl = URL_MAPINFO + (qs ? `?${qs}` : '');
    const content = { data: typeof filter === 'string' ? null : { ...filter } };

268
    return axiosBackend.request({
269
270
271
272
273
274
      url: apiUrl,
      method: 'POST',
      headers: {'Content-Type': 'application/json'},
      ...content,
    }).then(({ data }) => data);
  }
275
276
277
}

export default AccessionService;