Skip to content

Commit 72dae8b

Browse files
committed
add create and exists methods to dashboards
1 parent 46c5755 commit 72dae8b

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

AdafruitIO_Dashboard.cpp

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,50 @@
1212
#include "AdafruitIO_Dashboard.h"
1313
#include "AdafruitIO.h"
1414

15-
AdafruitIO_Dashboard::AdafruitIO_Dashboard(AdafruitIO *io, const char *k)
15+
AdafruitIO_Dashboard::AdafruitIO_Dashboard(AdafruitIO *io, const char *n)
1616
{
1717
_io = io;
18-
key = k;
18+
name = n;
1919
}
2020

2121
AdafruitIO_Dashboard::~AdafruitIO_Dashboard(){}
22+
23+
bool AdafruitIO_Dashboard::exists()
24+
{
25+
String url = "/api/v2/";
26+
url += _io->_username;
27+
url += "/dashboards/";
28+
url += name;
29+
30+
_io->_http->startRequest(url.c_str(), HTTP_METHOD_GET);
31+
_io->_http->sendHeader("X-AIO-Key", _io->_key);
32+
_io->_http->endRequest();
33+
34+
int status = _io->_http->responseStatusCode();
35+
_io->_http->responseBody(); // needs to be read even if not used
36+
37+
return status == 200;
38+
}
39+
40+
bool AdafruitIO_Dashboard::create()
41+
{
42+
String url = "/api/v2/";
43+
url += _io->_username;
44+
url += "/dashboards";
45+
46+
String body = "name=";
47+
body += name;
48+
49+
_io->_http->startRequest(url.c_str(), HTTP_METHOD_POST);
50+
_io->_http->sendHeader(HTTP_HEADER_CONTENT_TYPE, "application/x-www-form-urlencoded");
51+
_io->_http->sendHeader(HTTP_HEADER_CONTENT_LENGTH, body.length());
52+
_io->_http->sendHeader("X-AIO-Key", _io->_key);
53+
_io->_http->endRequest();
54+
_io->_http->write((const byte*)body.c_str(), body.length());
55+
56+
int status = _io->_http->responseStatusCode();
57+
_io->_http->responseBody(); // needs to be read even if not used
58+
59+
return status == 201;
60+
}
61+

AdafruitIO_Dashboard.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ class AdafruitIO;
2121
class AdafruitIO_Dashboard {
2222

2323
public:
24-
AdafruitIO_Dashboard(AdafruitIO *io, const char *key);
24+
AdafruitIO_Dashboard(AdafruitIO *io, const char *name);
2525
~AdafruitIO_Dashboard();
26-
const char *key;
26+
27+
const char *name;
28+
29+
bool exists();
30+
bool create();
2731

2832
private:
2933
AdafruitIO *_io;

0 commit comments

Comments
 (0)