59 lines
1.8 KiB
C++
59 lines
1.8 KiB
C++
#pragma once
|
|
#include <memory>
|
|
#include "esp_http_client.h"
|
|
#include "network/wifi_handler.h"
|
|
|
|
// forward declare NetworkHandler to avoid circular include with network.h
|
|
class NetworkHandler;
|
|
|
|
// default config values for esp_http_client_config_t
|
|
// disable Wmissing-field-initializers warning for these structs
|
|
#pragma GCC diagnostic push
|
|
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
|
|
static const inline esp_http_client_config_t DEFAULT_HTTP_CLIENT_CONFIG = {
|
|
.timeout_ms = 10000,
|
|
};
|
|
|
|
static const inline esp_http_client_config_t DEFAULT_HTTP_CLIENT_CONFIG_HTTPS = {
|
|
.transport_type = HTTP_TRANSPORT_OVER_SSL,
|
|
//
|
|
.use_global_ca_store = true,
|
|
.skip_cert_common_name_check = false,
|
|
};
|
|
#pragma GCC diagnostic pop
|
|
|
|
// esp http client wrapper with automatic initialization and cleanup
|
|
class HttpHandler {
|
|
public:
|
|
~HttpHandler();
|
|
|
|
esp_err_t set_method(esp_http_client_method_t method);
|
|
esp_err_t set_header(const char* header, const char* value);
|
|
esp_err_t set_post_field(const char* field, size_t len);
|
|
//
|
|
esp_err_t perform_request();
|
|
// buffer is allocated inside the method, caller must free it
|
|
void get_body(
|
|
char*& buffer,
|
|
int& total_len
|
|
);
|
|
|
|
|
|
// only NetworkHandler can create HttpHandler instances
|
|
friend class NetworkHandler;
|
|
friend esp_err_t http_event_handler(esp_http_client_event_t *evt);
|
|
// disable copy constructor and assignment operator
|
|
HttpHandler(const HttpHandler&) = delete;
|
|
HttpHandler& operator=(const HttpHandler&) = delete;
|
|
|
|
private:
|
|
// private constructor, only NetworkHandler can create instances
|
|
HttpHandler(const esp_http_client_config_t&& config, WifiHandler* wifiHandler);
|
|
|
|
esp_http_client_handle_t client;
|
|
// backreference to WifiHandler to ensure WiFi is connected, DO NOT DELETE
|
|
WifiHandler* wifiHandler;
|
|
char* response_buffer;
|
|
size_t response_size;
|
|
};
|