AccessionService.ts 9.56 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';
14

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

/*
* 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
41
  public static getByDoi(doi: string, xhrConfig?): Promise<Accession> {
42

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

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

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

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

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

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

    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
91
      ...xhrConfig,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
92 93 94 95 96 97
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as any);
  }

98 99 100 101 102
  /**
   * uuidFromIds at /api/v1/acn/id
   *
   * @param ids ids
   */
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
103
  public static uuidFromIds(ids: number[], xhrConfig?): Promise<string[]> {
104 105 106 107 108 109

    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
110
      ...xhrConfig,
111 112 113 114 115 116
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as string[]);
  }

117 118 119 120 121
  /**
   * getAccessionAuditLogByDoi at /api/v1/acn/auditlog/{doi}
   *
   * @param doi doi
   */
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
122
  public static getAccessionAuditLogByDoi(doi: string, xhrConfig?): Promise<AccessionAuditLog> {
123 124 125 126 127 128 129

    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
130
      ...xhrConfig,
131 132 133 134 135 136 137 138 139 140 141
      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
142
  public static getAccessionAuditLogByUUID(uuid: string, xhrConfig?): Promise<AccessionAuditLog> {
143 144 145 146 147 148

    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
149
      ...xhrConfig,
150 151 152 153 154 155
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as AccessionAuditLog);
  }

156 157 158 159 160
  /**
   * getByDoi at /api/v1/acn/10.{dummy}/**}
   *
   * @param authToken Authorization token
   */
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
161
  public static getDetailsByDoi(doi: string, xhrConfig?): Promise<AccessionDetails> {
162 163 164 165 166

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

167
    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
168
      ...xhrConfig,
169 170 171 172 173 174 175
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as AccessionDetails);
  }

  /**
176
   * getByUuid at /api/v1/acn/{uuid}
177 178
   *
   * @param authToken Authorization token
179
   * @param uuid UUID
180
   */
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
181
  public static getDetailsByUuid(uuid: string, xhrConfig?): Promise<AccessionDetails> {
182

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

187
    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
188
      ...xhrConfig,
189 190 191 192 193 194
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as AccessionDetails);
  }

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
195
  public static toUUID(identifiers: AccessionRef[], xhrConfig?) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
196 197 198
    const apiUrl = URL_TO_UUID;
    const content = { data: identifiers };

199
    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
200
      ...xhrConfig,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
201 202 203
      url: apiUrl,
      method: 'POST',
      ...content,
204
    }).then(({ data }) => data as Map<string, AccessionRef>);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
205 206
  }

207 208 209 210 211 212 213 214 215 216 217
  /**
   * 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
218
  public static list(filter: string | AccessionFilter, page: IPageRequest, xhrConfig?): Promise<FilteredPage<Accession>> {
219 220 221 222 223

    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
224
      d: page && page.direction ? page.direction : Accession.DEFAULT_SORT.direction,
225 226 227 228 229 230
      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 } };

231
    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
232
      ...xhrConfig,
233 234 235 236 237 238
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as FilteredPage<Accession>);
  }

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
  /**
   * 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
269
  public static listAllByUuid(UUIDs: string[], xhrConfig?): Promise<Accession[]> {
270 271 272 273 274 275

    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
276
      ...xhrConfig,
277 278 279 280 281 282
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as Accession[]);
  }

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
283
  public static listOverview(filter: string | AccessionFilter, xhrConfig?) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
284 285 286 287 288 289
    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 } };

290
    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
291
      ...xhrConfig,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
292 293 294 295 296 297
      url: apiUrl,
      method: 'POST',
      headers: {'Content-Type': 'application/json'},
      ...content,
    }).then(({ data }) => data);
  }
298 299 300 301 302 303 304

  /**
   * get map information
   *
   * @param authToken Authorization token
   * @param filter filter or AccessionFilter
   */
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
305
  public static mapInfo(filter: string | AccessionFilter, xhrConfig?): Promise<AccessionMapInfo> {
306 307 308 309 310 311
    const qs = QueryString.stringify({
      f: typeof filter === 'string' ? filter : undefined,
    }, {});
    const apiUrl = URL_MAPINFO + (qs ? `?${qs}` : '');
    const content = { data: typeof filter === 'string' ? null : { ...filter } };

312
    return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
313
      ...xhrConfig,
314 315 316 317 318 319
      url: apiUrl,
      method: 'POST',
      headers: {'Content-Type': 'application/json'},
      ...content,
    }).then(({ data }) => data);
  }
320 321 322
}

export default AccessionService;