Skip to content

Commit c36587a

Browse files
authored
Merge pull request #2 from SlicingDice/feature/correct-code
Correct connection logic
2 parents 3355c9c + 6a1063f commit c36587a

File tree

3 files changed

+48
-23
lines changed

3 files changed

+48
-23
lines changed

SlicingDice.cpp

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
#include "SlicingDice.h"
22

33

4-
SlicingDice::SlicingDice(const char* apiUserKey) {
5-
host = "api.slicingdice.com/v1";
6-
port = 80;
7-
apiKey = apiUserKey;
4+
SlicingDice::SlicingDice(String apiUserKey) {
5+
construct(apiUserKey, "api.slicingdice.com", 80, true);
86
}
97

10-
SlicingDice::SlicingDice(const char* apiUserKey, const char* customHost) {
11-
host = customHost;
12-
port = 80;
13-
apiKey = apiUserKey;
8+
SlicingDice::SlicingDice(String apiUserKey, const char* customHost) {
9+
construct(apiUserKey, customHost, 80, true);
1410
}
1511

16-
SlicingDice::SlicingDice(const char* apiUserKey, const char* customHost, int customPort) {
12+
SlicingDice::SlicingDice(String apiUserKey, const char* customHost, int customPort) {
13+
construct(apiUserKey, customHost, customPort, true);
14+
}
15+
16+
SlicingDice::SlicingDice(String apiUserKey, const char* customHost, int customPort, boolean production) {
17+
construct(apiUserKey, customHost, customPort, production);
18+
}
19+
20+
void SlicingDice::construct(String apiUserKey, const char* customHost, int customPort, boolean production) {
1721
host = customHost;
1822
port = customPort;
1923
apiKey = apiUserKey;
24+
useProduction = production;
2025
}
2126

2227
/* Index data on Slicing Dice API
@@ -39,13 +44,23 @@ void SlicingDice::makeRequest(const char* query){
3944
client.connect(host, port);
4045
}
4146

42-
client.println(F("POST /index/ HTTP/1.1"));
47+
String testEndPoint = String("");
48+
49+
if (!useProduction) {
50+
testEndPoint = String("test/");
51+
}
52+
53+
client.println("POST /v1/" + testEndPoint + "index HTTP/1.1");
4354
client.println(F("Content-Type: application/json"));
55+
String hostString = String(host);
56+
client.println("Host: " + hostString);
57+
client.println("Authorization: " + apiKey);
4458
client.println(F("Connection: close"));
45-
client.print(F("Content-Length: "));
46-
client.println(strlen(query));
47-
client.println();
48-
client.print(query);
59+
60+
String actualLength = String(strlen(query));
61+
client.println("Content-Length: " + actualLength);
62+
client.println();
63+
client.println(query);
4964
readResponse();
5065
client.stop();
5166
}

SlicingDice.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@
77
class SlicingDice {
88

99
public:
10-
SlicingDice(const char* apiUserKey);
11-
SlicingDice(const char* apiUserKey, const char* customHost);
12-
SlicingDice(const char* apiUserKey, const char* customHost, int customPort);
10+
SlicingDice(String apiUserKey);
11+
SlicingDice(String apiUserKey, const char* customHost);
12+
SlicingDice(String apiUserKey, const char* customHost, int customPort);
13+
SlicingDice(String apiUserKey, const char* customHost, int customPort, boolean production);
1314

1415
void index(JsonObject& query);
1516
int statusCode;
1617
String response;
1718

1819
private:
1920
void makeRequest(const char* query);
21+
void construct(String apiUserKey, const char* customHost, int customPort, boolean production);
2022
void readResponse();
2123

22-
const char* apiKey;
24+
String apiKey;
2325
const char* host;
2426
int port;
27+
boolean useProduction;
2528
EthernetClient client;
2629
};

tests_and_examples/Simple.ino

Lines changed: 12 additions & 5 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+
String apiKey = String("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfX3NhbHQiOiJkZW1vMThtIiwicGVybWlzc2lvbl9sZXZlbCI6MywicHJvamVjdF9pZCI6MTc5LCJjbGllbnRfaWQiOjEwfQ.OTb6REW9JtYF9wVUZhXajq4wheU5ULNbM5iEmMCYhhM");
6+
const char* host = "api.slicingdice.com";
7+
int port = 80;
8+
// if false will use test end-point, otherwise production end-point
9+
int useProduction = false;
10+
SlicingDice sd = SlicingDice(apiKey, host, port, useProduction);
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);
@@ -24,8 +30,9 @@ void loop() {
2430
JsonObject& queryIndex = jsonBuffer.createObject();
2531
JsonObject& nestedQueryIndex = queryIndex.createNestedObject("user1@slicingdice.com");
2632
nestedQueryIndex["age"] = 22;
33+
queryIndex["auto-create-fields"] = true;
2734
sd.index(queryIndex);
2835
Serial.print("Status code: ");
2936
Serial.println(sd.statusCode);
3037
Serial.println(sd.response);
31-
}
38+
}

0 commit comments

Comments
 (0)