diff options
author | tastytea | 2020-01-12 17:21:58 +0100 |
---|---|---|
committer | tastytea | 2020-01-12 17:24:02 +0100 |
commit | 975fe576775aa67008ec0134337cd1cd2ba201e6 (patch) | |
tree | aef8717a1ccb6b6be8d8401c4c6a478c331ec34e /src | |
parent | b4428ed82379c4a9bcfa9c42d6980c267fce20d7 (diff) | |
download | mastodonpp-975fe576775aa67008ec0134337cd1cd2ba201e6.tar mastodonpp-975fe576775aa67008ec0134337cd1cd2ba201e6.tar.gz mastodonpp-975fe576775aa67008ec0134337cd1cd2ba201e6.zip |
Simplify connection setup.
By adding CURLWrapper::setup_connection_properties.
Diffstat (limited to 'src')
-rw-r--r-- | src/connection.cpp | 20 | ||||
-rw-r--r-- | src/curl_wrapper.cpp | 40 | ||||
-rw-r--r-- | src/instance.cpp | 27 |
3 files changed, 30 insertions, 57 deletions
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<API::endpoint_type>(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<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) |