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
#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;

View File

@ -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

View File

@ -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;

View File

@ -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
{

View File

@ -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

View File

@ -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,