Skip to content

Correct connection logic #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions SlicingDice.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
#include "SlicingDice.h"


SlicingDice::SlicingDice(const char* apiUserKey) {
host = "api.slicingdice.com/v1";
port = 80;
apiKey = apiUserKey;
SlicingDice::SlicingDice(String apiUserKey) {
construct(apiUserKey, "api.slicingdice.com", 80, true);
}

SlicingDice::SlicingDice(const char* apiUserKey, const char* customHost) {
host = customHost;
port = 80;
apiKey = apiUserKey;
SlicingDice::SlicingDice(String apiUserKey, const char* customHost) {
construct(apiUserKey, customHost, 80, true);
}

SlicingDice::SlicingDice(const char* apiUserKey, const char* customHost, int customPort) {
SlicingDice::SlicingDice(String apiUserKey, const char* customHost, int customPort) {
construct(apiUserKey, customHost, customPort, true);
}

SlicingDice::SlicingDice(String apiUserKey, const char* customHost, int customPort, boolean production) {
construct(apiUserKey, customHost, customPort, production);
}

void SlicingDice::construct(String apiUserKey, const char* customHost, int customPort, boolean production) {
host = customHost;
port = customPort;
apiKey = apiUserKey;
useProduction = production;
}

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

client.println(F("POST /index/ HTTP/1.1"));
String testEndPoint = String("");

if (!useProduction) {
testEndPoint = String("test/");
}

client.println("POST /v1/" + testEndPoint + "index HTTP/1.1");
client.println(F("Content-Type: application/json"));
String hostString = String(host);
client.println("Host: " + hostString);
client.println("Authorization: " + apiKey);
client.println(F("Connection: close"));
client.print(F("Content-Length: "));
client.println(strlen(query));
client.println();
client.print(query);

String actualLength = String(strlen(query));
client.println("Content-Length: " + actualLength);
client.println();
client.println(query);
readResponse();
client.stop();
}
Expand Down
11 changes: 7 additions & 4 deletions SlicingDice.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@
class SlicingDice {

public:
SlicingDice(const char* apiUserKey);
SlicingDice(const char* apiUserKey, const char* customHost);
SlicingDice(const char* apiUserKey, const char* customHost, int customPort);
SlicingDice(String apiUserKey);
SlicingDice(String apiUserKey, const char* customHost);
SlicingDice(String apiUserKey, const char* customHost, int customPort);
SlicingDice(String apiUserKey, const char* customHost, int customPort, boolean production);

void index(JsonObject& query);
int statusCode;
String response;

private:
void makeRequest(const char* query);
void construct(String apiUserKey, const char* customHost, int customPort, boolean production);
void readResponse();

const char* apiKey;
String apiKey;
const char* host;
int port;
boolean useProduction;
EthernetClient client;
};
17 changes: 12 additions & 5 deletions tests_and_examples/Simple.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#include <SlicingDice.h>
#include "SlicingDice.h"
#include <ArduinoJson.h>

SlicingDice sd = SlicingDice("mytoken");
// 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
String apiKey = String("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfX3NhbHQiOiJkZW1vMThtIiwicGVybWlzc2lvbl9sZXZlbCI6MywicHJvamVjdF9pZCI6MTc5LCJjbGllbnRfaWQiOjEwfQ.OTb6REW9JtYF9wVUZhXajq4wheU5ULNbM5iEmMCYhhM");
const char* host = "api.slicingdice.com";
int port = 80;
// if false will use test end-point, otherwise production end-point
int useProduction = false;
SlicingDice sd = SlicingDice(apiKey, host, port, useProduction);

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

// Arduino network settings, should match your internet connection properties
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 10 };
byte gateway[] = { 192, 168, 1, 1 };
byte ip[] = { 192, 168, 0, 10 };
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[] = { 255, 255, 255, 0 };
byte dnxs[] = { 8, 8, 8, 8 };
Ethernet.begin(mac, ip, dnxs, gateway, subnet);
Expand All @@ -24,8 +30,9 @@ void loop() {
JsonObject& queryIndex = jsonBuffer.createObject();
JsonObject& nestedQueryIndex = queryIndex.createNestedObject("user1@slicingdice.com");
nestedQueryIndex["age"] = 22;
queryIndex["auto-create-fields"] = true;
sd.index(queryIndex);
Serial.print("Status code: ");
Serial.println(sd.statusCode);
Serial.println(sd.response);
}
}