This repository has been archived on 2020-05-10. You can view files and clone it, but cannot push or open issues or pull requests.
mastodon-cpp/examples/example08_rate_limiting.cpp

58 lines
1.4 KiB
C++
Raw Normal View History

2018-02-25 23:20:02 +01:00
/* This file is part of mastodon-cpp.
* In this example we look at HTTP headers to determine how many calls we are
* allowed to make.
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
2018-03-21 20:12:45 +01:00
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
2018-02-25 23:20:02 +01:00
using Mastodon::API;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
Mastodon::API masto(argv[1], argv[2]);
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
std::string answer;
std::uint16_t ret;
ret = masto.get(API::v1::accounts_verify_credentials, answer);
if (ret == 0)
{
std::string remaining = masto.get_header("X-RateLimit-Remaining");
std::string reset = masto.get_header("X-RateLimit-Reset");
std::cout << "You are allowed to make " << remaining
<< " calls until the counter is reset at " << reset << '\n';
if (std::stoi(remaining) <= 1)
{
std::cout << "🔥 Please let the server cool off a bit! 🔥\n";
}
}
2018-02-28 22:37:30 +01:00
else if (ret == 13)
2018-02-25 23:20:02 +01:00
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return 0;
}