semantic clarifications

This commit is contained in:
tastytea 2018-01-10 18:19:19 +01:00
parent be6fca69fe
commit c4a509c42e
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
4 changed files with 44 additions and 39 deletions

View File

@ -22,74 +22,74 @@
using namespace Mastodon; using namespace Mastodon;
using std::string; using std::string;
using std::cerr; using std::cerr;
const string API::get(const Mastodon::API::v1 &method) const string API::get(const Mastodon::API::v1 &call)
{ {
const std::vector<string> v{}; const std::vector<string> 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<string> &parameters) const std::vector<string> &parameters)
{ {
string strmethod = ""; string strcall = "";
switch (method) switch (call)
{ {
case v1::accounts_verify_credentials: case v1::accounts_verify_credentials:
strmethod = "/api/v1/accounts/verify_credentials"; strcall = "/api/v1/accounts/verify_credentials";
break; break;
default: default:
cerr << "ERROR: Invalid method.\n"; cerr << "ERROR: Invalid call.\n";
break; break;
} }
string answer; string answer;
_http.request_sync(http::method::GET, strmethod, answer); _http.request_sync(http::method::GET, strcall, answer);
return 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 string &argument)
{ {
const std::vector<string> v; const std::vector<string> 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 string &argument,
const std::vector<string> &parameters) const std::vector<string> &parameters)
{ {
string strmethod = ""; string strcall = "";
switch (method) switch (call)
{ {
case v1::accounts_id: case v1::accounts_id:
strmethod = "/api/v1/accounts/" + argument; strcall = "/api/v1/accounts/" + argument;
break; break;
case v1::accounts_id_followers: case v1::accounts_id_followers:
strmethod = "/api/v1/accounts/" + argument + "/followers"; strcall = "/api/v1/accounts/" + argument + "/followers";
break; break;
case v1::accounts_id_following: case v1::accounts_id_following:
strmethod = "/api/v1/accounts/" + argument + "/following"; strcall = "/api/v1/accounts/" + argument + "/following";
break; break;
case v1::accounts_id_statuses: case v1::accounts_id_statuses:
strmethod = "/api/v1/accounts/" + argument + "/statuses"; strcall = "/api/v1/accounts/" + argument + "/statuses";
break; break;
case v1::accounts_relationships: case v1::accounts_relationships:
strmethod = "/api/v1/accounts/relationships?id=" + argument; strcall = "/api/v1/accounts/relationships?id=" + argument;
break; break;
case v1::accounts_search: case v1::accounts_search:
strmethod = "/api/v1/accounts/search?q=" + argument; strcall = "/api/v1/accounts/search?q=" + argument;
break; break;
default: default:
cerr << "ERROR: Invalid method.\n"; cerr << "ERROR: Invalid call.\n";
return ""; return "";
break; break;
} }
string answer; string answer;
_http.request_sync(http::method::GET, strmethod, answer); _http.request_sync(http::method::GET, strcall, answer);
return answer; return answer;
} }
const string API::get(const std::string &method) const string API::get(const std::string &call)
{ {
return method; return call;
} }

View File

@ -31,11 +31,11 @@ using boost::asio::ip::tcp;
namespace ssl = boost::asio::ssl; namespace ssl = boost::asio::ssl;
typedef ssl::stream<tcp::socket> ssl_socket; typedef ssl::stream<tcp::socket> ssl_socket;
API::http::http(const string &instance, const string &access_token, API::http::http(const API &api, const string &instance,
const string &useragent) const string &access_token)
: _instance(instance) : parent(api)
, _instance(instance)
, _access_token(access_token) , _access_token(access_token)
, _useragent(useragent)
, _ctx(ssl::context::tlsv12) , _ctx(ssl::context::tlsv12)
, _resolver(_io_service) , _resolver(_io_service)
, _socket(_io_service, _ctx) , _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 << "Host: " << _instance << "\r\n";
request_stream << "Accept: */*\r\n"; request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\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 " request_stream << "Authorization: Bearer "
<< _access_token << "\r\n\r\n"; << _access_token << "\r\n\r\n";
boost::asio::write(_socket, request); boost::asio::write(_socket, request);

View File

@ -25,7 +25,7 @@ API::API(const string &instance, const string &access_token)
: _instance(instance) : _instance(instance)
, _access_token(access_token) , _access_token(access_token)
, _useragent(string("mastodon-cpp/") + global::version) , _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; _useragent = useragent;
} }
const std::string API::get_useragent() const
{
return _useragent;
}

View File

@ -42,18 +42,19 @@ namespace Mastodon
explicit API(const std::string &instance, explicit API(const std::string &instance,
const std::string &access_token); const std::string &access_token);
// Select one of the predefined methods. // Select one of the predefined methods.
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 &method, const std::string get(const Mastodon::API::v1 &call,
const std::vector<std::string> &parameters); const std::vector<std::string> &parameters);
const std::string get(const Mastodon::API::v1 &method, const std::string get(const Mastodon::API::v1 &call,
const std::string &argument, const std::string &argument,
const std::vector<std::string> &parameters); const std::vector<std::string> &parameters);
const std::string get(const Mastodon::API::v1 &method, const std::string get(const Mastodon::API::v1 &call,
const std::string &argument); const std::string &argument);
// Supply a custom method as string. // Supply a custom call as string.
const std::string get(const std::string &method); const std::string get(const std::string &call);
const void set_useragent(const std::string &useragent); const void set_useragent(const std::string &useragent);
const std::string get_useragent() const;
private: private:
const std::string _instance; const std::string _instance;
@ -71,9 +72,8 @@ namespace Mastodon
DELETE DELETE
}; };
explicit http(const std::string &instance, explicit http(const API &api, const std::string &instance,
const std::string &access_token, const std::string &access_token);
const std::string &useragent);
const std::uint16_t request_sync(const method &meth, const std::uint16_t request_sync(const method &meth,
const std::string &path, const std::string &path,
std::string &answer); std::string &answer);
@ -83,9 +83,9 @@ namespace Mastodon
std::string &answer); std::string &answer);
private: private:
const API &parent;
const std::string _instance; const std::string _instance;
const std::string _access_token; const std::string _access_token;
const std::string _useragent;
boost::asio::ssl::context _ctx; boost::asio::ssl::context _ctx;
boost::asio::io_service _io_service; boost::asio::io_service _io_service;
boost::asio::ip::tcp::resolver _resolver; boost::asio::ip::tcp::resolver _resolver;