|
@@ -2,6 +2,7 @@
|
|
|
|
|
|
#include <freertos/task.h>
|
|
|
#include <esp_log.h>
|
|
|
+#include <kbf/exception.h>
|
|
|
|
|
|
#include "kbf/macros.h"
|
|
|
|
|
@@ -62,7 +63,8 @@ http::Client::performRequest(const kbf::http::Method method, const std::string &
|
|
|
esp_http_client_set_header(handle, "Content-Type", "application/json");
|
|
|
esp_http_client_set_post_field(handle, body.c_str(), body.length());
|
|
|
} else {
|
|
|
- ESP_LOGW(TAG, "unhandled method: %d", method);
|
|
|
+ ESP_LOGE(TAG, "unhandled method: %d", method);
|
|
|
+ throw KBFError("unknown HTTP method " + std::to_string(method));
|
|
|
}
|
|
|
|
|
|
if (headers) {
|
|
@@ -89,16 +91,23 @@ http::Client::performRequest(const kbf::http::Method method, const std::string &
|
|
|
} else if (err == ESP_OK) {
|
|
|
ESP_LOGD(TAG, "success");
|
|
|
return response;
|
|
|
- } else if (err == ESP_ERR_HTTP_FETCH_HEADER && !retry) {
|
|
|
+ } else if ((err == ESP_ERR_HTTP_FETCH_HEADER || err == ESP_FAIL) && !retry) {
|
|
|
// TODO this will happen on timeout as well; perhaps set a retry limit in ctor / get / post?
|
|
|
- ESP_LOGW(TAG, "fetching headers failed, maybe the connection was dropped? retrying...");
|
|
|
+ ESP_LOGW(TAG, "request failed, maybe the connection was dropped? retrying...");
|
|
|
retry = true;
|
|
|
esp_http_client_cleanup(handle);
|
|
|
init();
|
|
|
- return get(url);
|
|
|
+ if (method == http::GET) {
|
|
|
+ return get(url, headers);
|
|
|
+ } else if (method == http::POST) {
|
|
|
+ return post(url, *postData, headers);
|
|
|
+ } else {
|
|
|
+ throw KBFError("unknown HTTP method " + std::to_string(method));
|
|
|
+ }
|
|
|
} else {
|
|
|
- ESP_LOGE(TAG, "HTTP GET failed: %s", esp_err_to_name(err));
|
|
|
+ ESP_LOGE(TAG, "HTTP request failed: %s", esp_err_to_name(err));
|
|
|
if (onError) onError(*this);
|
|
|
+ running = false;
|
|
|
return nullptr;
|
|
|
}
|
|
|
}
|