Skip to content

Commit 88a723a

Browse files
committed
Correct connection logic
1 parent 3355c9c commit 88a723a

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

SlicingDice.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,31 @@
22

33

44
SlicingDice::SlicingDice(const char* apiUserKey) {
5-
host = "api.slicingdice.com/v1";
5+
host = "api.slicingdice.com";
66
port = 80;
77
apiKey = apiUserKey;
8+
test = 0;
89
}
910

1011
SlicingDice::SlicingDice(const char* apiUserKey, const char* customHost) {
1112
host = customHost;
1213
port = 80;
1314
apiKey = apiUserKey;
15+
test = 0;
1416
}
1517

1618
SlicingDice::SlicingDice(const char* apiUserKey, const char* customHost, int customPort) {
1719
host = customHost;
1820
port = customPort;
1921
apiKey = apiUserKey;
22+
test = 0;
23+
}
24+
25+
SlicingDice::SlicingDice(const char* apiUserKey, const char* customHost, int customPort, int customTest) {
26+
host = customHost;
27+
port = customPort;
28+
apiKey = apiUserKey;
29+
test = customTest;
2030
}
2131

2232
/* Index data on Slicing Dice API
@@ -39,8 +49,14 @@ void SlicingDice::makeRequest(const char* query){
3949
client.connect(host, port);
4050
}
4151

42-
client.println(F("POST /index/ HTTP/1.1"));
52+
client.print(F("POST /v1/"));
53+
if (test == 1) {
54+
client.print("test/");
55+
}
56+
client.println(F("index/ HTTP/1.1"));
4357
client.println(F("Content-Type: application/json"));
58+
client.print(F("Authorization: "));
59+
client.println(apiKey);
4460
client.println(F("Connection: close"));
4561
client.print(F("Content-Length: "));
4662
client.println(strlen(query));

SlicingDice.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class SlicingDice {
1010
SlicingDice(const char* apiUserKey);
1111
SlicingDice(const char* apiUserKey, const char* customHost);
1212
SlicingDice(const char* apiUserKey, const char* customHost, int customPort);
13+
SlicingDice(const char* apiUserKey, const char* customHost, int customPort, int customTest);
1314

1415
void index(JsonObject& query);
1516
int statusCode;
@@ -22,5 +23,6 @@ class SlicingDice {
2223
const char* apiKey;
2324
const char* host;
2425
int port;
26+
int test;
2527
EthernetClient client;
26-
};
28+
};

tests_and_examples/Simple.ino

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
#include <SlicingDice.h>
1+
#include "SlicingDice.h"
22
#include <ArduinoJson.h>
33

4-
SlicingDice sd = SlicingDice("mytoken");
4+
// Demo API key, if you need a new demo API key visit: https://panel.slicingdice.com/docs/#api-details-api-connection-api-keys-demo-key
5+
const char* apiKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfX3NhbHQiOiJkZW1vNTZtIiwicGVybWlzc2lvbl9sZXZlbCI6MywicHJvamVjdF9pZCI6MjE3LCJjbGllbnRfaWQiOjEwfQ.rKHWahhcTN8Hvhns8-O2_KwjC_b3SFHd3kqZLyDMrfc";
6+
const char* host = "api.slicingdice.com";
7+
int port = 8080;
8+
// test = 1 indicates that will use test endpoint, 0 indicates will use production end-point
9+
int test = 1;
10+
SlicingDice sd = SlicingDice(apiKey, host, port, test);
511

612
void setup() {
713
// Open serial communications and wait for port to open:
@@ -11,8 +17,8 @@ void setup() {
1117

1218
// Arduino network settings, should match your internet connection properties
1319
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
14-
byte ip[] = { 192, 168, 1, 10 };
15-
byte gateway[] = { 192, 168, 1, 1 };
20+
byte ip[] = { 192, 168, 0, 10 };
21+
byte gateway[] = { 192, 168, 0, 1 };
1622
byte subnet[] = { 255, 255, 255, 0 };
1723
byte dnxs[] = { 8, 8, 8, 8 };
1824
Ethernet.begin(mac, ip, dnxs, gateway, subnet);

0 commit comments

Comments
 (0)