Optimize request-flow. 😃

This commit is contained in:
tastytea 2020-01-05 10:35:38 +01:00
parent db315a3a70
commit 9b49bc1d17
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
6 changed files with 27 additions and 24 deletions

View File

@ -18,13 +18,13 @@
#define MASTODONPP_API_HPP #define MASTODONPP_API_HPP
#include <map> #include <map>
#include <string> #include <string_view>
#include <variant> #include <variant>
namespace mastodonpp namespace mastodonpp
{ {
using std::string; using std::string_view;
using std::variant; using std::variant;
/*! /*!
@ -76,15 +76,15 @@ public:
* *
* @since 0.1.0 * @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 * @since 0.1.0
*/ */
[[nodiscard]] [[nodiscard]]
string to_string() const; string_view to_string_view() const;
private: private:
const endpoint_type _endpoint; const endpoint_type _endpoint;

View File

@ -23,11 +23,13 @@
#include "return_types.hpp" #include "return_types.hpp"
#include <string> #include <string>
#include <string_view>
namespace mastodonpp namespace mastodonpp
{ {
using std::string; using std::string;
using std::string_view;
/*! /*!
* @brief Represents a connection to an instance. Used for requests. * @brief Represents a connection to an instance. Used for requests.
@ -57,7 +59,7 @@ public:
* @since 0.1.0 * @since 0.1.0
*/ */
[[nodiscard]] [[nodiscard]]
answer_type get(API::endpoint_type endpoint); answer_type get(const API::endpoint_type &endpoint);
/*! /*!
* @brief Make a HTTP GET call. * @brief Make a HTTP GET call.
@ -67,10 +69,11 @@ public:
* @since 0.1.0 * @since 0.1.0
*/ */
[[nodiscard]] [[nodiscard]]
answer_type get(string endpoint); answer_type get(const string_view &endpoint);
private: private:
Instance &_instance; Instance &_instance;
const string_view _baseuri;
}; };
} // namespace mastodonpp } // namespace mastodonpp

View File

@ -17,6 +17,8 @@
#ifndef MASTODONPP_CURL_WRAPPER_HPP #ifndef MASTODONPP_CURL_WRAPPER_HPP
#define MASTODONPP_CURL_WRAPPER_HPP #define MASTODONPP_CURL_WRAPPER_HPP
#include "return_types.hpp"
#include "curl/curl.h" #include "curl/curl.h"
#include <string> #include <string>
@ -89,7 +91,7 @@ public:
* @since 0.1.0 * @since 0.1.0
*/ */
[[nodiscard]] [[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: private:
CURL *_connection; CURL *_connection;

View File

@ -17,21 +17,18 @@
#include "api.hpp" #include "api.hpp"
#include <map> #include <map>
#include <string_view>
#include <utility>
namespace mastodonpp namespace mastodonpp
{ {
using std::map; using std::map;
using std::string_view; using std::string_view;
using std::move;
API::API(endpoint_type &endpoint) API::API(const endpoint_type &endpoint)
: _endpoint{move(endpoint)} : _endpoint{endpoint}
{} {}
string API::to_string() const string_view API::to_string_view() const
{ {
static const map<endpoint_type,string_view> endpoint_map static const map<endpoint_type,string_view> endpoint_map
{ {

View File

@ -21,20 +21,19 @@ namespace mastodonpp
Connection::Connection(Instance &instance) Connection::Connection(Instance &instance)
: _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; return make_request(
answer.body = API{endpoint}.to_string(); http_method::GET,
return answer; 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; return make_request(http_method::GET, string(_baseuri).append(endpoint));
answer.body = make_request(http_method::GET, "https://ip.tastytea.de/");
return answer;
} }
} // namespace mastodonpp } // namespace mastodonpp

View File

@ -33,7 +33,7 @@ CURLWrapper::~CURLWrapper() noexcept
curl_global_cleanup(); curl_global_cleanup();
} }
string CURLWrapper::make_request(const http_method &meth, answer_type CURLWrapper::make_request(const http_method &meth,
const string_view &uri) const string_view &uri)
{ {
CURLcode code; CURLcode code;
@ -72,7 +72,9 @@ string CURLWrapper::make_request(const http_method &meth,
_curl_buffer_error}; _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, int CURLWrapper::writer(char *data, size_t size, size_t nmemb,