diff --git a/src/api/get.cpp b/src/api/get.cpp index bde7560..aff11da 100644 --- a/src/api/get.cpp +++ b/src/api/get.cpp @@ -19,141 +19,207 @@ #include "mastodon-cpp.hpp" using namespace Mastodon; -using std::cerr; -uint16_t API::get(const Mastodon::API::v1 &call, - const parametermap ¶meters, string &answer) +const return_call API::get(const Mastodon::API::v1 &call, + const parametermap ¶meters) { string strcall = ""; string strid = ""; // The ID is part of the path - const auto &it = parameters.find("id"); - if (it != parameters.end()) + const auto &it_id = parameters.find("id"); + if (it_id != parameters.end()) { - strid = it->second[0]; + strid = it_id->second[0]; } switch (call) { case v1::accounts_verify_credentials: + { strcall = "/api/v1/accounts/verify_credentials"; break; + } case v1::blocks: + { strcall = "/api/v1/blocks"; break; + } case v1::domain_blocks: + { strcall = "/api/v1/domain_blocks"; break; + } case v1::favourites: + { strcall = "/api/v1/favourites"; break; + } case v1::follow_requests: + { strcall = "/api/v1/follow_requests"; break; + } case v1::instance: + { strcall = "/api/v1/instance"; break; + } case v1::custom_emojis: + { strcall = "/api/v1/custom_emojis"; break; + } case v1::lists: + { strcall = "/api/v1/lists"; break; + } case v1::mutes: + { strcall = "/api/v1/mutes"; break; + } case v1::notifications: + { strcall = "/api/v1/notifications"; break; + } case v1::reports: + { strcall = "/api/v1/reports"; break; + } case v1::timelines_home: + { strcall = "/api/v1/timelines/home"; break; + } case v1::timelines_public: + { strcall = "/api/v1/timelines/public"; break; + } case v1::accounts_relationships: + { strcall = "/api/v1/accounts/relationships"; break; + } case v1::accounts_id: + { strcall = "/api/v1/accounts/" + strid; break; + } case v1::accounts_id_followers: + { strcall = "/api/v1/accounts/" + strid + "/followers"; break; + } case v1::accounts_id_following: + { strcall = "/api/v1/accounts/" + strid + "/following"; break; + } case v1::accounts_id_statuses: + { strcall = "/api/v1/accounts/" + strid + "/statuses"; break; + } case v1::accounts_search: + { strcall = "/api/v1/accounts/search"; break; + } case v1::accounts_id_lists: + { strcall = "/api/v1/accounts/" + strid + "/lists"; break; + } case v1::lists_id_accounts: + { strcall = "/api/v1/lists/" + strid + "/accounts"; break; + } case v1::lists_id: + { strcall = "/api/v1/lists/" + strid; break; + } case v1::notifications_id: + { strcall = "/api/v1/notifications/" + strid; break; + } case v1::search: + { strcall = "/api/v1/search"; break; + } case v1::statuses_id: + { strcall = "/api/v1/statuses/" + strid; break; + } case v1::statuses_id_context: + { strcall = "/api/v1/statuses/" + strid + "/context"; break; + } case v1::statuses_id_card: + { strcall = "/api/v1/statuses/" + strid + "/card"; break; + } case v1::statuses_id_reblogged_by: + { strcall = "/api/v1/statuses/" + strid + "/reblogged_by"; break; + } case v1::statuses_id_favourited_by: + { strcall = "/api/v1/statuses/" + strid + "/favourited_by"; break; + } case v1::timelines_tag_hashtag: + { + // The tag is part of the path + const auto &it = parameters.find("tag"); + if (it != parameters.end()) { - // The hashtag is part of the path - const auto &it = parameters.find("hashtag"); - if (it != parameters.end()) - { - strcall = "/api/v1/timelines/tag/" + urlencode(it->second[0]); - } - else - { - ttdebug << "ERROR: Invalid call.\n"; - return 11; - } + strcall = "/api/v1/timelines/tag/" + urlencode(it->second[0]); + } + else + { + ttdebug << "ERROR: Invalid argument.\n"; + return { 22, "Invalid argument", 0, "" }; } break; + } case v1::timelines_list_list_id: + { strcall = "/api/v1/timelines/list/" + strid; break; + } case v1::push_subscription: + { strcall = "/api/v1/push/subscription"; break; + } case v1::endorsements: + { strcall = "/api/v1/endorsements"; break; + } case v1::bookmarks: + { strcall = "/api/v1/bookmarks"; break; + } default: - ttdebug << "ERROR: Invalid call.\n"; - return 11; - break; + { + ttdebug << "ERROR: Invalid argument.\n"; + return { 22, "Invalid argument", 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 parametermap newparameters = parameters; newparameters.erase("id"); - newparameters.erase("hashtag"); + newparameters.erase("tag"); strcall += maptostr(newparameters); } - return get(strcall, answer); + return get(strcall); } -uint16_t API::get(const Mastodon::API::v2 &call, - const parametermap ¶meters, string &answer) +const return_call API::get(const Mastodon::API::v2 &call, + const parametermap ¶meters) { string strcall = ""; string strid = ""; @@ -184,12 +250,15 @@ uint16_t API::get(const Mastodon::API::v2 &call, switch (call) { case v2::search: + { strcall = "/api/v2/search"; break; + } default: - ttdebug << "ERROR: Invalid call.\n"; - return 11; - break; + { + ttdebug << "ERROR: Invalid argument.\n"; + return { 22, "Invalid argument", 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 parametermap newparameters = parameters; newparameters.erase("id"); - newparameters.erase("hashtag"); + newparameters.erase("tag"); 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, p, answer); + return get(call, {}); } -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); } diff --git a/src/mastodon-cpp.cpp b/src/mastodon-cpp.cpp index 0e222be..8e0dcfe 100644 --- a/src/mastodon-cpp.cpp +++ b/src/mastodon-cpp.cpp @@ -27,6 +27,43 @@ 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) : _instance(instance) , _access_token(access_token) diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index 8f4820c..915692d 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -24,33 +24,48 @@ #include #include #include +#include #include #include +using std::uint8_t; using std::uint16_t; using std::string; /*! - * @example example01_dump_json.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 + * @example example01_CHANGEME.cpp */ 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. * @@ -472,7 +487,7 @@ public: * * @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. @@ -485,18 +500,16 @@ public: * @return @ref error "Error code". If the URL has permanently changed, 13 * is returned and answer is set to the new URL. */ - uint16_t get(const Mastodon::API::v1 &call, - const parametermap ¶meters, - string &answer); + const return_call get(const Mastodon::API::v1 &call, + const parametermap ¶meters); /*! * @brief Make a GET request which requires parameters. * * @since 0.16.0 */ - uint16_t get(const Mastodon::API::v2 &call, - const parametermap ¶meters, - string &answer); + const return_call get(const Mastodon::API::v2 &call, + const parametermap ¶meters); /*! * @brief Make a custom GET request. @@ -510,7 +523,7 @@ public: * * @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.