Added new return types, replaced return types for API::get().

This commit is contained in:
tastytea 2019-02-22 11:33:36 +01:00
parent 5c4ca94d4b
commit 514ae8dc63
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 176 additions and 58 deletions

View File

@ -19,141 +19,207 @@
#include "mastodon-cpp.hpp" #include "mastodon-cpp.hpp"
using namespace Mastodon; using namespace Mastodon;
using std::cerr;
uint16_t API::get(const Mastodon::API::v1 &call, const return_call API::get(const Mastodon::API::v1 &call,
const parametermap &parameters, string &answer) const parametermap &parameters)
{ {
string strcall = ""; string strcall = "";
string strid = ""; string strid = "";
// The ID is part of the path // The ID is part of the path
const auto &it = parameters.find("id"); const auto &it_id = parameters.find("id");
if (it != parameters.end()) if (it_id != parameters.end())
{ {
strid = it->second[0]; strid = it_id->second[0];
} }
switch (call) switch (call)
{ {
case v1::accounts_verify_credentials: case v1::accounts_verify_credentials:
{
strcall = "/api/v1/accounts/verify_credentials"; strcall = "/api/v1/accounts/verify_credentials";
break; break;
}
case v1::blocks: case v1::blocks:
{
strcall = "/api/v1/blocks"; strcall = "/api/v1/blocks";
break; break;
}
case v1::domain_blocks: case v1::domain_blocks:
{
strcall = "/api/v1/domain_blocks"; strcall = "/api/v1/domain_blocks";
break; break;
}
case v1::favourites: case v1::favourites:
{
strcall = "/api/v1/favourites"; strcall = "/api/v1/favourites";
break; break;
}
case v1::follow_requests: case v1::follow_requests:
{
strcall = "/api/v1/follow_requests"; strcall = "/api/v1/follow_requests";
break; break;
}
case v1::instance: case v1::instance:
{
strcall = "/api/v1/instance"; strcall = "/api/v1/instance";
break; break;
}
case v1::custom_emojis: case v1::custom_emojis:
{
strcall = "/api/v1/custom_emojis"; strcall = "/api/v1/custom_emojis";
break; break;
}
case v1::lists: case v1::lists:
{
strcall = "/api/v1/lists"; strcall = "/api/v1/lists";
break; break;
}
case v1::mutes: case v1::mutes:
{
strcall = "/api/v1/mutes"; strcall = "/api/v1/mutes";
break; break;
}
case v1::notifications: case v1::notifications:
{
strcall = "/api/v1/notifications"; strcall = "/api/v1/notifications";
break; break;
}
case v1::reports: case v1::reports:
{
strcall = "/api/v1/reports"; strcall = "/api/v1/reports";
break; break;
}
case v1::timelines_home: case v1::timelines_home:
{
strcall = "/api/v1/timelines/home"; strcall = "/api/v1/timelines/home";
break; break;
}
case v1::timelines_public: case v1::timelines_public:
{
strcall = "/api/v1/timelines/public"; strcall = "/api/v1/timelines/public";
break; break;
}
case v1::accounts_relationships: case v1::accounts_relationships:
{
strcall = "/api/v1/accounts/relationships"; strcall = "/api/v1/accounts/relationships";
break; break;
}
case v1::accounts_id: case v1::accounts_id:
{
strcall = "/api/v1/accounts/" + strid; strcall = "/api/v1/accounts/" + strid;
break; break;
}
case v1::accounts_id_followers: case v1::accounts_id_followers:
{
strcall = "/api/v1/accounts/" + strid + "/followers"; strcall = "/api/v1/accounts/" + strid + "/followers";
break; break;
}
case v1::accounts_id_following: case v1::accounts_id_following:
{
strcall = "/api/v1/accounts/" + strid + "/following"; strcall = "/api/v1/accounts/" + strid + "/following";
break; break;
}
case v1::accounts_id_statuses: case v1::accounts_id_statuses:
{
strcall = "/api/v1/accounts/" + strid + "/statuses"; strcall = "/api/v1/accounts/" + strid + "/statuses";
break; break;
}
case v1::accounts_search: case v1::accounts_search:
{
strcall = "/api/v1/accounts/search"; strcall = "/api/v1/accounts/search";
break; break;
}
case v1::accounts_id_lists: case v1::accounts_id_lists:
{
strcall = "/api/v1/accounts/" + strid + "/lists"; strcall = "/api/v1/accounts/" + strid + "/lists";
break; break;
}
case v1::lists_id_accounts: case v1::lists_id_accounts:
{
strcall = "/api/v1/lists/" + strid + "/accounts"; strcall = "/api/v1/lists/" + strid + "/accounts";
break; break;
}
case v1::lists_id: case v1::lists_id:
{
strcall = "/api/v1/lists/" + strid; strcall = "/api/v1/lists/" + strid;
break; break;
}
case v1::notifications_id: case v1::notifications_id:
{
strcall = "/api/v1/notifications/" + strid; strcall = "/api/v1/notifications/" + strid;
break; break;
}
case v1::search: case v1::search:
{
strcall = "/api/v1/search"; strcall = "/api/v1/search";
break; break;
}
case v1::statuses_id: case v1::statuses_id:
{
strcall = "/api/v1/statuses/" + strid; strcall = "/api/v1/statuses/" + strid;
break; break;
}
case v1::statuses_id_context: case v1::statuses_id_context:
{
strcall = "/api/v1/statuses/" + strid + "/context"; strcall = "/api/v1/statuses/" + strid + "/context";
break; break;
}
case v1::statuses_id_card: case v1::statuses_id_card:
{
strcall = "/api/v1/statuses/" + strid + "/card"; strcall = "/api/v1/statuses/" + strid + "/card";
break; break;
}
case v1::statuses_id_reblogged_by: case v1::statuses_id_reblogged_by:
{
strcall = "/api/v1/statuses/" + strid + "/reblogged_by"; strcall = "/api/v1/statuses/" + strid + "/reblogged_by";
break; break;
}
case v1::statuses_id_favourited_by: case v1::statuses_id_favourited_by:
{
strcall = "/api/v1/statuses/" + strid + "/favourited_by"; strcall = "/api/v1/statuses/" + strid + "/favourited_by";
break; break;
}
case v1::timelines_tag_hashtag: case v1::timelines_tag_hashtag:
{ {
// The hashtag is part of the path // The tag is part of the path
const auto &it = parameters.find("hashtag"); const auto &it = parameters.find("tag");
if (it != parameters.end()) if (it != parameters.end())
{ {
strcall = "/api/v1/timelines/tag/" + urlencode(it->second[0]); strcall = "/api/v1/timelines/tag/" + urlencode(it->second[0]);
} }
else else
{ {
ttdebug << "ERROR: Invalid call.\n"; ttdebug << "ERROR: Invalid argument.\n";
return 11; return { 22, "Invalid argument", 0, "" };
}
} }
break; break;
}
case v1::timelines_list_list_id: case v1::timelines_list_list_id:
{
strcall = "/api/v1/timelines/list/" + strid; strcall = "/api/v1/timelines/list/" + strid;
break; break;
}
case v1::push_subscription: case v1::push_subscription:
{
strcall = "/api/v1/push/subscription"; strcall = "/api/v1/push/subscription";
break; break;
}
case v1::endorsements: case v1::endorsements:
{
strcall = "/api/v1/endorsements"; strcall = "/api/v1/endorsements";
break; break;
}
case v1::bookmarks: case v1::bookmarks:
{
strcall = "/api/v1/bookmarks"; strcall = "/api/v1/bookmarks";
break; break;
}
default: default:
ttdebug << "ERROR: Invalid call.\n"; {
return 11; ttdebug << "ERROR: Invalid argument.\n";
break; return { 22, "Invalid argument", 0, "" };
}
} }
if (parameters.size() > 0) if (parameters.size() > 0)
@ -161,15 +227,15 @@ uint16_t API::get(const Mastodon::API::v1 &call,
// Delete the parameters that are already in strcall // Delete the parameters that are already in strcall
parametermap newparameters = parameters; parametermap newparameters = parameters;
newparameters.erase("id"); newparameters.erase("id");
newparameters.erase("hashtag"); newparameters.erase("tag");
strcall += maptostr(newparameters); strcall += maptostr(newparameters);
} }
return get(strcall, answer); return get(strcall);
} }
uint16_t API::get(const Mastodon::API::v2 &call, const return_call API::get(const Mastodon::API::v2 &call,
const parametermap &parameters, string &answer) const parametermap &parameters)
{ {
string strcall = ""; string strcall = "";
string strid = ""; string strid = "";
@ -184,12 +250,15 @@ uint16_t API::get(const Mastodon::API::v2 &call,
switch (call) switch (call)
{ {
case v2::search: case v2::search:
{
strcall = "/api/v2/search"; strcall = "/api/v2/search";
break; break;
}
default: default:
ttdebug << "ERROR: Invalid call.\n"; {
return 11; ttdebug << "ERROR: Invalid argument.\n";
break; return { 22, "Invalid argument", 0, "" };
}
} }
if (parameters.size() > 0) if (parameters.size() > 0)
@ -197,20 +266,19 @@ uint16_t API::get(const Mastodon::API::v2 &call,
// Delete the parameters that are already in strcall // Delete the parameters that are already in strcall
parametermap newparameters = parameters; parametermap newparameters = parameters;
newparameters.erase("id"); newparameters.erase("id");
newparameters.erase("hashtag"); newparameters.erase("tag");
strcall += maptostr(newparameters); strcall += maptostr(newparameters);
} }
return get(strcall, answer); return get(strcall);
} }
uint16_t API::get(const Mastodon::API::v1 &call, string &answer) const return_call API::get(const Mastodon::API::v1 &call)
{ {
const parametermap p; return get(call, {});
return get(call, p, answer);
} }
uint16_t API::get(const std::string &call, string &answer) const return_call API::get(const std::string &call)
{ {
return _http.request(http::method::GET, call, answer); return _http.request(http::method::GET, call);
} }

View File

@ -27,6 +27,43 @@
using namespace Mastodon; using namespace Mastodon;
constexpr return_base::operator const bool() const
{
if (error_code == 0)
{
return true;
}
else
{
return false;
}
}
constexpr return_base::operator const uint8_t() const
{
return error_code;
}
const return_call::operator const string() const
{
return answer;
}
std::ostream &operator <<(std::ostream &out, const return_call &ret)
{
out << ret.answer;
return out;
}
return_call::return_call(const uint8_t ec, const string &em,
const uint16_t hec, const string &a)
: http_error_code(hec)
, answer(a)
{
error_code = ec;
error_message = em;
}
API::API(const string &instance, const string &access_token) API::API(const string &instance, const string &access_token)
: _instance(instance) : _instance(instance)
, _access_token(access_token) , _access_token(access_token)

View File

@ -24,33 +24,48 @@
#include <memory> #include <memory>
#include <array> #include <array>
#include <mutex> #include <mutex>
#include <ostream>
#include <curlpp/cURLpp.hpp> #include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp> #include <curlpp/Easy.hpp>
using std::uint8_t;
using std::uint16_t; using std::uint16_t;
using std::string; using std::string;
/*! /*!
* @example example01_dump_json.cpp * @example example01_CHANGEME.cpp
* @example example02_parse_account.cpp
* @example example03_mastocron.cpp
* @example example04_update_credentials.cpp
* @example example05_follow_unfollow.cpp
* @example example06_toot_delete-toot.cpp
* @example example07_register_app.cpp
* @example example08_rate_limiting.cpp
* @example example09_streaming_api.cpp
* @example example10_simplify.cpp
* @example example11_post_media.cpp
* @example example12_easy_laststatus.cpp
* @example example13_easy_stream.cpp
* @example example14_easy_treeview.cpp
* @example example15_proxy.cpp
* @example example16_account_fields.cpp
*/ */
namespace Mastodon namespace Mastodon
{ {
/*!
* Base return type.
*/
typedef struct return_base
{
uint8_t error_code = 0; // NOTE: http://mazack.org/unix/errno.php
string error_message;
constexpr operator const bool() const;
constexpr operator const uint8_t() const;
} return_base;
/*!
* Return type for API calls.
*/
typedef struct return_call : return_base
{
uint16_t http_error_code = 0;
string answer;
const operator const string() const;
friend std::ostream &operator <<(std::ostream &out,
const return_call &ret);
return_call(const uint8_t ec, const string &em,
const uint16_t hec, const string &a);
} return_call;
/*! /*!
* @brief Class for the Mastodon API. * @brief Class for the Mastodon API.
* *
@ -472,7 +487,7 @@ public:
* *
* @since before 0.11.0 * @since before 0.11.0
*/ */
uint16_t get(const Mastodon::API::v1 &call, string &answer); const return_call get(const Mastodon::API::v1 &call);
/*! /*!
* @brief Make a GET request which requires parameters. * @brief Make a GET request which requires parameters.
@ -485,18 +500,16 @@ public:
* @return @ref error "Error code". If the URL has permanently changed, 13 * @return @ref error "Error code". If the URL has permanently changed, 13
* is returned and answer is set to the new URL. * is returned and answer is set to the new URL.
*/ */
uint16_t get(const Mastodon::API::v1 &call, const return_call get(const Mastodon::API::v1 &call,
const parametermap &parameters, const parametermap &parameters);
string &answer);
/*! /*!
* @brief Make a GET request which requires parameters. * @brief Make a GET request which requires parameters.
* *
* @since 0.16.0 * @since 0.16.0
*/ */
uint16_t get(const Mastodon::API::v2 &call, const return_call get(const Mastodon::API::v2 &call,
const parametermap &parameters, const parametermap &parameters);
string &answer);
/*! /*!
* @brief Make a custom GET request. * @brief Make a custom GET request.
@ -510,7 +523,7 @@ public:
* *
* @since before 0.11.0 * @since before 0.11.0
*/ */
uint16_t get(const string &call, string &answer); const return_call get(const string &call);
/*! /*!
* @brief Make a streaming GET request. * @brief Make a streaming GET request.