Simplify connection setup.
By adding CURLWrapper::setup_connection_properties.
This commit is contained in:
parent
b4428ed823
commit
975fe57677
|
@ -82,7 +82,12 @@ public:
|
|||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
explicit Connection(Instance &instance);
|
||||
explicit Connection(Instance &instance)
|
||||
: _instance{instance}
|
||||
, _baseuri{instance.get_baseuri()}
|
||||
{
|
||||
_instance.copy_connection_properties(*this);
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Make a HTTP GET call with parameters.
|
||||
|
|
|
@ -189,6 +189,16 @@ public:
|
|||
return sbuf;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Set some properties of the connection.
|
||||
*
|
||||
* Meant for internal use. See Instance::copy_connection_properties().
|
||||
*
|
||||
* @since 0.3.0
|
||||
*/
|
||||
void setup_connection_properties(string_view proxy, string_view access_token,
|
||||
string_view cainfo);
|
||||
|
||||
protected:
|
||||
/*!
|
||||
* @brief Mutex for #get_buffer a.k.a. _curl_buffer_body.
|
||||
|
|
|
@ -53,7 +53,28 @@ public:
|
|||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
explicit Instance(string_view hostname, string_view access_token);
|
||||
explicit Instance(const string_view hostname,
|
||||
const string_view access_token)
|
||||
: _hostname{hostname}
|
||||
, _baseuri{"https://" + _hostname}
|
||||
, _access_token{access_token}
|
||||
, _max_chars{0}
|
||||
{}
|
||||
|
||||
/*!
|
||||
* @brief Set the properties of the connection of the calling class up.
|
||||
*
|
||||
* Meant for internal use. This aligns the properties of the connection of
|
||||
* the calling class with the properties of connection of this class.
|
||||
*
|
||||
* @param curlwrapper The CURLWrapper parent of the calling class.
|
||||
*
|
||||
* @since 0.3.0
|
||||
*/
|
||||
inline void copy_connection_properties(CURLWrapper &curlwrapper)
|
||||
{
|
||||
curlwrapper.setup_connection_properties(_proxy, _access_token, _cainfo);
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Returns the hostname.
|
||||
|
@ -223,7 +244,12 @@ public:
|
|||
class ObtainToken : public CURLWrapper
|
||||
{
|
||||
public:
|
||||
ObtainToken(Instance &instance);
|
||||
ObtainToken(Instance &instance)
|
||||
: _instance{instance}
|
||||
, _baseuri{instance.get_baseuri()}
|
||||
{
|
||||
_instance.copy_connection_properties(*this);
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Creates an application via `/api/v1/apps`.
|
||||
|
|
|
@ -21,26 +21,6 @@ namespace mastodonpp
|
|||
|
||||
using std::holds_alternative;
|
||||
|
||||
Connection::Connection(Instance &instance)
|
||||
: _instance{instance}
|
||||
, _baseuri{instance.get_baseuri()}
|
||||
{
|
||||
auto proxy{_instance.get_proxy()};
|
||||
if (!proxy.empty())
|
||||
{
|
||||
CURLWrapper::set_proxy(proxy);
|
||||
}
|
||||
|
||||
if (!_instance.get_access_token().empty())
|
||||
{
|
||||
CURLWrapper::set_access_token(_instance.get_access_token());
|
||||
}
|
||||
if (!_instance.get_cainfo().empty())
|
||||
{
|
||||
CURLWrapper::set_cainfo(_instance.get_cainfo());
|
||||
}
|
||||
}
|
||||
|
||||
string Connection::endpoint_to_uri(const endpoint_variant &endpoint) const
|
||||
{
|
||||
if (holds_alternative<API::endpoint_type>(endpoint))
|
||||
|
|
|
@ -64,16 +64,6 @@ CURLWrapper::~CURLWrapper() noexcept
|
|||
}
|
||||
}
|
||||
|
||||
void CURLWrapper::set_proxy(const string_view proxy)
|
||||
{
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
||||
CURLcode code{curl_easy_setopt(_connection, CURLOPT_PROXY, proxy.data())};
|
||||
if (code != CURLE_OK)
|
||||
{
|
||||
throw CURLException{code, "Failed to set proxy", _curl_buffer_error};
|
||||
}
|
||||
}
|
||||
|
||||
answer_type CURLWrapper::make_request(const http_method &method, string uri,
|
||||
const parametermap ¶meters)
|
||||
{
|
||||
|
@ -197,6 +187,36 @@ answer_type CURLWrapper::make_request(const http_method &method, string uri,
|
|||
return answer;
|
||||
}
|
||||
|
||||
void CURLWrapper::setup_connection_properties(string_view proxy,
|
||||
string_view access_token,
|
||||
string_view cainfo)
|
||||
{
|
||||
if (!proxy.empty())
|
||||
{
|
||||
set_proxy(proxy);
|
||||
}
|
||||
|
||||
if (!access_token.empty())
|
||||
{
|
||||
set_access_token(access_token);
|
||||
}
|
||||
|
||||
if (!cainfo.empty())
|
||||
{
|
||||
set_cainfo(cainfo);
|
||||
}
|
||||
}
|
||||
|
||||
void CURLWrapper::set_proxy(const string_view proxy)
|
||||
{
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
||||
CURLcode code{curl_easy_setopt(_connection, CURLOPT_PROXY, proxy.data())};
|
||||
if (code != CURLE_OK)
|
||||
{
|
||||
throw CURLException{code, "Failed to set proxy", _curl_buffer_error};
|
||||
}
|
||||
}
|
||||
|
||||
void CURLWrapper::set_access_token(const string_view access_token)
|
||||
{
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-signed-bitwise)
|
||||
|
|
|
@ -32,13 +32,6 @@ using std::regex;
|
|||
using std::regex_search;
|
||||
using std::smatch;
|
||||
|
||||
Instance::Instance(const string_view hostname, const string_view access_token)
|
||||
: _hostname{hostname}
|
||||
, _baseuri{"https://" + _hostname}
|
||||
, _access_token{access_token}
|
||||
, _max_chars{0}
|
||||
{}
|
||||
|
||||
uint64_t Instance::get_max_chars() noexcept
|
||||
{
|
||||
constexpr uint64_t default_max_chars{500};
|
||||
|
@ -162,26 +155,6 @@ vector<string> Instance::get_post_formats() noexcept
|
|||
return _post_formats;
|
||||
}
|
||||
|
||||
Instance::ObtainToken::ObtainToken(Instance &instance)
|
||||
: _instance{instance}
|
||||
, _baseuri{instance.get_baseuri()}
|
||||
{
|
||||
auto proxy{_instance.get_proxy()};
|
||||
if (!proxy.empty())
|
||||
{
|
||||
CURLWrapper::set_proxy(proxy);
|
||||
}
|
||||
|
||||
if (!_instance.get_access_token().empty())
|
||||
{
|
||||
CURLWrapper::set_access_token(_instance.get_access_token());
|
||||
}
|
||||
if (!_instance.get_cainfo().empty())
|
||||
{
|
||||
CURLWrapper::set_cainfo(_instance.get_cainfo());
|
||||
}
|
||||
}
|
||||
|
||||
answer_type Instance::ObtainToken::step_1(const string_view client_name,
|
||||
const string_view scopes,
|
||||
const string_view website)
|
||||
|
|
Loading…
Reference in New Issue
Block a user