|
10 | 10 |
|
11 | 11 | // RequesterBrowser make http requests from the browser
|
12 | 12 | class RequesterBrowser {
|
13 |
| - run(token, url, reqType, data = null) { |
| 13 | + run(token, url, reqType, data = null, sql) { |
14 | 14 | url = url.hostname + url.path;
|
| 15 | + let content_type; |
| 16 | + |
| 17 | + if (sql) { |
| 18 | + content_type = 'application/sql'; |
| 19 | + } else { |
| 20 | + content_type = 'application/json'; |
| 21 | + } |
| 22 | + |
15 | 23 | return new Promise(function(resolve, reject) {
|
16 | 24 | let req = new XMLHttpRequest();
|
17 | 25 | req.open(reqType, url, true);
|
18 | 26 | req.setRequestHeader("Authorization", token);
|
19 |
| - req.setRequestHeader('Content-Type', 'application/json'); |
| 27 | + req.setRequestHeader('Content-Type', content_type); |
20 | 28 | req.setRequestHeader('Access-Control-Allow-Origin', '*');
|
21 | 29 | req.setRequestHeader('Access-Control-Allow-Credentials', true);
|
22 | 30 | req.setRequestHeader('Accept', "application/json");
|
|
41 | 49 |
|
42 | 50 | // RequesterNode make http requests from the node.js (with we're not running in a web-browser)
|
43 | 51 | class RequesterNode {
|
44 |
| - run(token, urlReq, reqType, data = null) { |
| 52 | + run(token, urlReq, reqType, data = null, sql) { |
45 | 53 |
|
46 | 54 | return new Promise((resolve, reject) => {
|
47 | 55 | let port;
|
|
52 | 60 | } else{
|
53 | 61 | port = urlData.port;
|
54 | 62 | }
|
| 63 | + |
| 64 | + let content_type; |
| 65 | + |
| 66 | + if (sql) { |
| 67 | + content_type = 'application/sql'; |
| 68 | + } else { |
| 69 | + content_type = 'application/json'; |
| 70 | + } |
| 71 | + |
55 | 72 | let headers = {
|
56 |
| - 'Content-type': 'application/json', |
| 73 | + 'Content-type': content_type, |
57 | 74 | 'Authorization': token
|
58 | 75 | }
|
59 |
| - if (data !== null) { |
| 76 | + if (data !== null && !sql) { |
60 | 77 | jsonToSend = JSON.stringify(data);
|
61 | 78 | headers['Content-Length'] = Buffer.byteLength(jsonToSend);
|
| 79 | + } else { |
| 80 | + jsonToSend = data; |
62 | 81 | }
|
63 | 82 | let options = {
|
64 | 83 | hostname: urlData.hostname,
|
|
331 | 350 | }
|
332 | 351 |
|
333 | 352 | class SlicingDice{
|
334 |
| - constructor(apiKeys, usesTestEndpoint = false) { |
| 353 | + constructor(apiKeys) { |
335 | 354 | this._key = apiKeys;
|
336 | 355 | this._checkKey(apiKeys);
|
337 | 356 | this._sdRoutes = {
|
|
346 | 365 | result: '/data_extraction/result/',
|
347 | 366 | score: '/data_extraction/score/',
|
348 | 367 | saved: '/query/saved/',
|
349 |
| - database: '/database/' |
| 368 | + database: '/database/', |
| 369 | + sql: '/query/sql/' |
350 | 370 | };
|
351 | 371 | this._setUpRequest();
|
352 |
| - this._usesTestEndpoint = usesTestEndpoint; |
353 | 372 | }
|
354 | 373 |
|
355 | 374 | get sdAddress() {
|
|
411 | 430 | return currentLevelKey[0];
|
412 | 431 | }
|
413 | 432 |
|
414 |
| - /* Make request to Slicing Dice API, if this._usesTestEndpoint is true |
415 |
| - the request will be sent to test end-point |
| 433 | + /* Make request to Slicing Dice API |
416 | 434 | */
|
417 |
| - makeRequest(objRequest) { |
| 435 | + makeRequest(objRequest, sql = false) { |
418 | 436 | let token = this._getAPIKey(objRequest.levelKey);
|
419 |
| - let urlReq; |
420 |
| - // test if the request must be sent to test endpoint |
421 |
| - if (this._usesTestEndpoint){ |
422 |
| - urlReq = this.BASE_URL + "/test" + objRequest.path; |
423 |
| - } else { |
424 |
| - urlReq = this.BASE_URL + objRequest.path; |
425 |
| - } |
| 437 | + let urlReq = this.BASE_URL + objRequest.path; |
426 | 438 | let requestMethods = ["POST", "PUT", "GET", "DELETE", "PATCH"];
|
427 | 439 | if (requestMethods.indexOf(objRequest.reqType) === -1){
|
428 | 440 | throw new errors.InvalidMethodRequestError('This method request is invalid.');
|
|
431 | 443 | token,
|
432 | 444 | urlReq,
|
433 | 445 | objRequest.reqType,
|
434 |
| - objRequest.data); |
| 446 | + objRequest.data, |
| 447 | + sql); |
435 | 448 |
|
436 | 449 | return req.then((resp) => {
|
437 | 450 | let slicerResponse = new SlicerResponse(resp);
|
|
548 | 561 | *
|
549 | 562 | * @param (array) query - the query to send to Slicing Dice API
|
550 | 563 | */
|
551 |
| - countEntity(query){ |
| 564 | + countEntity(query) { |
552 | 565 | let path = this._sdRoutes.countEntity;
|
553 | 566 | let sdValidator = new QueryCountValidator(query);
|
554 | 567 | return this.countQueryWrapper(query, path);
|
|
704 | 717 | let path = this._sdRoutes.score;
|
705 | 718 | return this.dataExtractionWrapper(query, path);
|
706 | 719 | }
|
| 720 | + |
| 721 | + sql(query) { |
| 722 | + let path = this._sdRoutes.sql; |
| 723 | + return this.makeRequest({ |
| 724 | + path: path, |
| 725 | + reqType: "POST", |
| 726 | + data: query, |
| 727 | + levelKey: 0 |
| 728 | + }, true); |
| 729 | + } |
707 | 730 | }
|
708 | 731 |
|
709 | 732 | module.exports = SlicingDice;
|
|
0 commit comments