Skip to content

Commit 9f7bf27

Browse files
committed
added getData function
1 parent be178c9 commit 9f7bf27

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/PxServ.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,62 @@ PxServ::Callback PxServ::setData(String key, String value)
5454
delete client;
5555
return callback;
5656
}
57+
PxServ::Callback PxServ::getData(String key)
58+
{
59+
WiFiClientSecure *client = new WiFiClientSecure;
60+
Callback callback;
61+
62+
callback.status = -1;
63+
callback.message = "failed to send request";
64+
callback.data = "";
65+
66+
if (client)
67+
{
68+
client->setInsecure();
69+
70+
HTTPClient https;
71+
72+
if (https.begin(*client, "https://api.pxserv.net/database/getData"))
73+
{
74+
https.addHeader("Content-Type", "application/json");
75+
76+
JSONVar body;
77+
78+
body["key"] = key;
79+
body["apiKey"] = PxServ::_apiKey;
80+
81+
int httpCode = https.POST(JSON.stringify(body));
82+
if (httpCode > 0)
83+
{
84+
85+
String payload = https.getString();
86+
JSONVar result = JSON.parse(payload);
87+
88+
if (JSON.typeof(result) != "object")
89+
{
90+
callback.status = 400;
91+
callback.message = "Response format not appropriate";
92+
}
93+
else
94+
{
95+
int status = result["status"];
96+
String message = result["message"];
97+
98+
if (status == 200)
99+
{
100+
String value = result["data"]["value"];
101+
callback.data = value;
102+
}
103+
104+
callback.status = status;
105+
callback.message = message;
106+
}
107+
108+
https.end();
109+
}
110+
}
111+
}
112+
113+
delete client;
114+
return callback;
115+
}

src/PxServ.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class PxServ
2121

2222
PxServ(String apiKey);
2323
Callback setData(String key, String value);
24+
Callback getData(String key);
2425

2526
private:
2627
String _apiKey;

0 commit comments

Comments
 (0)