diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index e6c3b2c..067f063 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -28,13 +28,13 @@ #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" #include "easy/return_types_easy.hpp" - #include "easy/types.hpp" + #include "easy/types_easy.hpp" #include "easy/entities/notification.hpp" #include "easy/entities/status.hpp" #else #include #include - #include + #include #include #include #endif diff --git a/src/easy/entity.hpp b/src/easy/entity.hpp index 5cfd8f0..4d6eb13 100644 --- a/src/easy/entity.hpp +++ b/src/easy/entity.hpp @@ -14,17 +14,17 @@ * along with this program. If not, see . */ -#ifndef ENTITY_HPP -#define ENTITY_HPP +#ifndef MASTODON_CPP_EASY_ENTITY_HPP +#define MASTODON_CPP_EASY_ENTITY_HPP #include #include // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP - #include "easy/types.hpp" + #include "easy/types_easy.hpp" #else - #include + #include #endif using std::string; @@ -240,4 +240,4 @@ namespace Easy } } -#endif // ENTITY_HPP +#endif // MASTODON_CPP_EASY_ENTITY_HPP diff --git a/src/easy/simple_calls.cpp b/src/easy/simple_calls.cpp index 646a6fd..4532e8c 100644 --- a/src/easy/simple_calls.cpp +++ b/src/easy/simple_calls.cpp @@ -30,7 +30,7 @@ const return_entity API::send_toot(const Status &status) const return_entity API::send_post(const Status &status) { - API::parametermap parameters; + parametermap parameters; if (!status.content().empty()) { @@ -86,7 +86,7 @@ const return_entity API::send_post(const Status &status) std::vector media_ids; for (const Attachment &att : status.media_attachments()) { - API::parametermap param_att; + parametermap param_att; if (!att.file().empty()) { param_att.insert({ "file", { att.file() }}); @@ -132,7 +132,7 @@ const return_entity API::send_post(const Status &status) const return_entity_vector API::get_notifications( const uint16_t limit, const string since_id, const string max_id) { - API::parametermap parameters; + parametermap parameters; parameters.insert({ "limit", { std::to_string(limit) } }); if (!since_id.empty()) diff --git a/src/easy/types.cpp b/src/easy/types_easy.cpp similarity index 98% rename from src/easy/types.cpp rename to src/easy/types_easy.cpp index 8ced2c1..acad7d9 100644 --- a/src/easy/types.cpp +++ b/src/easy/types_easy.cpp @@ -14,7 +14,7 @@ * along with this program. If not, see . */ -#include "types.hpp" +#include "types_easy.hpp" using namespace Mastodon; diff --git a/src/easy/types.hpp b/src/easy/types_easy.hpp similarity index 96% rename from src/easy/types.hpp rename to src/easy/types_easy.hpp index 5605a9f..92de9c2 100644 --- a/src/easy/types.hpp +++ b/src/easy/types_easy.hpp @@ -14,8 +14,8 @@ * along with this program. If not, see . */ -#ifndef MASTODON_CPP_TYPES_HPP -#define MASTODON_CPP_TYPES_HPP +#ifndef MASTODON_CPP_EASY_TYPES_EASY_HPP +#define MASTODON_CPP_EASY_TYPES_EASY_HPP #include #include @@ -152,4 +152,4 @@ namespace Easy }; } } -#endif // MASTODON_CPP_TYPES_HPP +#endif // MASTODON_CPP_EASY_TYPES_EASY_HPP diff --git a/src/mastodon-cpp.cpp b/src/mastodon-cpp.cpp index 334a195..11e1262 100644 --- a/src/mastodon-cpp.cpp +++ b/src/mastodon-cpp.cpp @@ -159,7 +159,7 @@ return_call API::register_app1(const string &client_name, string &client_secret, string &url) { - API::parametermap parameters = + parametermap parameters = { { "client_name", { client_name } }, { "redirect_uris", { redirect_uri } }, @@ -207,7 +207,7 @@ return_call API::register_app2(const string &client_id, const string &code, string &access_token) { - API::parametermap parameters = + parametermap parameters = { { "client_id", { client_id } }, { "client_secret", { client_secret } }, diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index e5d7b8f..3c63921 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -19,7 +19,6 @@ #include #include -#include #include #include #include @@ -30,8 +29,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "return_types.hpp" + #include "types.hpp" #else #include + #include #endif using std::string; @@ -42,670 +43,665 @@ using std::string; namespace Mastodon { -/*! - * @brief Interface to the Mastodon API. - * - * All input is expected to be UTF-8. Binary data must be - * base64-encoded or a filename. - * It appears that media attachements can not be base64 encoded. - * - * @section error Error codes - * | Code | Explanation | - * | --------: |:-------------------------------------------| - * | 0 | No error | - * | 22 | Invalid argument | - * | 78 | URL changed (HTTP 301 or 308) | - * | 110 | Connection timed out | - * | 111 | Connection refused (check http_error_code) | - * | 192 | curlpp runtime error | - * | 193 | curlpp logic error | - * | 255 | Unknown error | - * - * @since before 0.11.0 - */ -class API -{ -public: /*! - * @brief http class. Do not use this directly. + * @brief Interface to the Mastodon API. + * + * All input is expected to be UTF-8. Binary data must be + * base64-encoded or a filename. + * It appears that media attachements can not be base64 encoded. + * + * @section error Error codes + * | Code | Explanation | + * | --------: |:-------------------------------------------| + * | 0 | No error | + * | 22 | Invalid argument | + * | 78 | URL changed (HTTP 301 or 308) | + * | 110 | Connection timed out | + * | 111 | Connection refused (check http_error_code) | + * | 192 | curlpp runtime error | + * | 193 | curlpp logic error | + * | 255 | Unknown error | * * @since before 0.11.0 */ - class http + class API { public: /*! - * @brief HTTP methods - */ - enum class method - { - GET, - PATCH, - POST, - PUT, - DELETE, - GET_STREAM - }; - - explicit http(const API &api, const string &instance, - const string &access_token); - ~http(); - return_call request(const method &meth, const string &path); - - /*! - * @brief HTTP Request. - * - * @param meth The method defined in http::method - * @param path The api call as string - * @param formdata The form data for PATCH and POST requests. - * @param answer The answer from the server - * - * @return @ref error "Error code". If the URL has permanently changed, - * 13 is returned and answer is set to the new URL. + * @brief http class. Do not use this directly. * * @since before 0.11.0 */ - return_call request(const method &meth, - const string &path, - const curlpp::Forms &formdata); + class http + { + public: + /*! + * @brief HTTP methods + */ + enum class method + { + GET, + PATCH, + POST, + PUT, + DELETE, + GET_STREAM + }; + + explicit http(const API &api, const string &instance, + const string &access_token); + ~http(); + return_call request(const method &meth, const string &path); + + /*! + * @brief HTTP Request. + * + * @param meth The method defined in http::method + * @param path The api call as string + * @param formdata The form data for PATCH and POST requests. + * @param answer The answer from the server + * + * @return @ref error "Error code". If the URL has permanently + * changed, 13 is returned and answer is set to the new URL. + * + * @since before 0.11.0 + */ + return_call request(const method &meth, + const string &path, + const curlpp::Forms &formdata); + + /*! + * @brief Get all headers in a string + */ + void get_headers(string &headers) const; + + /*! + * @brief Cancels the stream. Use only with streams. + * + * Cancels the stream next time data comes in. Can take a + * few seconds. This works only with streams, because only + * streams have an own http object. + * + * @since 0.12.2 + */ + void cancel_stream(); + + /*! + * @brief Gets the mutex guarding the string that is written to. + * + * The mutex guards the function that writes to the string + * you specified in get_stream(). + * + * @return A reference of the mutex. + * + * @since 0.12.3 + */ + std::mutex &get_mutex(); + + private: + const API &parent; + const string _instance; + const string _access_token; + string _headers; + bool _cancel_stream; + std::mutex _mutex; + + size_t callback_write(char* data, size_t size, size_t nmemb, + string *oss); + double callback_progress(double /* dltotal */, double /* dlnow */, + double /* ultotal */, double /* ulnow */); + }; /*! - * @brief Get all headers in a string + * @brief A list of all v1 API calls. + * + * The original `/` are substituted by `_`. + * + * @since before 0.11.0 */ - void get_headers(string &headers) const; + enum class v1 + { + // Mastodon + accounts_id, + // accounts, + accounts_verify_credentials, + accounts_update_credentials, + accounts_id_followers, + accounts_id_following, + accounts_id_statuses, + accounts_id_follow, + accounts_id_unfollow, + accounts_relationships, + accounts_search, + + apps, + // apps_verify_credentials, + + blocks, + accounts_id_block, + accounts_id_unblock, + + custom_emojis, + + domain_blocks, + + endorsements, + accounts_id_pin, + accounts_id_unpin, + + favourites, + statuses_id_favourite, + statuses_id_unfavourite, + + // filters, + // filters_id, + + follow_requests, + follow_requests_id_authorize, + follow_requests_id_reject, + + // suggestions, + // suggestions_id, + + instance, + + lists, + accounts_id_lists, + lists_id, + lists_id_accounts, + + media, + media_id, + + mutes, + accounts_id_mute, + accounts_id_unmute, + statuses_id_mute, + statuses_id_unmute, + + notifications, + notifications_id, + notifications_clear, + notifications_dismiss, + push_subscription, + + reports, + + // scheduled_statuses, + // scheduled_statuses_id, + + search, + + statuses, + statuses_id, + statuses_id_context, + statuses_id_card, + statuses_id_reblogged_by, + statuses_id_favourited_by, + statuses_id_reblog, + statuses_id_unreblog, + statuses_id_pin, + statuses_id_unpin, + + timelines_home, + // timelines_conversations, + timelines_public, + timelines_tag_hashtag, + timelines_list_list_id, + + streaming_user, + streaming_public, + streaming_public_local, + streaming_hashtag, + // streaming_hashtag_local, + streaming_list, + // streaming_direct, + + // Glitch-Soc + bookmarks, + statuses_id_bookmark, + statuses_id_unbookmark + }; /*! - * @brief Cancels the stream. Use only with streams. + * @brief A list of all v2 API calls. * - * Cancels the stream next time data comes in. Can take a few - * seconds. - * This works only with streams, because only streams have an - * own http object. + * The original `/` are substituted by `_`. * - * @since 0.12.2 + * @since 0.16.0 */ - void cancel_stream(); + enum class v2 + { + search + }; /*! - * @brief Gets the mutex guarding the string that is written to. + * @brief Constructs a new API object. * - * The mutex guards the function that writes to the string you - * specified in get_stream(). + * To register your application, leave access_token blank and + * call register_app1() and register_app2(). * - * @return A reference of the mutex. + * @param instance The hostname of your instance + * @param access_token Your access token. * - * @since 0.12.3 + * @since before 0.11.0 */ - std::mutex &get_mutex(); + explicit API(const string &instance, const string &access_token); + + /*! + * @brief Destroys the object. + * + * @since 0.100.0 + */ + virtual ~API(); + + /*! + * @brief Sets the useragent. Default is mastodon-cpp/version. + * + * @param useragent The useragent + * + * @since before 0.11.0 + */ + void set_useragent(const string &useragent); + + /*! + * @brief Gets the useragent. + * + * @return The useragent. + * + * @since before 0.11.0 + */ + const string get_useragent() const; + + /*! + * @brief Returns the instance. + * + * @return The instance. + * + * @since before 0.11.0 + */ + const string get_instance() const; + + /*! + * @brief Percent-encodes a string. This is done automatically, unless + * you make a custom request. + * + * Calls curlpp::escape(str) + * + * The only time you should use this, is if you use + * get(const string &call, string &answer). + * + * See RFC 3986 section 2.1 for more info. + * + * @param str The string + * + * @return The percent-encoded string + * + * @since before 0.11.0 + */ + static const string urlencode(const string &str); + + /*! + * @brief Decodes a percent-encoded string. + * + * Calls curlpp::unescape(str) + * + * See RFC 3986 section 2.1 for more info. + * + * @param str The string + * + * @return The decoded string + * + * @since 0.18.0 + */ + static const string urldecode(const string &str); + + /*! + * @brief Register application, step 1/2 + * + * @param client_name The name of the application + * @param redirect_uri urn:ietf:wg:oauth:2.0:oob for none + * @param scopes Scopes (`read`, `write`, `follow`, `push`; + * space separated) + * @param website The website of the application + * @param client_id Returned + * @param client_secret Returned + * @param url Returned, used to generate code for + * register_app2 + * + * @return @ref error "Error code". If the URL has permanently changed, + * 13 is returned and url is set to the new URL. + * + * @since before 0.11.0 + */ + return_call register_app1(const string &client_name, + const string &redirect_uri, + const string &scopes, + const string &website, + string &client_id, + string &client_secret, + string &url); + + + /*! + * @brief Register application, step 2/2 + * + * The access token will be used in all subsequent calls. + * + * @param client_id + * @param client_secret + * @param redirect_uri urn:ietf:wg:oauth:2.0:oob for none + * @param code The code generated by the website + * @param access_token Returned + * + * @return @ref error "Error code". + * + * @since before 0.11.0 + */ + return_call register_app2(const string &client_id, + const string &client_secret, + const string &redirect_uri, + const string &code, + string &access_token); + + /*! + * @brief Gets the header from the last answer. + * + * @param header The header to get + * + * @return The header, or "" on error. + * + * @since before 0.11.0 + */ + const string get_header(const string &header) const; + + /*! + * @brief Turn exceptions on or off. Defaults to off. + * + * This applies to exceptions from curlpp. curlpp::RuntimeError + * and curlpp::LogicError. + * + * @param value true for on, false for off + * + * @return true if exceptions are turned on, false otherwise + * + * @since before 0.11.0 + */ + bool exceptions(const bool &value); + + /*! + * @brief Returns true if exceptions are turned on, false otherwise + */ + bool exceptions() const; + + /*! + * @brief Replaces HTML entities with UTF-8 characters + * + * Supports named and numbered entities, decimal and + * hexadecimal. + * + * @since 0.12.0 + */ + static const string unescape_html(const string &html); + + /*! + * @brief Sets the proxy. + * + * Since mastodon-cpp is built on libcurl, it respects the same + * proxy environment variables. See `man curl`. + * + * @param proxy See `man 3 CURLOPT_PROXY` + * @param userpw See `man 3 CURLOPT_PROXYUSERPWD` (optional) + * + * @since 0.15.0 + */ + void set_proxy(const string &proxy, const string &userpw = ""); + + /*! + * @brief For internal use + * + * @param proxy URL + * @param userpw username:password + * + * @since 0.15.1 + */ + void get_proxy(string &proxy, string &userpw) const; + + /*! + * @brief Make a GET request which doesn't require parameters. + * + * @param call A call defined in Mastodon::API::v1 + * + * @return @ref error "Error code". + * + * @since 0.100.0 + */ + const return_call get(const Mastodon::API::v1 &call); + + /*! + * @brief Make a GET request which requires parameters. + * + * @param call A call defined in Mastodon::API::v1 + * @param parameters A Mastodon::parametermap containing + * parameters + * + * @return @ref error "Error code". + */ + const return_call get(const Mastodon::API::v1 &call, + const parametermap ¶meters); + + /*! + * @brief Make a GET request which requires parameters. + * + * @since 0.100.0 + * + * @param call A call defined in Mastodon::API::v2 + * @param parameters A Mastodon::parametermap containing + * parameters + * + * @return @ref error "Error code". + */ + const return_call get(const Mastodon::API::v2 &call, + const parametermap ¶meters); + + /*! + * @brief Make a custom GET request. + * + * @param call String in the form `/api/v1/example` + * + * @return @ref error "Error code". + * + * @since 0.100.0 + */ + const return_call get(const string &call); + + /*! + * @brief Make a streaming GET request. + * + * @param call A call defined in Mastodon::API::v1 + * @param parameters A Mastodon::parametermap containing + * parameters + * @param ptr Pointer to the http object. Can be used to call + * ptr->cancel_stream() + * + * @return @ref error "Error code". + * + * @since 0.100.0 + */ + return_call get_stream(const Mastodon::API::v1 &call, + const parametermap ¶meters, + std::unique_ptr &ptr); + + /*! + * @brief Make a streaming GET request. + * + * @param call A call defined in Mastodon::API::v1 + * @param ptr Pointer to the http object. Can be used to call + * ptr->cancel_stream() + * + * @return @ref error "Error code". + * + * @since 0.100.0 + */ + return_call get_stream(const Mastodon::API::v1 &call, + std::unique_ptr &ptr); + + /*! + * @brief Make a streaming GET request. + * + * @param call String in the form `/api/v1/example` + * @param ptr Pointer to the http object. Can be used to call + * ptr->cancel_stream() + * + * @return @ref error "Error code". + * + * @since 0.100.0 + */ + return_call get_stream(const string &call, + std::unique_ptr &ptr); + + /*! + * @brief Make a PATCH request. + * + * Binary data must be base64-encoded or a filename. + * + * @param call A call defined in Mastodon::API::v1 + * @param parameters A Mastodon::parametermap containing + * parameters + * + * @return @ref error "Error code". + * + * @since 0.100.0 + */ + return_call patch(const Mastodon::API::v1 &call, + const parametermap ¶meters); + + /*! + * @brief Make a POST request which doesn't require parameters. + * + * @param call A call defined in Mastodon::API::v1 + * + * @return @ref error "Error code". + * + * @since 0.100.0 + */ + return_call post(const Mastodon::API::v1 &call); + + /*! + * @brief Make a POST request which requires parameters. + * + * Binary data must be base64-encoded or a filename. + * + * @param call A call defined in Mastodon::API::v1 + * @param parameters A Mastodon::parametermap containing + * parameters + * + * @return @ref error "Error code". + * + * @since 0.100.0 + */ + return_call post(const Mastodon::API::v1 &call, + const parametermap ¶meters); + + /*! + * @brief Make a custom POST request. + * + * Binary data must be base64-encoded or a filename. + * + * @param call String in the form `/api/v1/example` + * @param parameters A Mastodon::parametermap containing + * parameters + * + * @return @ref error "Error code". + * + * @since 0.100.0 + */ + return_call post(const string &call, + const parametermap ¶meters); + + /*! + * @brief Make a PUT request which requires a parameters. + * + * @param call A call defined in Mastodon::API::v1 + * @param parameters A Mastodon::parametermap containing + * parameters + * + * @return @ref error "Error code". + * + * @since 0.100.0 + */ + return_call put(const Mastodon::API::v1 &call, + const parametermap ¶meters); + + /*! + * @brief Make a custom PUT request. + * + * @param call String in the form `/api/v1/example` + * @param parameters A Mastodon::parametermap containing + * parameters + * + * @return @ref error "Error code". + * + * @since 0.100.0 + */ + return_call put(const string &call, const parametermap ¶meters); + + /*! + * @brief Make a DELETE request which requires parameters. + * + * @param call A call defined in Mastodon::API::v1 + * @param parameters A Mastodon::parametermap containing + * parameters + * + * @return @ref error "Error code". + * + * @since 0.100.0 + */ + return_call del(const Mastodon::API::v1 &call, + const parametermap ¶meters); + + /*! + * @brief Make a custom DELETE request. + * + * @param call String in the form `/api/v1/example` + * @param parameters A Mastodon::parametermap containing + * parameters + * + * @return @ref error "Error code". + * + * @since 0.100.0 + */ + return_call del(const string &call, const parametermap ¶meters); private: - const API &parent; const string _instance; - const string _access_token; - string _headers; - bool _cancel_stream; - std::mutex _mutex; + string _access_token; + string _useragent; + http _http; + bool _exceptions; + string _proxy; + string _proxy_userpw; - size_t callback_write(char* data, size_t size, size_t nmemb, - string *oss); - double callback_progress(double /* dltotal */, double /* dlnow */, - double /* ultotal */, double /* ulnow */); + /*! + * @brief Converts map of parameters into a string. + * + * @param map Map of parameters + * @param firstparam Contains this map the first parameter? + * + * @return String of parameters + */ + const string maptostr(const parametermap &map, + const bool &firstparam = true); + + /*! + * @brief Converts map of parameters into form data + * + * @param map Map of parameters + * + * @return Form data as curlpp::Forms + */ + const curlpp::Forms maptoformdata(const parametermap &map); }; - - /*! - * @brief Used for passing parameters. - * - * Example: - * @code - * parametermap p = - * { - * {"field1", { "value1", "value2" } }, - * {"field2", { "value" } } - * } - * @endcode - * - * @since before 0.11.0 - */ - typedef std::map> parametermap; - - /*! - * @brief A list of all v1 API calls. - * - * The original `/` are substituted by `_`. - * - * @since before 0.11.0 - */ - enum class v1 - { - // Mastodon - accounts_id, - // accounts, - accounts_verify_credentials, - accounts_update_credentials, - accounts_id_followers, - accounts_id_following, - accounts_id_statuses, - accounts_id_follow, - accounts_id_unfollow, - accounts_relationships, - accounts_search, - - apps, - // apps_verify_credentials, - - blocks, - accounts_id_block, - accounts_id_unblock, - - custom_emojis, - - domain_blocks, - - endorsements, - accounts_id_pin, - accounts_id_unpin, - - favourites, - statuses_id_favourite, - statuses_id_unfavourite, - - // filters, - // filters_id, - - follow_requests, - follow_requests_id_authorize, - follow_requests_id_reject, - - // suggestions, - // suggestions_id, - - instance, - - lists, - accounts_id_lists, - lists_id, - lists_id_accounts, - - media, - media_id, - - mutes, - accounts_id_mute, - accounts_id_unmute, - statuses_id_mute, - statuses_id_unmute, - - notifications, - notifications_id, - notifications_clear, - notifications_dismiss, - push_subscription, - - reports, - - // scheduled_statuses, - // scheduled_statuses_id, - - search, - - statuses, - statuses_id, - statuses_id_context, - statuses_id_card, - statuses_id_reblogged_by, - statuses_id_favourited_by, - statuses_id_reblog, - statuses_id_unreblog, - statuses_id_pin, - statuses_id_unpin, - - timelines_home, - // timelines_conversations, - timelines_public, - timelines_tag_hashtag, - timelines_list_list_id, - - streaming_user, - streaming_public, - streaming_public_local, - streaming_hashtag, - // streaming_hashtag_local, - streaming_list, - // streaming_direct, - - // Glitch-Soc - bookmarks, - statuses_id_bookmark, - statuses_id_unbookmark - }; - - /*! - * @brief A list of all v2 API calls. - * - * The original `/` are substituted by `_`. - * - * @since 0.16.0 - */ - enum class v2 - { - search - }; - - /*! - * @brief Constructs a new API object. - * - * To register your application, leave access_token blank and call - * register_app1() and register_app2(). - * - * @param instance The hostname of your instance - * @param access_token Your access token. - * - * @since before 0.11.0 - */ - explicit API(const string &instance, const string &access_token); - - /*! - * @brief Destroys the object. - * - * @since 0.100.0 - */ - virtual ~API(); - - /*! - * @brief Sets the useragent. Default is mastodon-cpp/version. - * - * @param useragent The useragent - * - * @since before 0.11.0 - */ - void set_useragent(const string &useragent); - - /*! - * @brief Gets the useragent. - * - * @return The useragent. - * - * @since before 0.11.0 - */ - const string get_useragent() const; - - /*! - * @brief Returns the instance. - * - * @return The instance. - * - * @since before 0.11.0 - */ - const string get_instance() const; - - /*! - * @brief Percent-encodes a string. This is done automatically, unless you - * make a custom request. - * - * Calls curlpp::escape(str) - * - * The only time you should use this, is if you use - * get(const string &call, string &answer). - * - * See RFC 3986 section 2.1 for more info. - * - * @param str The string - * - * @return The percent-encoded string - * - * @since before 0.11.0 - */ - static const string urlencode(const string &str); - - /*! - * @brief Decodes a percent-encoded string. - * - * Calls curlpp::unescape(str) - * - * See RFC 3986 section 2.1 for more info. - * - * @param str The string - * - * @return The decoded string - * - * @since 0.18.0 - */ - static const string urldecode(const string &str); - - /*! - * @brief Register application, step 1/2 - * - * @param client_name The name of the application - * @param redirect_uri urn:ietf:wg:oauth:2.0:oob for none - * @param scopes Scopes (`read`, `write`, `follow`, `push`; space - * separated) - * @param website The website of the application - * @param client_id Returned - * @param client_secret Returned - * @param url Returned, used to generate code for register_app2 - * - * @return @ref error "Error code". If the URL has permanently changed, 13 - * is returned and url is set to the new URL. - * - * @since before 0.11.0 - */ - return_call register_app1(const string &client_name, - const string &redirect_uri, - const string &scopes, - const string &website, - string &client_id, - string &client_secret, - string &url); - - - /*! - * @brief Register application, step 2/2 - * - * The access token will be used in all subsequent calls. - * - * @param client_id - * @param client_secret - * @param redirect_uri urn:ietf:wg:oauth:2.0:oob for none - * @param code The code generated by the website - * @param access_token Returned - * - * @return @ref error "Error code". - * - * @since before 0.11.0 - */ - return_call register_app2(const string &client_id, - const string &client_secret, - const string &redirect_uri, - const string &code, - string &access_token); - - /*! - * @brief Gets the header from the last answer. - * - * @param header The header to get - * - * @return The header, or "" on error. - * - * @since before 0.11.0 - */ - const string get_header(const string &header) const; - - /*! - * @brief Turn exceptions on or off. Defaults to off. - * - * This applies to exceptions from curlpp. curlpp::RuntimeError and - * curlpp::LogicError. - * - * @param value true for on, false for off - * - * @return true if exceptions are turned on, false otherwise - * - * @since before 0.11.0 - */ - bool exceptions(const bool &value); - - /*! - * @brief Returns true if exceptions are turned on, false otherwise - */ - bool exceptions() const; - - /*! - * @brief Replaces HTML entities with UTF-8 characters - * - * Supports named and numbered entities, decimal and hexadecimal. - * - * @since 0.12.0 - */ - static const string unescape_html(const string &html); - - /*! - * @brief Sets the proxy. - * - * Since mastodon-cpp is built on libcurl, it respects the same - * proxy environment variables. See `man curl`. - * - * @param proxy See `man 3 CURLOPT_PROXY` - * @param userpw See `man 3 CURLOPT_PROXYUSERPWD` (optional) - * - * @since 0.15.0 - */ - void set_proxy(const string &proxy, const string &userpw = ""); - - /*! - * @brief For internal use - * - * @param proxy URL - * @param userpw username:password - * - * @since 0.15.1 - */ - void get_proxy(string &proxy, string &userpw) const; - - /*! - * @brief Make a GET request which doesn't require parameters. - * - * @param call A call defined in Mastodon::API::v1 - * - * @return @ref error "Error code". - * - * @since 0.100.0 - */ - const return_call get(const Mastodon::API::v1 &call); - - /*! - * @brief Make a GET request which requires parameters. - * - * @param call A call defined in Mastodon::API::v1 - * @param parameters A Mastodon::API::parametermap containing parameters - * - * @return @ref error "Error code". - */ - const return_call get(const Mastodon::API::v1 &call, - const parametermap ¶meters); - - /*! - * @brief Make a GET request which requires parameters. - * - * @since 0.100.0 - * - * @param call A call defined in Mastodon::API::v2 - * @param parameters A Mastodon::API::parametermap containing parameters - * - * @return @ref error "Error code". - */ - const return_call get(const Mastodon::API::v2 &call, - const parametermap ¶meters); - - /*! - * @brief Make a custom GET request. - * - * @param call String in the form `/api/v1/example` - * - * @return @ref error "Error code". - * - * @since 0.100.0 - */ - const return_call get(const string &call); - - /*! - * @brief Make a streaming GET request. - * - * @param call A call defined in Mastodon::API::v1 - * @param parameters A Mastodon::API::parametermap containing parameters - * @param ptr Pointer to the http object. Can be used to call - * ptr->cancel_stream() - * - * @return @ref error "Error code". - * - * @since 0.100.0 - */ - return_call get_stream(const Mastodon::API::v1 &call, - const parametermap ¶meters, - std::unique_ptr &ptr); - - /*! - * @brief Make a streaming GET request. - * - * @param call A call defined in Mastodon::API::v1 - * @param ptr Pointer to the http object. Can be used to call - * ptr->cancel_stream() - * - * @return @ref error "Error code". - * - * @since 0.100.0 - */ - return_call get_stream(const Mastodon::API::v1 &call, - std::unique_ptr &ptr); - - /*! - * @brief Make a streaming GET request. - * - * @param call String in the form `/api/v1/example` - * @param ptr Pointer to the http object. Can be used to call - * ptr->cancel_stream() - * - * @return @ref error "Error code". - * - * @since 0.100.0 - */ - return_call get_stream(const string &call, - std::unique_ptr &ptr); - - /*! - * @brief Make a PATCH request. - * - * Binary data must be base64-encoded or a filename. - * - * @param call A call defined in Mastodon::API::v1 - * @param parameters A Mastodon::API::parametermap containing parameters - * - * @return @ref error "Error code". - * - * @since 0.100.0 - */ - return_call patch(const Mastodon::API::v1 &call, - const parametermap ¶meters); - - /*! - * @brief Make a POST request which doesn't require parameters. - * - * @param call A call defined in Mastodon::API::v1 - * - * @return @ref error "Error code". - * - * @since 0.100.0 - */ - return_call post(const Mastodon::API::v1 &call); - - /*! - * @brief Make a POST request which requires parameters. - * - * Binary data must be base64-encoded or a filename. - * - * @param call A call defined in Mastodon::API::v1 - * @param parameters A Mastodon::API::parametermap containing parameters - * - * @return @ref error "Error code". - * - * @since 0.100.0 - */ - return_call post(const Mastodon::API::v1 &call, - const parametermap ¶meters); - - /*! - * @brief Make a custom POST request. - * - * Binary data must be base64-encoded or a filename. - * - * @param call String in the form `/api/v1/example` - * @param parameters A Mastodon::API::parametermap containing parameters - * - * @return @ref error "Error code". - * - * @since 0.100.0 - */ - return_call post(const string &call, - const parametermap ¶meters); - - /*! - * @brief Make a PUT request which requires a parameters. - * - * @param call A call defined in Mastodon::API::v1 - * @param parameters A Mastodon::API::parametermap containing parameters - * - * @return @ref error "Error code". - * - * @since 0.100.0 - */ - return_call put(const Mastodon::API::v1 &call, - const parametermap ¶meters); - - /*! - * @brief Make a custom PUT request. - * - * @param call String in the form `/api/v1/example` - * @param parameters A Mastodon::API::parametermap containing parameters - * - * @return @ref error "Error code". - * - * @since 0.100.0 - */ - return_call put(const string &call, const parametermap ¶meters); - - /*! - * @brief Make a DELETE request which requires parameters. - * - * @param call A call defined in Mastodon::API::v1 - * @param parameters A Mastodon::API::parametermap containing parameters - * - * @return @ref error "Error code". - * - * @since 0.100.0 - */ - return_call del(const Mastodon::API::v1 &call, - const parametermap ¶meters); - - /*! - * @brief Make a custom DELETE request. - * - * @param call String in the form `/api/v1/example` - * @param parameters A Mastodon::API::parametermap containing parameters - * - * @return @ref error "Error code". - * - * @since 0.100.0 - */ - return_call del(const string &call, const parametermap ¶meters); - -private: - const string _instance; - string _access_token; - string _useragent; - http _http; - bool _exceptions; - string _proxy; - string _proxy_userpw; - - /*! - * @brief Converts map of parameters into a string. - * - * @param map Map of parameters - * @param firstparam Contains this map the first parameter? - * - * @return String of parameters - */ - const string maptostr(const parametermap &map, - const bool &firstparam = true); - - /*! - * @brief Converts map of parameters into form data - * - * @param map Map of parameters - * - * @return Form data as curlpp::Forms - */ - const curlpp::Forms maptoformdata(const parametermap &map); -}; } #endif diff --git a/src/types.hpp b/src/types.hpp new file mode 100644 index 0000000..d71156b --- /dev/null +++ b/src/types.hpp @@ -0,0 +1,45 @@ +/* This file is part of mastodon-cpp. + * Copyright © 2019 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef MASTODON_CPP_TYPES_HPP +#define MASTODON_CPP_TYPES_HPP + +#include +#include +#include + +using std::string; + +namespace Mastodon +{ + /*! + * @brief Used for passing parameters. + * + * Example: + * @code + * parametermap p = + * { + * {"field1", { "value1", "value2" } }, + * {"field2", { "value" } } + * } + * @endcode + * + * @since before 0.11.0 + */ + typedef std::map> parametermap; +} + +#endif // MASTODON_CPP_TYPES_HPP