diff --git a/include/curl_wrapper.hpp b/include/curl_wrapper.hpp index dcd40ec..c7bd86e 100644 --- a/include/curl_wrapper.hpp +++ b/include/curl_wrapper.hpp @@ -173,6 +173,16 @@ private: * @since 0.1.0 */ void setup_curl(); + + /*! + * @brief Add parameters to URI. + * + * @param uri Reference to the URI. + * @param parameters The parametermap. + * + * @since 0.1.0 + */ + void add_parameters_to_uri(string &uri, const parametermap ¶meters); }; } // namespace mastodonpp diff --git a/src/curl_wrapper.cpp b/src/curl_wrapper.cpp index 0b8b49c..cb047d9 100644 --- a/src/curl_wrapper.cpp +++ b/src/curl_wrapper.cpp @@ -84,50 +84,7 @@ answer_type CURLWrapper::make_request(const http_method &method, string uri, // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) code = curl_easy_setopt(_connection, CURLOPT_HTTPGET, 1L); - for (const auto ¶m : parameters) - { - static constexpr array replace_in_uri - { - "id", "nickname", "nickname_or_id", - "hashtag", "permission_group" - }; - if (any_of(replace_in_uri.begin(), replace_in_uri.end(), - [¶m](const auto &s) { return s == param.first; })) - { - const auto pos{uri.find('<')}; - if (pos != string::npos) - { - uri.replace(pos, param.first.size() + 2, - get(param.second)); - } - continue; - } - static bool first{true}; - if (first) - { - uri += "?"; - first = false; - } - else - { - uri += "&"; - } - if (holds_alternative(param.second)) - { - ((uri += param.first) += "=") += get(param.second); - } - else - { - for (const auto &arg : get>(param.second)) - { - ((uri += param.first) += "[]=") += arg; - if (arg != *get>(param.second).rbegin()) - { - uri += "&"; - } - } - } - } + add_parameters_to_uri(uri, parameters); break; } @@ -266,4 +223,55 @@ void CURLWrapper::setup_curl() curl_easy_setopt(_connection, CURLOPT_MAXREDIRS, 10L); } +void CURLWrapper::add_parameters_to_uri(string &uri, + const parametermap ¶meters) +{ + // Replace with the value of parameter “id” and so on. + for (const auto ¶m : parameters) + { + static constexpr array replace_in_uri + { + "id", "nickname", "nickname_or_id", + "hashtag", "permission_group" + }; + if (any_of(replace_in_uri.begin(), replace_in_uri.end(), + [¶m](const auto &s) { return s == param.first; })) + { + const auto pos{uri.find('<')}; + if (pos != string::npos) + { + uri.replace(pos, param.first.size() + 2, + get(param.second)); + continue; + } + } + + static bool first{true}; + if (first) + { + uri += "?"; + first = false; + } + else + { + uri += "&"; + } + if (holds_alternative(param.second)) + { + ((uri += param.first) += "=") += get(param.second); + } + else + { + for (const auto &arg : get>(param.second)) + { + ((uri += param.first) += "[]=") += arg; + if (arg != *get>(param.second).rbegin()) + { + uri += "&"; + } + } + } + } +} + } // namespace mastodonpp