From c93810e24186afffeaa980f6516e8b673362440e Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 8 Jan 2020 17:51:53 +0100 Subject: [PATCH] Move URI building for GET requets to add_parameters_to_uri(). --- include/curl_wrapper.hpp | 10 +++++ src/curl_wrapper.cpp | 96 ++++++++++++++++++++++------------------ 2 files changed, 62 insertions(+), 44 deletions(-) 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