diff --git a/include/connection.hpp b/include/connection.hpp index 761f777..27119e4 100644 --- a/include/connection.hpp +++ b/include/connection.hpp @@ -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. diff --git a/include/curl_wrapper.hpp b/include/curl_wrapper.hpp index f8e567b..0759e01 100644 --- a/include/curl_wrapper.hpp +++ b/include/curl_wrapper.hpp @@ -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. diff --git a/include/instance.hpp b/include/instance.hpp index 6185b7f..85756e9 100644 --- a/include/instance.hpp +++ b/include/instance.hpp @@ -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`. diff --git a/src/connection.cpp b/src/connection.cpp index 0eba695..77e7ad0 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -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(endpoint)) diff --git a/src/curl_wrapper.cpp b/src/curl_wrapper.cpp index 4cfb9b5..50e20ab 100644 --- a/src/curl_wrapper.cpp +++ b/src/curl_wrapper.cpp @@ -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) diff --git a/src/instance.cpp b/src/instance.cpp index 19c6b8b..cd99645 100644 --- a/src/instance.cpp +++ b/src/instance.cpp @@ -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 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)