Skip to content

Commit 2cdab3d

Browse files
authored
Merge pull request #5 from SlicingDice/feature/client-corrections
Move tests indicator to client constructor
2 parents d460ecf + 779b689 commit 2cdab3d

File tree

2 files changed

+48
-108
lines changed

2 files changed

+48
-108
lines changed

src/slicer.js

Lines changed: 47 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@
328328
}
329329

330330
class SlicingDice{
331-
constructor(key) {
331+
constructor(key, usesTestEndpoint = false) {
332332
this._key = key;
333333
this._checkKey(key);
334334
this._sdRoutes = {
@@ -346,6 +346,7 @@
346346
project: '/project/'
347347
};
348348
this._setUpRequest();
349+
this._usesTestEndpoint = usesTestEndpoint;
349350
}
350351

351352
get sdAddress() {
@@ -407,14 +408,14 @@
407408
return currentLevelKey[0];
408409
}
409410

410-
/* Make request to Slicing Dice API, if objRequest.test is true
411+
/* Make request to Slicing Dice API, if this._usesTestEndpoint is true
411412
the request will be sent to test end-point
412413
*/
413414
makeRequest(objRequest) {
414415
let token = this._getAPIKey(objRequest.levelKey);
415416
let urlReq;
416417
// test if the request must be sent to test endpoint
417-
if (objRequest.test){
418+
if (this._usesTestEndpoint){
418419
urlReq = this.BASE_URL + "/test" + objRequest.path;
419420
} else {
420421
urlReq = this.BASE_URL + objRequest.path;
@@ -436,80 +437,59 @@
436437
}, (err) => { return err;});
437438
}
438439

439-
/* Get all projects
440-
*
441-
* @param (boolean) test - if true we will use test end-point,
442-
* otherwise production end-point
443-
*/
444-
getProjects(test = false){
440+
/* Get all projects */
441+
getProjects(){
445442
let path = this._sdRoutes.project;
446443
return this.makeRequest({
447444
path: path,
448445
reqType: "GET",
449-
levelKey: 2,
450-
test: test
446+
levelKey: 2
451447
});
452448
}
453449

454-
/* Get all fields
455-
*
456-
* @param (boolean) test - if is true we will use test end-point,
457-
* otherwise production end-point
458-
*/
459-
getFields(test = false){
450+
/* Get all fields */
451+
getFields(){
460452
let path = this._sdRoutes.field;
461453
return this.makeRequest({
462454
path: path,
463455
reqType: "GET",
464-
levelKey: 2,
465-
test: test
456+
levelKey: 2
466457
});
467458
}
468459

469-
/* Get all saved queries
470-
*
471-
* @param (boolean) test - if is true we will use test end-point,
472-
* otherwise production end-point
473-
*/
474-
getSavedQueries(test = false) {
460+
/* Get all saved queries */
461+
getSavedQueries() {
475462
let path = this._sdRoutes.saved;
476463
return this.makeRequest({
477464
path: path,
478465
reqType: "GET",
479-
levelKey: 2,
480-
test: test
466+
levelKey: 2
481467
});
482468
}
483469

484470
/* Delete a saved query
485471
*
486472
* @param (string) name - the name of the saved query that will be deleted
487-
* @param (boolean) test - if is true we will use test end-point,
488-
* otherwise production end-point
489473
*/
490-
deleteSavedQuery(name, test = false) {
474+
deleteSavedQuery(name) {
491475
let path = this._sdRoutes.saved + name;
492476
return this.makeRequest({
493477
path: path,
494478
reqType: "DELETE",
495-
levelKey: 2,
496-
teste: test
479+
levelKey: 2
497480
});
498481
}
499482

500483
/* Get saved query by name
501484
*
502485
* @param (string) name - the name of the saved query that will be retrieved
503-
* @param (boolean) test - if is true we will use test end-point,
504-
* otherwise production end-point
505486
*/
506-
getSavedQuery(name, test = false) {
487+
getSavedQuery(name) {
507488
let path = this._sdRoutes.saved + name;
508489
return this.makeRequest({
509490
path: path,
510491
reqType: "GET",
511-
levelKey: 0,
512-
test: test,
492+
levelKey: 0
513493
});
514494
}
515495

@@ -518,10 +498,8 @@
518498
* @param (array) query - the query to send to Slicing Dice API
519499
* @param (boolean) autoCreateFields - if is true Slicing Dice API will
520500
* automatically create nonexistent fields
521-
* @param (boolean) test - if is true we will use test end-point,
522-
* otherwise production end-point
523501
*/
524-
index(query, autoCreateFields = false, test = false){
502+
index(query, autoCreateFields = false){
525503
if (autoCreateFields){
526504
query["auto-create-fields"] = true
527505
}
@@ -531,27 +509,23 @@
531509
path: path,
532510
reqType: "POST",
533511
data: query,
534-
levelKey: 1,
535-
test: test
512+
levelKey: 1
536513
});
537514
}
538515

539516
/* Create a field on Slicing Dice API
540517
*
541518
* @param (array) query - the query to send to Slicing Dice API
542-
* @param (boolean) test - if is true we will use test end-point,
543-
* otherwise production end-point
544519
*/
545-
createField(query, test = false){
520+
createField(query){
546521
let path = this._sdRoutes.field;
547522
let sdValidator = new FieldValidator(query);
548523
if (sdValidator.validator()){
549524
return this.makeRequest({
550525
path: path,
551526
reqType: "POST",
552527
data: query,
553-
levelKey: 1,
554-
test: test
528+
levelKey: 1
555529
});
556530
}
557531
}
@@ -560,67 +534,53 @@
560534
*
561535
* @param (array) query - the query to send to Slicing Dice API
562536
* @param (string) path - the path to send the query (count entity or count event path)
563-
* @param (boolean) test - if is true we will use test end-point,
564-
* otherwise production end-point
565537
*/
566-
countQueryWrapper(query, path, test){
538+
countQueryWrapper(query, path){
567539
let sdValidator = new QueryCountValidator(query);
568540
if (sdValidator.validator()){
569541
return this.makeRequest({
570542
path: path,
571543
reqType: "POST",
572544
data: query,
573-
levelKey: 0,
574-
test: test
545+
levelKey: 0
575546
});
576547
}
577548
}
578549

579550
/* Makes a count entity query on Slicing Dice API
580551
*
581552
* @param (array) query - the query to send to Slicing Dice API
582-
* @param (boolean) test - if is true we will use test end-point,
583-
* otherwise production end-point
584553
*/
585-
countEntity(query, test = false){
554+
countEntity(query){
586555
let path = this._sdRoutes.countEntity;
587556
let sdValidator = new QueryCountValidator(query);
588-
return this.countQueryWrapper(query, path, test);
557+
return this.countQueryWrapper(query, path);
589558
}
590559

591-
/* Makes a total query on Slicing Dice API
592-
*
593-
* @param (boolean) test - if is true we will use test end-point,
594-
* otherwise production end-point
595-
*/
596-
countEntityTotal(test = false) {
560+
/* Makes a total query on Slicing Dice API */
561+
countEntityTotal() {
597562
let path = this._sdRoutes.countEntityTotal;
598563
return this.makeRequest({
599564
path: path,
600565
reqType: "GET",
601-
levelKey: 0,
602-
test: test
566+
levelKey: 0
603567
})
604568
}
605569

606570
/* Makes a count event query on Slicing Dice API
607571
*
608572
* @param (array) query - the query to send to Slicing Dice API
609-
* @param (boolean) test - if is true we will use test end-point,
610-
* otherwise production end-point
611573
*/
612-
countEvent(query, test = false){
574+
countEvent(query){
613575
let path = this._sdRoutes.countEvent;
614-
return this.countQueryWrapper(query, path, test);
576+
return this.countQueryWrapper(query, path);
615577
}
616578

617579
/* Makes a exists query on Slicing Dice API
618580
*
619-
* @param (array) ids - the array of ids to check
620-
* @param (boolean) test - if is true we will use test end-point,
621-
* otherwise production end-point
581+
* @param (array) ids - the array of ids to check
622582
*/
623-
existsEntity(ids, test = false) {
583+
existsEntity(ids) {
624584
if (ids.constructor != Array){
625585
throw new errors.WrongTypeError("This method should receive an array as parameter");
626586
}
@@ -635,64 +595,54 @@
635595
path: path,
636596
reqType: "POST",
637597
data: query,
638-
levelKey: 0,
639-
test: test
598+
levelKey: 0
640599
});
641600
}
642601

643602
/* Makes an aggregation query on Slicing Dice API
644603
*
645604
* @param (array) query - the query to send to Slicing Dice API
646-
* @param (boolean) test - if is true we will use test end-point,
647-
* otherwise production end-point
648605
*/
649-
aggregation(query, test = false){
606+
aggregation(query){
650607
let path = this._sdRoutes.aggregation;
651608
return this.makeRequest({
652609
path: path,
653610
reqType: "POST",
654611
data: query,
655-
levelKey: 0,
656-
test: test
612+
levelKey: 0
657613
});
658614
}
659615

660616
/* Makes a top values query on Slicing Dice API
661617
*
662618
* @param (array) query - the query to send to Slicing Dice API
663-
* @param (boolean) test - if is true we will use test end-point,
664-
* otherwise production end-point
665619
*/
666-
topValues(query, test = false) {
620+
topValues(query) {
667621
let path = this._sdRoutes.topValues;
668622
let sdValidator = new QueryTopValuesValidator(query);
669623
if (sdValidator.validator()){
670624
return this.makeRequest({
671625
path: path,
672626
reqType: "POST",
673627
data: query,
674-
levelKey: 0,
675-
test: test
628+
levelKey: 0
676629
});
677630
}
678631
}
679632

680633
/* Create a saved query on Slicing Dice API
681634
*
682635
* @param (array) query - the query to send to Slicing Dice API
683-
* @param (boolean) test - if is true we will use test end-point,
684-
* otherwise production end-point
685636
*/
686-
createSavedQuery(query, test) {
637+
createSavedQuery(query) {
687638
let path = this._sdRoutes.saved;
688639
let sdValidator = new SavedQueryValidator(query);
689640
if (sdValidator.validator()){
690641
return this.makeRequest({
691642
path: path,
692643
reqType: "POST",
693644
data: query,
694-
levelKey: 2,
695-
test: test
645+
levelKey: 2
696646
});
697647
}
698648
}
@@ -701,60 +651,50 @@
701651
*
702652
* @param (string) name - the name of the saved query to update
703653
* @param (array) query - the query to send to Slicing Dice API
704-
* @param (boolean) test - if is true we will use test end-point,
705-
* otherwise production end-point
706654
*/
707-
updateSavedQuery(name, query, test = false) {
655+
updateSavedQuery(name, query) {
708656
let path = this._sdRoutes.saved + name;
709657
return this.makeRequest({
710658
path: path,
711659
reqType: "PUT",
712660
data: query,
713-
levelKey: 2,
714-
test: test
661+
levelKey: 2
715662
});
716663
}
717664

718665
/* Makes a data extraction query (result or score) on Slicing Dice API
719666
*
720667
* @param (array) query - the query to send to Slicing Dice API
721668
* @param (string) path - the path to send the query (result or score path)
722-
* @param (boolean) test - if is true we will use test end-point,
723-
* otherwise production end-point
724669
*/
725-
dataExtractionWrapper(query, path, test) {
670+
dataExtractionWrapper(query, path) {
726671
let sdValidator = new QueryDataExtractionValidator(query);
727672
if (sdValidator.validator()){
728673
return this.makeRequest({
729674
path: path,
730675
reqType: "POST",
731676
data: query,
732-
levelKey: 0,
733-
test: test
677+
levelKey: 0
734678
});
735679
}
736680
}
737681

738682
/* Makes a result query on Slicing Dice API
739683
*
740684
* @param (array) query - the query to send to Slicing Dice API
741-
* @param (boolean) test - if is true we will use test end-point,
742-
* otherwise production end-point
743685
*/
744-
result(query, test = false) {
686+
result(query) {
745687
let path = this._sdRoutes.result;
746-
return this.dataExtractionWrapper(query, path, test);
688+
return this.dataExtractionWrapper(query, path);
747689
}
748690

749691
/* Makes a score query on Slicing Dice API
750692
*
751693
* @param (array) query - the query to send to Slicing Dice API
752-
* @param (boolean) test - if is true we will use test end-point,
753-
* otherwise production end-point
754694
*/
755-
score(query, test = false) {
695+
score(query) {
756696
let path = this._sdRoutes.score;
757-
return this.dataExtractionWrapper(query, path, test);
697+
return this.dataExtractionWrapper(query, path);
758698
}
759699
}
760700

tests_and_examples/runQueryTests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class SlicingDiceTester {
3232
constructor(api_key, verbose=false) {
3333
this.client = new SlicingDice({
3434
masterKey: api_key
35-
});
35+
}, true);
3636

3737
// Translation table for fields with timestamp
3838
this.fieldTranslation = {}

0 commit comments

Comments
 (0)