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 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<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)
{
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<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 std::vector<string> &parameters)
{
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;
}

View File

@ -31,11 +31,11 @@ using boost::asio::ip::tcp;
namespace ssl = boost::asio::ssl;
typedef ssl::stream<tcp::socket> 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);

View File

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

View File

@ -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<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::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);
// 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;