SubsetService.ts 13.6 KB
Newer Older
Matija Obreza's avatar
Subsets  
Matija Obreza committed
1 2 3

import * as UrlTemplate from 'url-template';
import * as QueryString from 'query-string';
4
import { axiosBackend } from 'utilities/requestUtils';
Matija Obreza's avatar
Subsets  
Matija Obreza committed
5 6

import FilteredPage, { IPageRequest } from 'model/FilteredPage';
7
import Page from 'model/Page';
8
import Subset from 'model/subset/Subset';
9
import SubsetCreator from 'model/subset/SubsetCreator';
10
import SubsetFilter from 'model/subset/SubsetFilter';
11
import {AccessionRef} from 'model/accession/AccessionRef';
12

13 14
const URL_LIST_ACCESSIONS = UrlTemplate.parse(`/api/v1/subset/accessions/{uuid}`);
const URL_ADD_ACCESSIONS = UrlTemplate.parse(`/api/v1/subset/add-accessions/{uuid},{version}`);
15 16
const URL_SET_ACCESSIONS = UrlTemplate.parse(`/api/v1/subset/set-accessions/{uuid},{version}`);
const URL_REMATCH_ACCESSIONS = UrlTemplate.parse(`/api/v1/subset/rematch-accessions/{uuid},{version}`);
17 18 19 20 21 22
const URL_APPROVE_SUBSET = `/api/v1/subset/approve`;
const URL_CREATE = `/api/v1/subset/create`;
const URL_REVIEW_SUBSET = `/api/v1/subset/for-review`;
const URL_LIST = `/api/v1/subset/list`;
const URL_MY_SUBSETS = `/api/v1/subset/list-mine`;
const URL_REJECT_SUBSET = `/api/v1/subset/reject`;
23
const URL_REMOVE_ACCESSIONS = UrlTemplate.parse(`/api/v1/subset/remove-accessions/{uuid},{version}`);
24
const URL_UPDATE = `/api/v1/subset/update`;
25 26 27 28 29 30 31 32
const URL_GET = UrlTemplate.parse(`/api/v1/subset/{uuid}`);
const URL_REMOVE = UrlTemplate.parse(`/api/v1/subset/{uuid},{version}`);
const URL_AUTOCOMPLETE = `/api/v1/subset/{uuid}/subsetcreator/autocomplete`;
const URL_CREATE_SUBSET_CREATOR = UrlTemplate.parse(`/api/v1/subset/{uuid}/subsetcreator/create`);
const URL_DELETE_SUBSET_CREATOR = UrlTemplate.parse(`/api/v1/subset/{uuid}/subsetcreator/delete`);
const URL_LIST_SUBSET_CREATORS = UrlTemplate.parse(`/api/v1/subset/{uuid}/subsetcreator/list`);
const URL_UPDATE_SUBSET_CREATOR = UrlTemplate.parse(`/api/v1/subset/{uuid}/subsetcreator/update`);
const URL_LOAD_BY_UUID = UrlTemplate.parse(`/api/v1/subset/{uuid}/subsetcreator/{creatorUuid}`);
Matija Obreza's avatar
Subsets  
Matija Obreza committed
33 34

/*
35
* Defined in Swagger as 'subset'
Matija Obreza's avatar
Subsets  
Matija Obreza committed
36 37 38
*/
class SubsetService {

39
  /**
40
   * listAccessions at /api/v1/subset/accessions/{uuid}
41
   *
42
   * @param uuid UUID
43 44
   * @param page undefined
   */
45
  public static listAccessions(uuid: string, page?: IPageRequest): Promise<Page<AccessionRef>> {
46 47 48 49 50 51 52

    const qs = QueryString.stringify({
      p: page.page || undefined,
      l: page.size || undefined,
      d: page.direction ? page.direction : undefined,
      s: page.properties || undefined,
    }, {});
53
    const apiUrl = URL_LIST_ACCESSIONS.expand({ uuid }) + (qs ? `?${qs}` : '');
54 55 56 57 58 59 60 61 62 63
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

    return axiosBackend.request({
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as Page<AccessionRef>);
  }

Matija Obreza's avatar
Subsets  
Matija Obreza committed
64
  /**
65
   * addAccessions at /api/v1/subset/add-accessions/{uuid},{version}
Matija Obreza's avatar
Subsets  
Matija Obreza committed
66
   *
67
   * @param uuid UUID
Matija Obreza's avatar
Subsets  
Matija Obreza committed
68
   * @param version version
69
   * @param accessionIds accessionIds
Matija Obreza's avatar
Subsets  
Matija Obreza committed
70
   */
71
  public static addAccessions(uuid: string, version: number, accessionRefs: AccessionRef[]): Promise<Subset> {
Matija Obreza's avatar
Subsets  
Matija Obreza committed
72

73
    const apiUrl = URL_ADD_ACCESSIONS.expand({uuid, version});
Matija Obreza's avatar
Subsets  
Matija Obreza committed
74
    // console.log(`Fetching from ${apiUrl}`);
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
    const content = { data: accessionRefs };

    return axiosBackend.request({
      url: apiUrl,
      method: 'POST',
      timeout: 0, // don't timeout
      ...content,
    }).then(({ data }) => data as Subset);
  }

  /**
   * setAccessions at /api/v1/subset/set-accessions/{uuid},{version}
   *
   * @param uuid UUID
   * @param version version
   * @param accessionIds accessionIds
   */
  public static setAccessions(uuid: string, version: number, accessionRefs: AccessionRef[]): Promise<Subset> {

    const apiUrl = URL_SET_ACCESSIONS.expand({uuid, version});
    // console.log(`Fetching from ${apiUrl}`);
    const content = { data: accessionRefs };

    return axiosBackend.request({
      url: apiUrl,
      method: 'POST',
      timeout: 0, // don't timeout
      ...content,
    }).then(({ data }) => data as Subset);
  }

  /**
   * rematchAccessions at /api/v1/subset/rematch-accessions/{uuid},{version}
   *
   * @param uuid UUID
   * @param version version
   * @param accessionIds accessionIds
   */
  public static rematchAccessions(uuid: string, version: number): Promise<Subset> {

    const apiUrl = URL_REMATCH_ACCESSIONS.expand({uuid, version});
    // console.log(`Fetching from ${apiUrl}`);
    const content = { };
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

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

  /**
   * approveSubset at /api/v1/subset/approve
   *
   * @param uuid uuid
   * @param version version
   */
  public static approveSubset(uuid: string, version: number): Promise<Subset> {

    const qs = QueryString.stringify({
      uuid: uuid || undefined,
      version: version || undefined,
    }, {});
    const apiUrl = URL_APPROVE_SUBSET + (qs ? `?${qs}` : '');
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };
Matija Obreza's avatar
Subsets  
Matija Obreza committed
141

142
    return axiosBackend.request({
Matija Obreza's avatar
Subsets  
Matija Obreza committed
143 144 145 146 147 148 149 150 151 152 153
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as Subset);
  }

  /**
   * create at /api/v1/subset/create
   *
   * @param subset subset
   */
154
  public static create(subset: Subset): Promise<Subset> {
Matija Obreza's avatar
Subsets  
Matija Obreza committed
155 156 157 158 159

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

160
    return axiosBackend.request({
Matija Obreza's avatar
Subsets  
Matija Obreza committed
161 162 163 164 165 166
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as Subset);
  }

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  /**
   * reviewSubset at /api/v1/subset/for-review
   *
   * @param uuid uuid
   * @param version version
   */
  public static reviewSubset(uuid: string, version: number): Promise<Subset> {

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

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

Matija Obreza's avatar
Subsets  
Matija Obreza committed
190 191 192 193
  /**
   * list at /api/v1/subset/list
   *
   * @param filter filter
194
   * @param page undefined
Matija Obreza's avatar
Subsets  
Matija Obreza committed
195
   */
196
  public static list(filter: string | SubsetFilter, page?: IPageRequest): Promise<FilteredPage<Subset>> {
Matija Obreza's avatar
Subsets  
Matija Obreza committed
197 198 199 200 201

    const qs = QueryString.stringify({
      f: typeof filter === 'string' ? filter : undefined,
      p: page.page || undefined,
      l: page.size || undefined,
Matija Obreza's avatar
Matija Obreza committed
202
      d: page.direction && page.direction || undefined,
203
      s: page.properties || undefined,
Matija Obreza's avatar
Subsets  
Matija Obreza committed
204 205
    }, {});
    const apiUrl = URL_LIST + (qs ? `?${qs}` : '');
206
    console.log(`Fetching from ${apiUrl}`, page);
Matija Obreza's avatar
Subsets  
Matija Obreza committed
207
    const content = { data: typeof filter === 'string' ? null : { ...filter } };
Matija Obreza's avatar
Matija Obreza committed
208

209
    return axiosBackend.request({
Matija Obreza's avatar
Matija Obreza committed
210 211 212 213 214 215 216
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as FilteredPage<Subset>);
  }

  /**
217
   * mySubsets at /api/v1/subset/list-mine
Matija Obreza's avatar
Matija Obreza committed
218 219
   *
   * @param filter filter
220
   * @param page undefined
Matija Obreza's avatar
Matija Obreza committed
221
   */
222
  public static mySubsets(filter: string | SubsetFilter, page?: IPageRequest): Promise<FilteredPage<Subset>> {
Matija Obreza's avatar
Matija Obreza committed
223 224 225 226 227

    const qs = QueryString.stringify({
      f: typeof filter === 'string' ? filter : undefined,
      p: page.page || undefined,
      l: page.size || undefined,
Matija Obreza's avatar
Matija Obreza committed
228
      d: page.direction ? page.direction : undefined,
229
      s: page.properties || undefined,
Matija Obreza's avatar
Matija Obreza committed
230
    }, {});
231
    const apiUrl = URL_MY_SUBSETS + (qs ? `?${qs}` : '');
Matija Obreza's avatar
Matija Obreza committed
232 233
    // console.log(`Fetching from ${apiUrl}`);
    const content = { data: typeof filter === 'string' ? null : { ...filter } };
Matija Obreza's avatar
Subsets  
Matija Obreza committed
234

235
    return axiosBackend.request({
Matija Obreza's avatar
Subsets  
Matija Obreza committed
236 237 238 239 240 241
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as FilteredPage<Subset>);
  }

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
  /**
   * rejectSubset at /api/v1/subset/reject
   *
   * @param uuid uuid
   * @param version version
   */
  public static rejectSubset(uuid: string, version: number): Promise<Subset> {

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

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

Matija Obreza's avatar
Subsets  
Matija Obreza committed
265
  /**
266
   * removeAccessions at /api/v1/subset/remove-accessions/{uuid},{version}
Matija Obreza's avatar
Subsets  
Matija Obreza committed
267
   *
268
   * @param uuid UUID
Matija Obreza's avatar
Subsets  
Matija Obreza committed
269 270 271
   * @param version version
   * @param accessionsUuids accessionsUuids
   */
272
  public static removeAccessions(uuid: string, version: number, accessionsUuids: string[]): Promise<Subset> {
Matija Obreza's avatar
Subsets  
Matija Obreza committed
273

274
    const apiUrl = URL_REMOVE_ACCESSIONS.expand({uuid, version});
Matija Obreza's avatar
Subsets  
Matija Obreza committed
275 276 277
    // console.log(`Fetching from ${apiUrl}`);
    const content = { data: accessionsUuids };

278
    return axiosBackend.request({
Matija Obreza's avatar
Subsets  
Matija Obreza committed
279 280 281 282 283 284 285 286 287 288 289
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as Subset);
  }

  /**
   * update at /api/v1/subset/update
   *
   * @param subset subset
   */
290
  public static update(subset: Subset): Promise<Subset> {
Matija Obreza's avatar
Subsets  
Matija Obreza committed
291 292 293 294 295

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

296
    return axiosBackend.request({
Matija Obreza's avatar
Subsets  
Matija Obreza committed
297 298 299 300 301 302 303
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as Subset);
  }

  /**
304
   * get at /api/v1/subset/{uuid}
Matija Obreza's avatar
Subsets  
Matija Obreza committed
305
   *
306
   * @param uuid UUID
Matija Obreza's avatar
Subsets  
Matija Obreza committed
307
   */
308
  public static get(uuid: string): Promise<Subset> {
Matija Obreza's avatar
Subsets  
Matija Obreza committed
309

310
    const apiUrl = URL_GET.expand({uuid});
Matija Obreza's avatar
Subsets  
Matija Obreza committed
311 312 313
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

314
    return axiosBackend.request({
Matija Obreza's avatar
Subsets  
Matija Obreza committed
315 316 317 318 319 320 321
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as Subset);
  }

  /**
322
   * remove at /api/v1/subset/{uuid},{version}
Matija Obreza's avatar
Subsets  
Matija Obreza committed
323
   *
324
   * @param uuid UUID
Matija Obreza's avatar
Subsets  
Matija Obreza committed
325 326
   * @param version version
   */
327
  public static remove(uuid: string, version: number): Promise<Subset> {
Matija Obreza's avatar
Subsets  
Matija Obreza committed
328

329
    const apiUrl = URL_REMOVE.expand({uuid, version});
Matija Obreza's avatar
Subsets  
Matija Obreza committed
330 331 332
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

333
    return axiosBackend.request({
Matija Obreza's avatar
Subsets  
Matija Obreza committed
334 335 336 337 338 339
      url: apiUrl,
      method: 'DELETE',
      ...content,
    }).then(({ data }) => data as Subset);
  }

340
  /**
341
   * autocomplete at /api/v1/subset/{uuid}/subsetcreator/autocomplete
342 343 344 345
   *
   * @param c c
   */
  public static autocomplete(c: string): Promise<SubsetCreator[]> {
Matija Obreza's avatar
Subsets  
Matija Obreza committed
346

347 348 349 350 351 352
    const qs = QueryString.stringify({
      c: c || undefined,
    }, {});
    const apiUrl = URL_AUTOCOMPLETE + (qs ? `?${qs}` : '');
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
353

354
    return axiosBackend.request({
355 356 357 358
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as SubsetCreator[]);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
359 360
  }

361
  /**
362
   * createSubsetCreator at /api/v1/subset/{uuid}/subsetcreator/create
363
   *
364
   * @param uuid UUID
365 366
   * @param subsetCreator subsetCreator
   */
367
  public static createSubsetCreator(uuid: string, subsetCreator: SubsetCreator): Promise<SubsetCreator> {
368

369
    const apiUrl = URL_CREATE_SUBSET_CREATOR.expand({uuid});
370 371
    // console.log(`Fetching from ${apiUrl}`);
    const content = { data: subsetCreator };
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
372

373
    return axiosBackend.request({
374
      url: apiUrl,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
375
      method: 'POST',
376 377
      ...content,
    }).then(({ data }) => data as SubsetCreator);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
378 379
  }

380
  /**
381
   * deleteSubsetCreator at /api/v1/subset/{uuid}/subsetcreator/delete
382
   *
383
   * @param uuid UUID
384 385
   * @param subsetCreator subsetCreator
   */
386
  public static deleteSubsetCreator(uuid: string, subsetCreator: SubsetCreator): Promise<SubsetCreator> {
387

388
    const apiUrl = URL_DELETE_SUBSET_CREATOR.expand({uuid});
389 390
    // console.log(`Fetching from ${apiUrl}`);
    const content = { data: subsetCreator };
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
391

392
    return axiosBackend.request({
393
      url: apiUrl,
394
      method: 'POST',
395 396
      ...content,
    }).then(({ data }) => data as SubsetCreator);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
397 398
  }

399
  /**
400
   * listSubsetCreators at /api/v1/subset/{uuid}/subsetcreator/list
401
   *
402
   * @param uuid UUID
403 404
   * @param page undefined
   */
405
  public static listSubsetCreators(uuid: string, page?: IPageRequest): Promise<Page<SubsetCreator>> {
406 407 408 409

    const qs = QueryString.stringify({
      p: page.page || undefined,
      l: page.size || undefined,
Matija Obreza's avatar
Matija Obreza committed
410
      d: page.direction ? page.direction : undefined,
411 412
      s: page.properties || undefined,
    }, {});
413
    const apiUrl = URL_LIST_SUBSET_CREATORS.expand({uuid}) + (qs ? `?${qs}` : '');
414 415
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
416

417
    return axiosBackend.request({
418 419 420 421 422 423 424
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as Page<SubsetCreator>);
  }

  /**
425
   * updateSubsetCreator at /api/v1/subset/{uuid}/subsetcreator/update
426
   *
427
   * @param uuid UUID
428 429
   * @param subsetCreator subsetCreator
   */
430
  public static updateSubsetCreator(uuid: string, subsetCreator: SubsetCreator): Promise<SubsetCreator> {
431

432
    const apiUrl = URL_UPDATE_SUBSET_CREATOR.expand({uuid});
433 434 435 436 437
    // console.log(`Fetching from ${apiUrl}`);
    const content = { data: subsetCreator };

    return axiosBackend.request({
      url: apiUrl,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
438
      method: 'POST',
439 440
      ...content,
    }).then(({ data }) => data as SubsetCreator);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
441 442
  }

443
  /**
444
   * loadByUuid at /api/v1/subset/{uuid}/subsetcreator/{creatorUuid}
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
   *
   * @param creatorUuid creatorUuid
   */
  public static loadByUuid(creatorUuid: string): Promise<SubsetCreator> {

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

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


Matija Obreza's avatar
Subsets  
Matija Obreza committed
462 463 464
}

export default SubsetService;