Skip to content

Commit 990ee28

Browse files
authored
Merge pull request #19 from SlicingDice/feature/new-api-formats
Change client to match new api formats
2 parents a56a40e + 3f0b803 commit 990ee28

13 files changed

+49403
-53740
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package-lock.json
2+
examples/*
3+
node_modules/*

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
## [2.0.1]
44
### Updated
55
- Correct data extraction validator to accept columns: all
6+
- Add support for SQL queries on client
7+
- Adapt test queries to the changes on SlicingDice API
68

79
## [2.0.0]
810
### Updated

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SlicingDice Official JavaScript Client (v2.0.0)
1+
# SlicingDice Official JavaScript Client (v2.0.1)
22
### Build Status: [![CircleCI](https://circleci.com/gh/SlicingDice/slicingdice-javascript.svg?style=svg)](https://circleci.com/gh/SlicingDice/slicingdice-javascript)
33

44
Official JavaScript client for [SlicingDice](http://www.slicingdice.com/), Data Warehouse and Analytics Database as a Service.

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "slicerjs",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Official JavaScript client for SlicingDice, Data Warehouse and Analytics Database as a Service.",
55
"main": "src/slicer.js",
66
"scripts": {
@@ -26,10 +26,10 @@
2626
},
2727
"homepage": "https://github.com/SlicingDice/slicingdice-javascript",
2828
"devDependencies": {
29-
"babel-cli": "^6.11.4",
30-
"babel-preset-es2015": "^6.13.2",
31-
"async": "^2.0.1",
32-
"sleep": "^4.0.0"
29+
"babel-cli": "^6.26.0",
30+
"babel-preset-es2015": "^6.24.0",
31+
"async": "^2.6.0",
32+
"sleep": "^5.1.1"
3333
},
3434
"dependencies": {}
3535
}

src/slicer.js

+43-20
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@
1010

1111
// RequesterBrowser make http requests from the browser
1212
class RequesterBrowser {
13-
run(token, url, reqType, data = null) {
13+
run(token, url, reqType, data = null, sql) {
1414
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+
1523
return new Promise(function(resolve, reject) {
1624
let req = new XMLHttpRequest();
1725
req.open(reqType, url, true);
1826
req.setRequestHeader("Authorization", token);
19-
req.setRequestHeader('Content-Type', 'application/json');
27+
req.setRequestHeader('Content-Type', content_type);
2028
req.setRequestHeader('Access-Control-Allow-Origin', '*');
2129
req.setRequestHeader('Access-Control-Allow-Credentials', true);
2230
req.setRequestHeader('Accept', "application/json");
@@ -41,7 +49,7 @@
4149

4250
// RequesterNode make http requests from the node.js (with we're not running in a web-browser)
4351
class RequesterNode {
44-
run(token, urlReq, reqType, data = null) {
52+
run(token, urlReq, reqType, data = null, sql) {
4553

4654
return new Promise((resolve, reject) => {
4755
let port;
@@ -52,13 +60,24 @@
5260
} else{
5361
port = urlData.port;
5462
}
63+
64+
let content_type;
65+
66+
if (sql) {
67+
content_type = 'application/sql';
68+
} else {
69+
content_type = 'application/json';
70+
}
71+
5572
let headers = {
56-
'Content-type': 'application/json',
73+
'Content-type': content_type,
5774
'Authorization': token
5875
}
59-
if (data !== null) {
76+
if (data !== null && !sql) {
6077
jsonToSend = JSON.stringify(data);
6178
headers['Content-Length'] = Buffer.byteLength(jsonToSend);
79+
} else {
80+
jsonToSend = data;
6281
}
6382
let options = {
6483
hostname: urlData.hostname,
@@ -331,7 +350,7 @@
331350
}
332351

333352
class SlicingDice{
334-
constructor(apiKeys, usesTestEndpoint = false) {
353+
constructor(apiKeys) {
335354
this._key = apiKeys;
336355
this._checkKey(apiKeys);
337356
this._sdRoutes = {
@@ -346,10 +365,10 @@
346365
result: '/data_extraction/result/',
347366
score: '/data_extraction/score/',
348367
saved: '/query/saved/',
349-
database: '/database/'
368+
database: '/database/',
369+
sql: '/query/sql/'
350370
};
351371
this._setUpRequest();
352-
this._usesTestEndpoint = usesTestEndpoint;
353372
}
354373

355374
get sdAddress() {
@@ -411,18 +430,11 @@
411430
return currentLevelKey[0];
412431
}
413432

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
416434
*/
417-
makeRequest(objRequest) {
435+
makeRequest(objRequest, sql = false) {
418436
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;
426438
let requestMethods = ["POST", "PUT", "GET", "DELETE", "PATCH"];
427439
if (requestMethods.indexOf(objRequest.reqType) === -1){
428440
throw new errors.InvalidMethodRequestError('This method request is invalid.');
@@ -431,7 +443,8 @@
431443
token,
432444
urlReq,
433445
objRequest.reqType,
434-
objRequest.data);
446+
objRequest.data,
447+
sql);
435448

436449
return req.then((resp) => {
437450
let slicerResponse = new SlicerResponse(resp);
@@ -548,7 +561,7 @@
548561
*
549562
* @param (array) query - the query to send to Slicing Dice API
550563
*/
551-
countEntity(query){
564+
countEntity(query) {
552565
let path = this._sdRoutes.countEntity;
553566
let sdValidator = new QueryCountValidator(query);
554567
return this.countQueryWrapper(query, path);
@@ -704,6 +717,16 @@
704717
let path = this._sdRoutes.score;
705718
return this.dataExtractionWrapper(query, path);
706719
}
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+
}
707730
}
708731

709732
module.exports = SlicingDice;

0 commit comments

Comments
 (0)