Made http_client movable and added missing APIs

This commit is contained in:
2021-04-15 21:07:14 +02:00
parent d8af81ddd9
commit 5e7f5775ee

View File

@ -1,23 +1,31 @@
#pragma once
// system includes
#include <utility>
// espressif includes
#include <esp_http_client.h>
// local includes
#include "cppmacros.h"
namespace espcpputils {
class http_client
{
CPP_DISABLE_COPY_MOVE(http_client)
public:
http_client(const esp_http_client_config_t *config) :
handle{esp_http_client_init(config)}
{}
http_client() : handle{NULL} {}
http_client(const esp_http_client_config_t *config) : handle{esp_http_client_init(config)} {}
http_client(const http_client &) = delete;
http_client(http_client &&other) : handle{std::move(other.handle)} { other.handle = NULL; }
http_client &operator=(const http_client &) = delete;
http_client &operator=(http_client &&other) { if (handle) esp_http_client_cleanup(handle); handle = std::move(other.handle); other.handle = NULL; return *this; }
~http_client() { if (handle) esp_http_client_cleanup(handle); }
operator bool() const { return handle != NULL; }
esp_err_t perform() { return esp_http_client_perform(handle); }
esp_err_t set_url(const char *url) { return esp_http_client_set_url(handle, url); }
esp_err_t set_post_field(const char *data, int len) { return esp_http_client_set_post_field(handle, data, len); }
@ -42,7 +50,12 @@ public:
esp_err_t set_redirection() { return esp_http_client_set_redirection(handle); }
void add_auth() { return esp_http_client_add_auth(handle); }
bool is_complete_data_received() const { return esp_http_client_is_complete_data_received(handle); }
int read_response(char *buffer, int len) { return esp_http_client_read_response(handle, buffer, len); }
esp_err_t flush_response(int *len) { return esp_http_client_flush_response(handle, len); }
esp_err_t get_url(char *url, const int len) const { return esp_http_client_get_url(handle, url, len); }
esp_err_t get_chunk_length(int *len) const { return esp_http_client_get_chunk_length(handle, len); }
const esp_http_client_handle_t handle;
private:
esp_http_client_handle_t handle;
};
} // namespace espcpputils