-
Notifications
You must be signed in to change notification settings - Fork 1
/
v2.js
74 lines (71 loc) · 2.88 KB
/
v2.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
const OsduBaseService = require('../base');
/**
* Class that provides named access to OSDU V2 Query endpoints
* - [Service Documentation]{@link https://community.opengroup.org/osdu/platform/system/search-service/-/blob/master/docs/tutorial/SearchService.md}
* - [API Documentation]{@link https://community.opengroup.org/osdu/platform/system/search-service/-/blob/master/docs/api/search_openapi.yaml}
* @class
* @category Services
* @subcategory Query
*/
class OsduV2QueryService extends OsduBaseService {
/**
* @constructor
* @param {BaseOsduClient} osdu_client - An implementation of the OSDU client class to broker communication with the OSDU API
* @param {string} data_partition - The data partition against which requests will be made
*/
constructor(osdu_client, data_partition) {
super(osdu_client, data_partition);
}
/**
* Get OSDU records that match the given query constraints
* @param {Object} query_params - Query parameters built using the [OsduQueryBuilder]{@link OsduQueryBuilder}
* @returns {Object} The API Response
*/
async query(query_params) {
if (!query_params.offset) {
query_params.offset = 0;
}
return await this._client.post(`/api/search/v2/query`, query_params, this._dataPartition);
}
/**
* Get OSDU records that match the given query constraints
* - Allow specification of a cursor for paged queries
* @param {Object} query_params - Query parameters built using the [OsduQueryBuilder]{@link OsduQueryBuilder}
* @returns {Object} The API Response
*/
async queryWithPaging(query_params, cursor) {
if (!query_params.limit) {
query_params.limit = 1000;
}
query_params.offset = undefined;
if (cursor) {
query_params.cursor = cursor;
}
return await this._client.post(`/api/search/v2/query_with_cursor`, query_params, this._dataPartition);
}
/**
* Get all OSDU records that match the given query constraints
* - Will page internally and aggregate results until no more pages are found
* - Note that this may make multiple network requests if multiple pages are found
* @param {Object} query_params - Query parameters built using the [OsduQueryBuilder]{@link OsduQueryBuilder}
* @returns {Object} The API Response
*/
async queryAll(query_params) {
var output = {
results: [],
totalCount: 0,
batches: 0
};
var response;
var cursor = undefined;
do {
response = await this.queryWithPaging(query_params, cursor);
output.results.push(...response.results);
output.totalCount = response.totalCount;
output.batches++;
cursor = response.cursor;
} while (cursor);
return output;
}
}
module.exports = OsduV2QueryService;