Send the access token.

This commit is contained in:
tastytea 2020-01-10 14:26:50 +01:00
parent 1910e780b0
commit c3bb9a20c4
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 39 additions and 0 deletions

View File

@ -199,6 +199,13 @@ protected:
return _curl_buffer_body; return _curl_buffer_body;
} }
/*!
* @brief Set OAuth 2.0 Bearer Access Token.
*
* @since 0.1.0
*/
void set_access_token(const string_view access_token);
private: private:
CURL *_connection; CURL *_connection;
char _curl_buffer_error[CURL_ERROR_SIZE]; char _curl_buffer_error[CURL_ERROR_SIZE];

View File

@ -30,6 +30,11 @@ Connection::Connection(Instance &instance)
{ {
CURLWrapper::set_proxy(proxy); CURLWrapper::set_proxy(proxy);
} }
if (!_instance.get_access_token().empty())
{
CURLWrapper::set_access_token(_instance.get_access_token());
}
} }
string Connection::endpoint_to_uri(const endpoint_variant &endpoint) const string Connection::endpoint_to_uri(const endpoint_variant &endpoint) const

View File

@ -166,6 +166,33 @@ answer_type CURLWrapper::make_request(const http_method &method, string uri,
return answer; return answer;
} }
void CURLWrapper::set_access_token(const string_view access_token)
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
CURLcode code{curl_easy_setopt(_connection, CURLOPT_XOAUTH2_BEARER,
access_token.data())};
if (code != CURLE_OK)
{
throw CURLException{code, "Could not set authorization token.",
_curl_buffer_error};
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
code = curl_easy_setopt(_connection, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
if (code == CURLE_NOT_BUILT_IN) // libcurl < 7.61.0.
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
code = curl_easy_setopt(_connection, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
}
if (code != CURLE_OK)
{
throw CURLException{code, "Could not set authorization token.",
_curl_buffer_error};
}
debuglog << "Set authorization token.\n";
}
size_t CURLWrapper::writer_body(char *data, size_t size, size_t nmemb) size_t CURLWrapper::writer_body(char *data, size_t size, size_t nmemb)
{ {
if(data == nullptr) if(data == nullptr)