Skip to content

Commit 1a1103f

Browse files
authored
Merge pull request #5 from jeoooo/refactor-1
Refactor 1
2 parents 83f824b + 79def77 commit 1a1103f

3 files changed

Lines changed: 13 additions & 25 deletions

File tree

PocketbaseArduino.cpp

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// #include <ESP8266WiFi.h>
66
// #include <ESP8266HTTPClient.h>
77
// #include <BearSSLHelpers.h>
8-
#include <ArduinoJson.h>
98

109
#if defined(ESP8266)
1110
#include <ESP8266HTTPClient.h>
@@ -32,6 +31,12 @@ PocketbaseArduino::PocketbaseArduino(const char *baseUrl)
3231
fields_param = "";
3332
}
3433

34+
PocketbaseArduino &PocketbaseArduino::collection(const char *collection)
35+
{
36+
current_endpoint = "collections/" + String(collection) + "/";
37+
return *this;
38+
}
39+
3540
String performGETRequest(const char *endpoint)
3641
{
3742

@@ -218,7 +223,7 @@ String performDELETERequest(const char *endpoint)
218223
#endif
219224
}
220225

221-
String performPOSTRequest(const char *endpoint, const std::unordered_map<String, String> &params)
226+
String performPOSTRequest(const char *endpoint, const String &requestBody)
222227
{
223228
#if defined(ESP32)
224229
// ESP32 implementation
@@ -235,18 +240,6 @@ String performPOSTRequest(const char *endpoint, const std::unordered_map<String,
235240
{
236241
Serial.print("[HTTPS] POST...\n");
237242

238-
// Create a JSON object to hold the parameters
239-
DynamicJsonDocument jsonBody(1024); // Adjust the capacity based on your needs
240-
241-
for (const auto &param : params)
242-
{
243-
jsonBody[param.first] = param.second;
244-
}
245-
246-
// Serialize the JSON object to a string
247-
String requestBody;
248-
serializeJson(jsonBody, requestBody);
249-
250243
int httpCode = http.POST(requestBody);
251244
if (httpCode > 0)
252245
{
@@ -276,12 +269,6 @@ String performPOSTRequest(const char *endpoint, const std::unordered_map<String,
276269
#endif
277270
}
278271

279-
PocketbaseArduino &PocketbaseArduino::collection(const char *collection)
280-
{
281-
current_endpoint = "collections/" + String(collection) + "/";
282-
return *this;
283-
}
284-
285272
String PocketbaseArduino::getOne(const char *recordId, const char *expand /* = nullptr */, const char *fields /* = nullptr */)
286273
{
287274
String fullEndpoint = base_url + String(current_endpoint) + "records/" + recordId;
@@ -379,11 +366,11 @@ String PocketbaseArduino::deleteRecord(const char *recordId)
379366
return performDELETERequest(fullEndpoint.c_str());
380367
}
381368

382-
String PocketbaseArduino::create(const std::unordered_map<String, String> &params)
369+
String PocketbaseArduino::create(const String &requestBody)
383370
{
384371
// Construct the endpoint based on the current_endpoint
385372
String fullEndpoint = current_endpoint + "records/";
386373

387374
// Call performPOSTRequest with the constructed endpoint and provided parameters
388-
return performPOSTRequest(fullEndpoint.c_str(), params);
375+
return performPOSTRequest(fullEndpoint.c_str(), requestBody);
389376
}

PocketbaseArduino.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
#define PocketbaseArduino_h
55

66
#include "Arduino.h"
7-
#include <ESP8266HTTPClient.h>
87

8+
#include <ESP8266HTTPClient.h>
99
#include <ESP8266WiFi.h>
1010
#include <ESP8266HTTPClient.h>
11+
1112
#include <BearSSLHelpers.h>
1213

1314
class PocketbaseArduino
@@ -90,7 +91,7 @@ class PocketbaseArduino
9091
const char *expand /* = nullptr */,
9192
const char *fields /* = nullptr */);
9293

93-
String create(const std::unordered_map<String, String> &params);
94+
String create(const String &requestBody);
9495

9596
private:
9697
String base_url;

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void loop()
9999
100100
## Contributing
101101
102-
1.[Fork](https://github.com/jeoooo/PocketbaseArduino/fork) this Github repository.
102+
1. [Fork](https://github.com/jeoooo/PocketbaseArduino/fork) this Github repository
103103
2. Create your feature branch (`git checkout -b my-new-feature`).
104104
3. Commit your changes (`git commit -am 'Add some feature`)
105105
4. Push to the branch (`git push origin my-new-feature`)

0 commit comments

Comments
 (0)