Skip to content

Instantly share code, notes, and snippets.

@espired
Created June 26, 2024 08:15
Show Gist options
  • Select an option

  • Save espired/2cf24e95bc8f79c1b82b4f2392e7077b to your computer and use it in GitHub Desktop.

Select an option

Save espired/2cf24e95bc8f79c1b82b4f2392e7077b to your computer and use it in GitHub Desktop.
ESP-IDF call
esp_err_t SpotifyApi::sendRequest(const String &endpoint, const String &method, String &response, const char *post_data) const
{
const int MAX_RETRIES = 3;
int retries = 0;
while (retries < MAX_RETRIES)
{
String finalUrl = "https://api.spotify.com/v1" + endpoint;
ESP_LOGI(_log_tag, "Sending request to: %s", finalUrl.c_str());
esp_http_client_config_t config = {
.url = finalUrl.c_str(),
.method = HTTP_METHOD_GET,
.crt_bundle_attach = esp_crt_bundle_attach,
};
if (method == "POST")
{
config.method = HTTP_METHOD_POST;
}
else if (method == "PUT")
{
config.method = HTTP_METHOD_PUT;
}
esp_http_client_handle_t client = esp_http_client_init(&config);
if (client == nullptr)
{
ESP_LOGE(_log_tag, "Failed to initialize HTTP client with URL: %s, Method: %d", config.url, config.method);
return ESP_FAIL;
}
esp_http_client_set_header(client, "Authorization", ("Bearer " + m_accessToken).c_str());
esp_http_client_set_header(client, "Content-Type", "application/json");
esp_http_client_set_header(client, "Accept-Encoding", "gzip"); // Accept gzip encoding
if (post_data != nullptr)
{
esp_http_client_set_post_field(client, post_data, strlen(post_data));
}
esp_err_t err = esp_http_client_perform(client);
if (err == ESP_OK)
{
int status_code = esp_http_client_get_status_code(client);
if (status_code == 401 && retries < MAX_RETRIES - 1)
{
ESP_LOGW(_log_tag, "Received 401 Unauthorized. Refreshing token and retrying...");
esp_http_client_cleanup(client);
// Refresh the token
esp_err_t refresh_err = const_cast<SpotifyApi *>(this)->refreshAccessToken();
if (refresh_err != ESP_OK)
{
ESP_LOGE(_log_tag, "Failed to refresh access token");
return refresh_err;
}
retries++;
continue;
}
int content_length = esp_http_client_get_content_length(client);
ESP_LOGI(_log_tag, "Content length: %d", content_length);
if (content_length > 0)
{
std::vector<char> buffer(content_length + 1); // +1 for null terminator
ESP_LOGI(_log_tag, "Attempting to read response, content_length: %d", content_length);
int read_len = esp_http_client_read(client, buffer.data(), content_length);
ESP_LOGI(_log_tag, "Read response, read_len: %d", read_len);
if (read_len <= 0)
{
ESP_LOGE(_log_tag, "Error reading response, read_len: %d, content_length: %d", read_len, content_length);
esp_http_client_cleanup(client);
return ESP_FAIL;
}
buffer[read_len] = '\0'; // Null-terminate the buffer
response = String(buffer.data(), read_len);
}
else
{
ESP_LOGW(_log_tag, "Content length is 0");
}
ESP_LOGI(_log_tag, "HTTP Status = %d, content_length = %d", status_code, content_length);
ESP_LOGI(_log_tag, "Response: %s", response.c_str());
esp_http_client_cleanup(client);
return ESP_OK;
}
else
{
ESP_LOGE(_log_tag, "HTTP request failed: %s", esp_err_to_name(err));
esp_http_client_cleanup(client);
return err;
}
}
ESP_LOGE(_log_tag, "Max retries reached. Failed to send request");
return ESP_FAIL;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment