diff options
author | tastytea | 2020-01-05 10:35:38 +0100 |
---|---|---|
committer | tastytea | 2020-01-05 10:35:38 +0100 |
commit | 9b49bc1d174d4d25ed5ade18c222640e91e514a4 (patch) | |
tree | 960724c4b56c41e223a57247d19997dcf8863f41 | |
parent | db315a3a70fb4c428171609d879d4728b8fa552d (diff) | |
download | mastodonpp-9b49bc1d174d4d25ed5ade18c222640e91e514a4.tar mastodonpp-9b49bc1d174d4d25ed5ade18c222640e91e514a4.tar.gz mastodonpp-9b49bc1d174d4d25ed5ade18c222640e91e514a4.zip |
Optimize request-flow. 😃
-rw-r--r-- | include/api.hpp | 10 | ||||
-rw-r--r-- | include/connection.hpp | 7 | ||||
-rw-r--r-- | include/curl_wrapper.hpp | 4 | ||||
-rw-r--r-- | src/api.cpp | 9 | ||||
-rw-r--r-- | src/connection.cpp | 15 | ||||
-rw-r--r-- | src/curl_wrapper.cpp | 6 |
6 files changed, 27 insertions, 24 deletions
diff --git a/include/api.hpp b/include/api.hpp index 2fb26d8..72ca3f7 100644 --- a/include/api.hpp +++ b/include/api.hpp @@ -18,13 +18,13 @@ #define MASTODONPP_API_HPP #include <map> -#include <string> +#include <string_view> #include <variant> namespace mastodonpp { -using std::string; +using std::string_view; using std::variant; /*! @@ -76,15 +76,15 @@ public: * * @since 0.1.0 */ - explicit API(endpoint_type &endpoint); + explicit API(const endpoint_type &endpoint); /*! - * @brief Convert #endpoint_type to string. + * @brief Convert #endpoint_type to `std::string_view`. * * @since 0.1.0 */ [[nodiscard]] - string to_string() const; + string_view to_string_view() const; private: const endpoint_type _endpoint; diff --git a/include/connection.hpp b/include/connection.hpp index 1db8286..a26120c 100644 --- a/include/connection.hpp +++ b/include/connection.hpp @@ -23,11 +23,13 @@ #include "return_types.hpp" #include <string> +#include <string_view> namespace mastodonpp { using std::string; +using std::string_view; /*! * @brief Represents a connection to an instance. Used for requests. @@ -57,7 +59,7 @@ public: * @since 0.1.0 */ [[nodiscard]] - answer_type get(API::endpoint_type endpoint); + answer_type get(const API::endpoint_type &endpoint); /*! * @brief Make a HTTP GET call. @@ -67,10 +69,11 @@ public: * @since 0.1.0 */ [[nodiscard]] - answer_type get(string endpoint); + answer_type get(const string_view &endpoint); private: Instance &_instance; + const string_view _baseuri; }; } // namespace mastodonpp diff --git a/include/curl_wrapper.hpp b/include/curl_wrapper.hpp index 6a088ff..c98a6a9 100644 --- a/include/curl_wrapper.hpp +++ b/include/curl_wrapper.hpp @@ -17,6 +17,8 @@ #ifndef MASTODONPP_CURL_WRAPPER_HPP #define MASTODONPP_CURL_WRAPPER_HPP +#include "return_types.hpp" + #include "curl/curl.h" #include <string> @@ -89,7 +91,7 @@ public: * @since 0.1.0 */ [[nodiscard]] - string make_request(const http_method &meth, const string_view &uri); + answer_type make_request(const http_method &meth, const string_view &uri); private: CURL *_connection; diff --git a/src/api.cpp b/src/api.cpp index 30a11b9..5168e83 100644 --- a/src/api.cpp +++ b/src/api.cpp @@ -17,21 +17,18 @@ #include "api.hpp" #include <map> -#include <string_view> -#include <utility> namespace mastodonpp { using std::map; using std::string_view; -using std::move; -API::API(endpoint_type &endpoint) - : _endpoint{move(endpoint)} +API::API(const endpoint_type &endpoint) + : _endpoint{endpoint} {} -string API::to_string() const +string_view API::to_string_view() const { static const map<endpoint_type,string_view> endpoint_map { diff --git a/src/connection.cpp b/src/connection.cpp index b7fe886..e6bca95 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -21,20 +21,19 @@ namespace mastodonpp Connection::Connection(Instance &instance) : _instance{instance} + , _baseuri{instance.get_baseuri()} {} -answer_type Connection::get(API::endpoint_type endpoint) +answer_type Connection::get(const API::endpoint_type &endpoint) { - answer_type answer; - answer.body = API{endpoint}.to_string(); - return answer; + return make_request( + http_method::GET, + string(_baseuri).append(API{endpoint}.to_string_view())); } -answer_type Connection::get(string endpoint) +answer_type Connection::get(const string_view &endpoint) { - answer_type answer; - answer.body = make_request(http_method::GET, "https://ip.tastytea.de/"); - return answer; + return make_request(http_method::GET, string(_baseuri).append(endpoint)); } } // namespace mastodonpp diff --git a/src/curl_wrapper.cpp b/src/curl_wrapper.cpp index 32d6056..ac534bb 100644 --- a/src/curl_wrapper.cpp +++ b/src/curl_wrapper.cpp @@ -33,7 +33,7 @@ CURLWrapper::~CURLWrapper() noexcept curl_global_cleanup(); } -string CURLWrapper::make_request(const http_method &meth, +answer_type CURLWrapper::make_request(const http_method &meth, const string_view &uri) { CURLcode code; @@ -72,7 +72,9 @@ string CURLWrapper::make_request(const http_method &meth, _curl_buffer_error}; } - return _curl_buffer; + answer_type answer; + answer.body = _curl_buffer; + return answer; } int CURLWrapper::writer(char *data, size_t size, size_t nmemb, |