Merge branch 'develop'
This commit is contained in:
commit
b4d6e37c96
91
examples/example03_get_favourites.cpp
Normal file
91
examples/example03_get_favourites.cpp
Normal file
@ -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 <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#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<string> args(argv, argv + argc);
|
||||
if (args.size() < 3)
|
||||
{
|
||||
std::cerr << "usage: " << args[0] << " <instance> <auth token>\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 <https://docs.joinmastodon.org/api/rest/notifications/#pagination>
|
||||
print_favs(masto, masto.get_link().max_id());
|
||||
// The same: print_favs(masto, masto.get_link().next());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
#include <iostream>
|
||||
int main()
|
||||
{
|
||||
std::cout << "mastodon-cpp was compiled without Easy support.\n";
|
||||
return 255;
|
||||
}
|
||||
#endif // WITHOUT_EASY
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -20,6 +20,7 @@
|
||||
#include <utility>
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
#include <algorithm>
|
||||
#include <Poco/Net/FilePartSource.h>
|
||||
#include <Poco/URI.h>
|
||||
#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);
|
||||
|
@ -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.
|
||||
|
Reference in New Issue
Block a user