-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrAPIClientService.js
117 lines (100 loc) · 2.68 KB
/
BrAPIClientService.js
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
#!/usr/bin/env node
let BASE_URL = "https://test-server.brapi.org/brapi/v1/";
// const BASE_URL = "https://yambase.org/brapi/v1/";
const setBrAPI = function(url) {
if (url.charAt(url.length) !== "/") {
url += "/";
}
console.error("setting BASE_URL to ",url);
BASE_URL = url;
}
const request = require('request-promise');
const _ = require('lodash');
const getPrograms = () => {
const options = {
method: 'GET',
uri: BASE_URL + 'programs',
qs: {
page: 0,
pageSize: 10
},
json: true
}
return request(options)
.then(function(response){
return response.result.data;
})
.catch(function(err){return console.log(err);});
};
const getTrials = (programDbId) => {
const options = {
method: 'GET',
uri: BASE_URL + 'trials',
qs: {
page: 0,
pageSize: 10,
programDbId: programDbId
},
json: true
}
return request(options)
.then(function(response){
return response.result.data;
})
.catch(function(err){return console.log(err);});
};
const getStudies = (trialDbId) => {
const options = {
method: 'GET',
uri: BASE_URL + 'studies',
qs: {
page: 0,
pageSize: 10,
trialDbId: trialDbId
},
json: true
}
return request(options)
.then(function(response){
return response.result.data;
})
.catch(function(err){return console.log(err);});
};
const getAnything = (endpoint, page) => {
page = page || 0;
const pageSize = (endpoint === 'germplasm-search') ? 1000 : 100;
console.error(`get ${BASE_URL}${endpoint}?page=${page}`);
let options = {
method: 'GET',
uri: BASE_URL + endpoint,
qs: {
page: page,
pageSize: pageSize
},
json: true
};
return request(options)
.then(async response => {
const prior = response.metadata.pagination.currentPage * response.metadata.pagination.pageSize;
console.error(`prior=${prior}, totalCount=${response.metadata.pagination.totalCount}`);
if (response.result.data.length === pageSize && response.result.data.length + prior < response.metadata.pagination.totalCount) {
let moreData = await getAnything(endpoint, page+1);
// if (!moreData) {
// console.error("moreData not defined",endpoint,page);
// }
if (!moreData.result) {
console.error("moreData.result not defined",endpoint,page);
process.exit(2);
}
response.result.data = _.concat(response.result.data, moreData.result.data);
return response;
}
return response
})
.catch(err => console.error(err))
}
module.exports.setBrAPI = setBrAPI;
module.exports.getAnything = getAnything;
module.exports.getPrograms = getPrograms;
module.exports.getTrials = getTrials;
module.exports.getStudies = getStudies;