1
+ /**
2
+ * Copyright 2015 IBM Corp. All Rights Reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ 'use strict' ;
18
+
19
+ var util = require ( 'util' ) ;
20
+ var requestFactory = require ( '../lib/requestwrapper' ) ;
21
+ var BaseService = require ( '../lib/base_service' ) ;
22
+ var pick = require ( 'object.pick' ) ;
23
+
24
+ /**
25
+ *
26
+ * @param options
27
+ * @constructor
28
+ */
29
+ function DiscoveryV1 ( options ) {
30
+ BaseService . call ( this , options ) ;
31
+
32
+ // Check if 'version_date' was provided
33
+ if ( typeof this . _options . version_date === 'undefined' ) {
34
+ throw new Error ( 'Argument error: version_date was not specified, use DiscoveryV1.VERSION_DATE_2016_12_15' ) ;
35
+ }
36
+ this . _options . qs . version = options . version_date ;
37
+ }
38
+
39
+ util . inherits ( DiscoveryV1 , BaseService ) ;
40
+ DiscoveryV1 . prototype . name = 'discovery' ;
41
+ DiscoveryV1 . prototype . version = 'v1' ;
42
+ DiscoveryV1 . URL = 'https://gateway.watsonplatform.net/discovery/api' ;
43
+
44
+
45
+ /**
46
+ * Initial release
47
+ * @type {string }
48
+ */
49
+ DiscoveryV1 . VERSION_DATE_2016_12_15 = '2016-12-15' ;
50
+
51
+
52
+
53
+ /**
54
+ * Return the list of environments
55
+ *
56
+ * @param {Object } params
57
+ */
58
+ DiscoveryV1 . prototype . getEnvironments = function ( params , callback ) {
59
+ params = params || { } ;
60
+
61
+ var parameters = {
62
+ options : {
63
+ url : '/v1/environments' ,
64
+ method : 'GET' ,
65
+ json : true
66
+ } ,
67
+ defaultOptions : this . _options
68
+ } ;
69
+ return requestFactory ( parameters , callback ) ;
70
+ } ;
71
+
72
+ /**
73
+ * Get details about an environment
74
+ *
75
+ * @param {Object } params
76
+ * @param {String } params.environment_id
77
+ */
78
+ DiscoveryV1 . prototype . getEnvironment = function ( params , callback ) {
79
+ params = params || { } ;
80
+
81
+ var parameters = {
82
+ options : {
83
+ url : '/v1/environments/{environment_id}' ,
84
+ method : 'GET' ,
85
+ path : pick ( params , [ 'environment_id' ] ) ,
86
+ json : true
87
+ } ,
88
+ requiredParams : [ 'environment_id' ] ,
89
+ defaultOptions : this . _options
90
+ } ;
91
+ return requestFactory ( parameters , callback ) ;
92
+ } ;
93
+
94
+ /**
95
+ * List all configurations
96
+ *
97
+ * @param {Object } params
98
+ * @param {String } params.environment_id
99
+ */
100
+ DiscoveryV1 . prototype . getConfigurations = function ( params , callback ) {
101
+ params = params || { } ;
102
+
103
+ var parameters = {
104
+ options : {
105
+ url : '/v1/environments/{environment_id}/configurations' ,
106
+ method : 'GET' ,
107
+ path : pick ( params , [ 'environment_id' ] ) ,
108
+ json : true
109
+ } ,
110
+ requiredParams : [ 'environment_id' ] ,
111
+ defaultOptions : this . _options
112
+ } ;
113
+ return requestFactory ( parameters , callback ) ;
114
+ } ;
115
+
116
+ /**
117
+ * Get details about a configuration
118
+ *
119
+ * @param {Object } params
120
+ * @param {String } params.environment_id
121
+ * @param {String } params.configuration_id
122
+ */
123
+ DiscoveryV1 . prototype . getConfiguration = function ( params , callback ) {
124
+ params = params || { } ;
125
+
126
+ var parameters = {
127
+ options : {
128
+ url : '/v1/environments/{environment_id}/configurations/{configuration_id}' ,
129
+ method : 'GET' ,
130
+ path : pick ( params , [ 'environment_id' , 'configuration_id' ] ) ,
131
+ json : true
132
+ } ,
133
+ requiredParams : [ 'environment_id' , 'configuration_id' ] ,
134
+ defaultOptions : this . _options
135
+ } ;
136
+ return requestFactory ( parameters , callback ) ;
137
+ } ;
138
+
139
+ /**
140
+ * Return the list of collections in the given environment
141
+ *
142
+ * @param {Object } params
143
+ * @param {String } params.environment_id
144
+ */
145
+ DiscoveryV1 . prototype . getCollections = function ( params , callback ) {
146
+ params = params || { } ;
147
+
148
+ var parameters = {
149
+ options : {
150
+ url : '/v1/environments/{environment_id}/collections' ,
151
+ method : 'GET' ,
152
+ path : pick ( params , [ 'environment_id' ] ) ,
153
+ json : true
154
+ } ,
155
+ requiredParams : [ 'environment_id' ] ,
156
+ defaultOptions : this . _options
157
+ } ;
158
+ return requestFactory ( parameters , callback ) ;
159
+ } ;
160
+
161
+ /**
162
+ * Get details about a collection
163
+ *
164
+ * @param {Object } params
165
+ * @param {String } params.environment_id
166
+ * @param {string } params.collection_id
167
+ */
168
+ DiscoveryV1 . prototype . getCollection = function ( params , callback ) {
169
+ params = params || { } ;
170
+
171
+ var parameters = {
172
+ options : {
173
+ url : '/v1/environments/{environment_id}/collections/{collection_id}' ,
174
+ method : 'GET' ,
175
+ path : pick ( params , [ 'environment_id' , 'collection_id' ] ) ,
176
+ json : true
177
+ } ,
178
+ requiredParams : [ 'environment_id' , 'collection_id' ] ,
179
+ defaultOptions : this . _options
180
+ } ;
181
+ return requestFactory ( parameters , callback ) ;
182
+ } ;
183
+
184
+ /**
185
+ * Create a new collection
186
+ *
187
+ * @param {Object } params
188
+ * @param {String } params.environment_id environment guid for the collection
189
+ * @param {string } params.collection_name
190
+ * @param {string } params.description
191
+ * @param {string } params.configuration_id configuration to create the collection in
192
+ * @param {string } params.language_code currently, only `en_us` is supported
193
+ */
194
+ DiscoveryV1 . prototype . createCollection = function ( params , callback ) {
195
+ params = params || { } ;
196
+
197
+ params . language_code = params . language_code || 'en_us' ;
198
+
199
+ var parameters = {
200
+ options : {
201
+ url : '/v1/environments/{environment_id}/collections' ,
202
+ method : 'POST' ,
203
+ path : pick ( params , [ 'environment_id' ] ) ,
204
+ multipart : [
205
+ {
206
+ 'content-type' : 'application/json' ,
207
+ body : JSON . stringify ( pick ( params , [ 'collection_name' , 'description' , 'configuration_id' , 'language_code' ] ) )
208
+ }
209
+ ] ,
210
+ json : true
211
+ } ,
212
+ requiredParams : [ 'environment_id' , 'configuration_id' , 'collection_name' ] ,
213
+ defaultOptions : this . _options
214
+ } ;
215
+ return requestFactory ( parameters , callback ) ;
216
+ } ;
217
+
218
+ /**
219
+ * Delete a collection
220
+ *
221
+ * @param {Object } params
222
+ * @param {String } params.environment_id environment guid for the collection
223
+ * @param {string } params.collection_id the guid of the collection to delete
224
+ */
225
+ DiscoveryV1 . prototype . deleteCollection = function ( params , callback ) {
226
+ params = params || { } ;
227
+
228
+ params . language_code = params . language_code || 'en_us' ;
229
+
230
+ var parameters = {
231
+ options : {
232
+ url : '/v1/environments/{environment_id}/collections/{collection_id}' ,
233
+ method : 'DELETE' ,
234
+ path : pick ( params , [ 'environment_id' , 'collection_id' ] ) ,
235
+ json : true
236
+ } ,
237
+ requiredParams : [ 'environment_id' , 'collection_id' ] ,
238
+ defaultOptions : this . _options
239
+ } ;
240
+ return requestFactory ( parameters , callback ) ;
241
+ } ;
242
+
243
+ /**
244
+ * Add a document to a collection
245
+ * @param params
246
+ * @param {String } params.environment_id environment guid for the collection
247
+ * @param {string } params.collection_id the guid of the collection to delete
248
+ * @param {Buffer|ReadableStream|Object } params.file a file to post (smaller than 50mb)
249
+ * @param {string } [params.configuration_id] config guid
250
+ * @param {string } [params.metadata] file metadata, including content-type (will infer if missing)
251
+ * @param callback
252
+ * @returns {ReadableStream|undefined }
253
+ */
254
+ DiscoveryV1 . prototype . addDocument = function ( params , callback ) {
255
+ params = params || { } ;
256
+
257
+ var query_params = pick ( params , [ 'configuration_id' ] )
258
+
259
+ var parameters = {
260
+ options : {
261
+ url : '/v1/environments/{environment_id}/collections/{collection_id}/documents' ,
262
+ method : 'POST' ,
263
+ path : pick ( params , [ 'environment_id' , 'collection_id' ] ) ,
264
+ qs : query_params ,
265
+ formData : pick ( params , [ 'file' , 'metadata' ] ) ,
266
+ json : true
267
+ } ,
268
+ requiredParams : [ 'environment_id' , 'collection_id' , 'file' ] ,
269
+ defaultOptions : this . _options
270
+ } ;
271
+ return requestFactory ( parameters , callback ) ;
272
+ } ;
273
+
274
+ /**
275
+ * Delete a specific document
276
+ * @param params
277
+ * @param {String } params.environment_id environment guid for the collection
278
+ * @param {string } params.collection_id the guid of the collection to delete
279
+ * @param {string } params.document_id the guid of the document to delete
280
+ * @param callback
281
+ * @returns {ReadableStream|undefined }
282
+ */
283
+ DiscoveryV1 . prototype . deleteDocument = function ( params , callback ) {
284
+ params = params || { } ;
285
+
286
+ var parameters = {
287
+ options : {
288
+ url : '/v1/environments/{environment_id}/collections/{collection_id}/documents/{document_id}' ,
289
+ method : 'DELETE' ,
290
+ path : pick ( params , [ 'environment_id' , 'collection_id' , 'document_id' ] ) ,
291
+ json : true
292
+ } ,
293
+ requiredParams : [ 'environment_id' , 'collection_id' , 'document_id' ] ,
294
+ defaultOptions : this . _options
295
+ } ;
296
+ return requestFactory ( parameters , callback ) ;
297
+ } ;
298
+
299
+ /**
300
+ * Queries the collection
301
+ *
302
+ * @param {Object } params
303
+ * @param {String } params.environment_id
304
+ * @param {string } params.collection_id
305
+ * @param {String } [params.filter] A cacheable query that allows you to limit the information returned to exclude anything that isn't related to what you are searching. Filter searches are better for metadata type searches and when you are trying to get a sense of concepts in the dataset.
306
+ * @param {String } [params.query] A query search returns all possible results, even when it's not very relevant, with the most relevant documents listed first. Use a query search when you want to find the most relevant search results. Results are scored between 0 and 1, with 1 being an exact match and 0 being not a match at all.
307
+ * @param {String } [params.aggregation] An aggregation search uses combinations of filters and query search to return an exact answer. Aggregations are useful for building applications, because you can use them to build lists, tables, and time series. For a full list of possible aggregrations, see the Query reference.
308
+ * @param {Number } [params.count=10] Number of documents to return
309
+ * @param {String } [params.return] A comma separated list of the portion of the document hierarchy to return.
310
+ * @param {Number } [params.offset=0] For pagination purposes. Returns additional pages of results. Deep pagination is highly unperformant, and should be avoided.
311
+ */
312
+ DiscoveryV1 . prototype . query = function ( params , callback ) {
313
+ params = params || { } ;
314
+
315
+ var parameters = {
316
+ options : {
317
+ url : '/v1/environments/{environment_id}/collections/{collection_id}/query' ,
318
+ method : 'GET' ,
319
+ json : true ,
320
+ path : pick ( params , [ 'environment_id' , 'collection_id' ] ) ,
321
+ qs : pick ( params , [ 'filter' , 'aggregation' , 'return' , 'count' , 'offset' , 'query' ] )
322
+ } ,
323
+ requiredParams : [ 'environment_id' , 'collection_id' ] ,
324
+ defaultOptions : this . _options
325
+ } ;
326
+ return requestFactory ( parameters , callback ) ;
327
+ } ;
328
+
329
+ module . exports = DiscoveryV1 ;
0 commit comments