scheduler http tests

This commit is contained in:
proddy
2024-07-20 13:22:01 +02:00
parent 353e1f4460
commit 809c5c7ead
4 changed files with 65 additions and 6 deletions

View File

@@ -0,0 +1,27 @@
#ifndef HTTPClient_H_
#define HTTPClient_H_
#include "WString.h"
class HTTPClient {
public:
// HTTPClient();
// ~HTTPClient();
bool begin(String url) {
return true;
};
void end(void) {};
int GET() {
return 200;
};
int POST(String payload) {
return 200;
};
void addHeader(const String & name, const String & value, bool first = false, bool replace = true) {};
String getString(void) {
return "Hello, World!";
};
};
#endif /* HTTPClient_H_ */

View File

@@ -53,6 +53,14 @@ class String {
return 1; return 1;
} }
int len() const {
return _str.size();
}
bool startsWith(const char * prefix) const {
return _str.find(prefix) == 0;
}
private: private:
std::string _str; std::string _str;
}; };

View File

@@ -342,22 +342,44 @@ bool WebSchedulerService::command(const char * name, const char * cmd, const cha
HTTPClient http; HTTPClient http;
String url = doc["url"]; String url = doc["url"];
if (http.begin(url)) { if (http.begin(url)) {
// It's an HTTP call
// add any given headers
for (JsonPair p : doc["header"].as<JsonObject>()) { for (JsonPair p : doc["header"].as<JsonObject>()) {
http.addHeader(p.key().c_str(), p.value().as<String>().c_str()); http.addHeader(p.key().c_str(), p.value().as<String>().c_str());
} }
String value = doc["value"] | ""; String value = doc["value"] | data; // extract value if its in the command, or take the data
String method = doc["method"] | "GET"; // default GET
// if there is data, force a POST
if (value.length()) { if (value.length()) {
if (value.startsWith("{")) {
http.addHeader("Content-Type", "application/json"); // auto-set to JSON
}
httpResult = http.POST(value); httpResult = http.POST(value);
} else if (data && data[0] != '\0') { // post
httpResult = http.POST(String(data));
} else { } else {
httpResult = http.GET(); // no value, but check if it still a POST request
if (method == "POST") {
httpResult = http.POST(value);
} else {
httpResult = http.GET(); // normal GET
} }
}
http.end(); http.end();
// check HTTP return code
if (httpResult != 200) {
char error[100];
snprintf(error, sizeof(error), "Schedule %s: URL command failed with http code %d", name, httpResult);
emsesp::EMSESP::logger().warning(error);
return false;
}
return true;
} }
} }
return httpResult > 0;
} }
JsonDocument doc_input; JsonDocument doc_input;
JsonObject input = doc_input.to<JsonObject>(); JsonObject input = doc_input.to<JsonObject>();
if (strlen(data)) { // empty data queries a value if (strlen(data)) { // empty data queries a value
@@ -562,6 +584,8 @@ void WebSchedulerService::test() {
// (14 - 40) * 2.8 + 5 = -67.8 // (14 - 40) * 2.8 + 5 = -67.8
test_value = "(custom/seltemp - boiler/flowtempoffset) * 2.8 + 5"; test_value = "(custom/seltemp - boiler/flowtempoffset) * 2.8 + 5";
command("test", test_cmd.c_str(), compute(test_value).c_str()); command("test", test_cmd.c_str(), compute(test_value).c_str());
// TODO add some HTTP/URI tests
} }
#endif #endif