Skip to content

Commit

Permalink
refactor(HTTP): refactor get
Browse files Browse the repository at this point in the history
  • Loading branch information
becem-gharbi committed Jun 21, 2024
1 parent b37dc73 commit f117373
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions src/HTTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,42 +29,43 @@ namespace ESPAdmin
};

esp_http_client_handle_t client = esp_http_client_init(&config);
if (client == nullptr)
{
_logger.error("failed to init http client");
return "";
}

esp_http_client_set_header(client, "Api-Key", Store::options.apiKey);

esp_err_t err = esp_http_client_open(client, 0);

if (err == ESP_OK)
if (err != ESP_OK)
{
int contentLength = esp_http_client_fetch_headers(client);

if (contentLength == -1)
{
_logger.error("failed to read");
}
else
{
int statusCode = esp_http_client_get_status_code(client);

if (statusCode < 200 || statusCode >= 300)
{
_logger.error("failed with %d", statusCode);
}
else
{
esp_http_client_read_response(client, response, Store::options.httpMaxResponseSize);
}
}

esp_http_client_close(client);
_logger.error("failed to open connection");
esp_http_client_cleanup(client);
return "";
}

int contentLength = esp_http_client_fetch_headers(client);
if (contentLength == -1)
{
_logger.error("failed to read");
esp_http_client_cleanup(client);
return "";
}
else

int statusCode = esp_http_client_get_status_code(client);
if (statusCode < 200 || statusCode >= 300)
{
_logger.error("failed to open connection");
_logger.error("failed with %d", statusCode);
esp_http_client_cleanup(client);
return "";
}

esp_http_client_read_response(client, response, Store::options.httpMaxResponseSize);

esp_http_client_close(client);
esp_http_client_cleanup(client);

return String(response);
}

Expand Down

0 comments on commit f117373

Please sign in to comment.