2019-11-09 18:37:52 +01:00
|
|
|
#include "http_request.h"
|
|
|
|
#include "esphome/core/log.h"
|
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace http_request {
|
|
|
|
|
|
|
|
static const char *TAG = "http_request";
|
|
|
|
|
|
|
|
void HttpRequestComponent::dump_config() {
|
|
|
|
ESP_LOGCONFIG(TAG, "HTTP Request:");
|
|
|
|
ESP_LOGCONFIG(TAG, " Timeout: %ums", this->timeout_);
|
|
|
|
ESP_LOGCONFIG(TAG, " User-Agent: %s", this->useragent_);
|
|
|
|
}
|
|
|
|
|
2020-12-03 19:37:00 +01:00
|
|
|
void HttpRequestComponent::set_url(std::string url) {
|
|
|
|
this->url_ = url;
|
|
|
|
this->secure_ = url.compare(0, 6, "https:") == 0;
|
|
|
|
|
|
|
|
if (!this->last_url_.empty() && this->url_ != this->last_url_) {
|
|
|
|
// Close connection if url has been changed
|
|
|
|
this->client_.setReuse(false);
|
|
|
|
this->client_.end();
|
|
|
|
}
|
|
|
|
this->client_.setReuse(true);
|
|
|
|
}
|
|
|
|
|
2019-11-09 18:37:52 +01:00
|
|
|
void HttpRequestComponent::send() {
|
|
|
|
bool begin_status = false;
|
2020-07-21 22:26:21 +02:00
|
|
|
const String url = this->url_.c_str();
|
2019-11-09 18:37:52 +01:00
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
2020-07-21 22:26:21 +02:00
|
|
|
begin_status = this->client_.begin(url);
|
2019-11-09 18:37:52 +01:00
|
|
|
#endif
|
|
|
|
#ifdef ARDUINO_ARCH_ESP8266
|
|
|
|
#ifndef CLANG_TIDY
|
|
|
|
this->client_.setFollowRedirects(true);
|
|
|
|
this->client_.setRedirectLimit(3);
|
2020-07-21 22:26:21 +02:00
|
|
|
begin_status = this->client_.begin(*this->get_wifi_client_(), url);
|
2019-11-09 18:37:52 +01:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!begin_status) {
|
|
|
|
this->client_.end();
|
|
|
|
this->status_set_warning();
|
|
|
|
ESP_LOGW(TAG, "HTTP Request failed at the begin phase. Please check the configuration");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->client_.setTimeout(this->timeout_);
|
|
|
|
if (this->useragent_ != nullptr) {
|
|
|
|
this->client_.setUserAgent(this->useragent_);
|
|
|
|
}
|
|
|
|
for (const auto &header : this->headers_) {
|
|
|
|
this->client_.addHeader(header.name, header.value, false, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
int http_code = this->client_.sendRequest(this->method_, this->body_.c_str());
|
|
|
|
if (http_code < 0) {
|
2020-05-01 04:05:11 +02:00
|
|
|
ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Error: %s", this->url_.c_str(),
|
|
|
|
HTTPClient::errorToString(http_code).c_str());
|
2019-11-09 18:37:52 +01:00
|
|
|
this->status_set_warning();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (http_code < 200 || http_code >= 300) {
|
2020-05-01 04:05:11 +02:00
|
|
|
ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Code: %d", this->url_.c_str(), http_code);
|
2019-11-09 18:37:52 +01:00
|
|
|
this->status_set_warning();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->status_clear_warning();
|
2020-05-01 04:05:11 +02:00
|
|
|
ESP_LOGD(TAG, "HTTP Request completed; URL: %s; Code: %d", this->url_.c_str(), http_code);
|
2019-11-09 18:37:52 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 01:27:05 +01:00
|
|
|
#ifdef ARDUINO_ARCH_ESP8266
|
|
|
|
WiFiClient *HttpRequestComponent::get_wifi_client_() {
|
|
|
|
if (this->secure_) {
|
|
|
|
if (this->wifi_client_secure_ == nullptr) {
|
|
|
|
this->wifi_client_secure_ = new BearSSL::WiFiClientSecure();
|
|
|
|
this->wifi_client_secure_->setInsecure();
|
|
|
|
this->wifi_client_secure_->setBufferSizes(512, 512);
|
|
|
|
}
|
|
|
|
return this->wifi_client_secure_;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->wifi_client_ == nullptr) {
|
|
|
|
this->wifi_client_ = new WiFiClient();
|
|
|
|
}
|
|
|
|
return this->wifi_client_;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-12-03 19:37:00 +01:00
|
|
|
void HttpRequestComponent::close() {
|
|
|
|
this->last_url_ = this->url_;
|
|
|
|
this->client_.end();
|
|
|
|
}
|
2020-03-12 01:27:05 +01:00
|
|
|
|
2020-05-01 04:05:11 +02:00
|
|
|
const char *HttpRequestComponent::get_string() {
|
|
|
|
static const String STR = this->client_.getString();
|
|
|
|
return STR.c_str();
|
|
|
|
}
|
2020-03-12 01:27:05 +01:00
|
|
|
|
2019-11-09 18:37:52 +01:00
|
|
|
} // namespace http_request
|
|
|
|
} // namespace esphome
|