diff --git a/examples/example03_get_favourites.cpp b/examples/example03_get_favourites.cpp new file mode 100644 index 0000000..067ecc5 --- /dev/null +++ b/examples/example03_get_favourites.cpp @@ -0,0 +1,91 @@ +// This file is part of mastodon-cpp. +// Get 2 pages of 5 favourites each. + +// Don't compile this if the Easy-interface is turned off +#ifndef WITHOUT_EASY + +#include +#include +#include +#include "mastodon-cpp.hpp" +#include "easy/all.hpp" + +using std::cout; +using std::cerr; +using std::endl; +using std::string; +using std::vector; +using namespace Mastodon; + +void print_favs(Easy::API &masto, const string &max_id = "") +{ + // Set up parameters. + Mastodon::parameters params = + { + { "limit", { "5" } }, + { "exclude_types", { "follow", "reblog", "mention" } } + }; + // Set max_id if given. + if (!max_id.empty()) + { + params.push_back({ "max_id", { max_id } }); + } + + // Retrieve the last 5 favourites. + return_call ret = masto.get(API::v1::notifications, params); + // If no error was returned. + if (ret) + { + // Convert answer to vector of strings, loop through vector. + for (const string &str : Easy::json_array_to_vector(ret.answer)) + { + // Construct Mastodon::Easy::Notification from string. + const Easy::Notification notif(str); + cout << notif.created_at().strtime("%F %T: "); + cout << notif.account().display_name() << " favourited a status.\n"; + } + } + else + { + // Print error message. + cerr << ret.error_message << endl; + // Print HTTP status code, if there is one. + if (ret.http_error_code != 0) + { + cerr << "HTTP status code: " + << std::to_string(ret.http_error_code) << endl; + } + } +} + +int main(int argc, char *argv[]) +{ + const vector args(argv, argv + argc); + if (args.size() < 3) + { + std::cerr << "usage: " << args[0] << " \n"; + return 1; + } + + // Construct a Mastodon::Easy object. + Easy::API masto(args[1], args[2]); + print_favs(masto); + + cout << "\nLink header is: " << masto.get_header("link") << "\n\n"; + + // Get the max_id for pagination. + // See + print_favs(masto, masto.get_link().max_id()); + // The same: print_favs(masto, masto.get_link().next()); + + return 0; +} + +#else +#include +int main() +{ + std::cout << "mastodon-cpp was compiled without Easy support.\n"; + return 255; +} +#endif // WITHOUT_EASY diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index f583e77..fe4f1e1 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -101,8 +101,8 @@ Easy::Link::Link(const string &link_header) : _next() , _prev() { - std::regex renext("max_id=([[:digit:]]*)"); - std::regex reprev("since_id=([[:digit:]]*)"); + std::regex renext("max_id=([[:alnum:]]*)"); + std::regex reprev("(?:since|min)_id=([[:alnum:]]*)"); std::smatch match; if (std::regex_search(link_header, match, renext)) @@ -134,3 +134,8 @@ const string Easy::Link::since_id() const { return _prev; } + +const string Easy::Link::min_id() const +{ + return _prev; +} diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index ed53b3b..c7c7902 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -89,6 +89,13 @@ namespace Easy */ const string since_id() const; + /*! + * @brief Returns min_id. + * + * @since 0.111.0 + */ + const string min_id() const; + private: string _next; string _prev; diff --git a/src/http.cpp b/src/http.cpp index 5296ffa..48e8b32 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -234,13 +234,17 @@ return_call API::http::request_common(const http_method &meth, } HTTPResponse response; - istream &rs = session.receiveResponse(response); + istream &body_stream = session.receiveResponse(response); const uint16_t http_code = response.getStatus(); ttdebug << "Response code: " << http_code << '\n'; answer.clear(); - StreamCopier::copyToString(rs, answer); + StreamCopier::copyToString(body_stream, answer); + + std::ostringstream headers_stream; + response.write(headers_stream); + _headers = headers_stream.str(); switch (http_code) { diff --git a/src/mastodon-cpp.cpp b/src/mastodon-cpp.cpp index 332603c..95b7e73 100644 --- a/src/mastodon-cpp.cpp +++ b/src/mastodon-cpp.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include "version.hpp" @@ -265,10 +266,13 @@ return_call API::register_app2(const string &client_id, return ret; } -const string API::get_header(const std::string &header) const +const string API::get_header(std::string header) const { string headers; _http.get_headers(headers); + std::transform(headers.begin(), headers.end(), headers.begin(), ::tolower); + std::transform(header.begin(), header.end(), header.begin(), ::tolower); + size_t startpos = headers.find(header + ':'); if (startpos != std::string::npos) { @@ -307,7 +311,7 @@ const parameters API::delete_params(const parameters ¶ms, std::copy_if(params.begin(), params.end(), newparams.begin(), [&keys](const param &p) { - return std::any_of(keys.begin(), keys.end(), + return std::all_of(keys.begin(), keys.end(), [&p](const string &k) { return (k != p.key); diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index 98c0b37..57e44f5 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -411,7 +411,7 @@ namespace Mastodon string &access_token); /*! - * @brief Gets the header from the last answer. + * @brief Gets the header from the last answer. Case insensitive. * * @param header The header to get * @@ -419,7 +419,7 @@ namespace Mastodon * * @since before 0.11.0 */ - const string get_header(const string &header) const; + const string get_header(string header) const; /*! * @brief Turn exceptions on or off. Defaults to off.