From c4a509c42ef87d8a283e04e04042e8e55f877162 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 10 Jan 2018 18:19:19 +0100 Subject: [PATCH] semantic clarifications --- src/api_get.cpp | 46 ++++++++++++++++----------------- src/{http.cpp => http_sync.cpp} | 10 +++---- src/mastodon-cpp.cpp | 7 ++++- src/mastodon-cpp.hpp | 20 +++++++------- 4 files changed, 44 insertions(+), 39 deletions(-) rename src/{http.cpp => http_sync.cpp} (95%) diff --git a/src/api_get.cpp b/src/api_get.cpp index 189443e..9113a3a 100644 --- a/src/api_get.cpp +++ b/src/api_get.cpp @@ -22,74 +22,74 @@ using namespace Mastodon; using std::string; using std::cerr; -const string API::get(const Mastodon::API::v1 &method) +const string API::get(const Mastodon::API::v1 &call) { const std::vector v{}; - return get(method, v); + return get(call, v); } -const string API::get(const Mastodon::API::v1 &method, +const string API::get(const Mastodon::API::v1 &call, const std::vector ¶meters) { - string strmethod = ""; - switch (method) + string strcall = ""; + switch (call) { case v1::accounts_verify_credentials: - strmethod = "/api/v1/accounts/verify_credentials"; + strcall = "/api/v1/accounts/verify_credentials"; break; default: - cerr << "ERROR: Invalid method.\n"; + cerr << "ERROR: Invalid call.\n"; break; } string answer; - _http.request_sync(http::method::GET, strmethod, answer); + _http.request_sync(http::method::GET, strcall, answer); return answer; } -const string API::get(const Mastodon::API::v1 &method, +const string API::get(const Mastodon::API::v1 &call, const string &argument) { const std::vector v; - return get(method, argument, v); + return get(call, argument, v); } -const string API::get(const Mastodon::API::v1 &method, +const string API::get(const Mastodon::API::v1 &call, const string &argument, const std::vector ¶meters) { - string strmethod = ""; - switch (method) + string strcall = ""; + switch (call) { case v1::accounts_id: - strmethod = "/api/v1/accounts/" + argument; + strcall = "/api/v1/accounts/" + argument; break; case v1::accounts_id_followers: - strmethod = "/api/v1/accounts/" + argument + "/followers"; + strcall = "/api/v1/accounts/" + argument + "/followers"; break; case v1::accounts_id_following: - strmethod = "/api/v1/accounts/" + argument + "/following"; + strcall = "/api/v1/accounts/" + argument + "/following"; break; case v1::accounts_id_statuses: - strmethod = "/api/v1/accounts/" + argument + "/statuses"; + strcall = "/api/v1/accounts/" + argument + "/statuses"; break; case v1::accounts_relationships: - strmethod = "/api/v1/accounts/relationships?id=" + argument; + strcall = "/api/v1/accounts/relationships?id=" + argument; break; case v1::accounts_search: - strmethod = "/api/v1/accounts/search?q=" + argument; + strcall = "/api/v1/accounts/search?q=" + argument; break; default: - cerr << "ERROR: Invalid method.\n"; + cerr << "ERROR: Invalid call.\n"; return ""; break; } string answer; - _http.request_sync(http::method::GET, strmethod, answer); + _http.request_sync(http::method::GET, strcall, answer); return answer; } -const string API::get(const std::string &method) +const string API::get(const std::string &call) { - return method; + return call; } diff --git a/src/http.cpp b/src/http_sync.cpp similarity index 95% rename from src/http.cpp rename to src/http_sync.cpp index 6acd0f5..177ddcd 100644 --- a/src/http.cpp +++ b/src/http_sync.cpp @@ -31,11 +31,11 @@ using boost::asio::ip::tcp; namespace ssl = boost::asio::ssl; typedef ssl::stream ssl_socket; -API::http::http(const string &instance, const string &access_token, - const string &useragent) -: _instance(instance) +API::http::http(const API &api, const string &instance, + const string &access_token) +: parent(api) +, _instance(instance) , _access_token(access_token) -, _useragent(useragent) , _ctx(ssl::context::tlsv12) , _resolver(_io_service) , _socket(_io_service, _ctx) @@ -97,7 +97,7 @@ const std::uint16_t API::http::request_sync(const method &meth, request_stream << "Host: " << _instance << "\r\n"; request_stream << "Accept: */*\r\n"; request_stream << "Connection: close\r\n"; - request_stream << "User-Agent: " << _useragent << "\r\n"; + request_stream << "User-Agent: " << parent.get_useragent() << "\r\n"; request_stream << "Authorization: Bearer " << _access_token << "\r\n\r\n"; boost::asio::write(_socket, request); diff --git a/src/mastodon-cpp.cpp b/src/mastodon-cpp.cpp index 3e348fc..aa16347 100644 --- a/src/mastodon-cpp.cpp +++ b/src/mastodon-cpp.cpp @@ -25,7 +25,7 @@ API::API(const string &instance, const string &access_token) : _instance(instance) , _access_token(access_token) , _useragent(string("mastodon-cpp/") + global::version) -, _http(instance, access_token, _useragent) +, _http(*this, instance, access_token) { // } @@ -34,3 +34,8 @@ const void API::set_useragent(const std::string &useragent) { _useragent = useragent; } + +const std::string API::get_useragent() const +{ + return _useragent; +} diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index 96f447b..71d94de 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -42,18 +42,19 @@ namespace Mastodon explicit API(const std::string &instance, const std::string &access_token); // Select one of the predefined methods. - const std::string get(const Mastodon::API::v1 &method); - const std::string get(const Mastodon::API::v1 &method, + const std::string get(const Mastodon::API::v1 &call); + const std::string get(const Mastodon::API::v1 &call, const std::vector ¶meters); - const std::string get(const Mastodon::API::v1 &method, + const std::string get(const Mastodon::API::v1 &call, const std::string &argument, const std::vector ¶meters); - const std::string get(const Mastodon::API::v1 &method, + const std::string get(const Mastodon::API::v1 &call, const std::string &argument); - // Supply a custom method as string. - const std::string get(const std::string &method); + // Supply a custom call as string. + const std::string get(const std::string &call); const void set_useragent(const std::string &useragent); + const std::string get_useragent() const; private: const std::string _instance; @@ -71,9 +72,8 @@ namespace Mastodon DELETE }; - explicit http(const std::string &instance, - const std::string &access_token, - const std::string &useragent); + explicit http(const API &api, const std::string &instance, + const std::string &access_token); const std::uint16_t request_sync(const method &meth, const std::string &path, std::string &answer); @@ -83,9 +83,9 @@ namespace Mastodon std::string &answer); private: + const API &parent; const std::string _instance; const std::string _access_token; - const std::string _useragent; boost::asio::ssl::context _ctx; boost::asio::io_service _io_service; boost::asio::ip::tcp::resolver _resolver;