C++ wrapper for the Mastodon API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

example08_rate_limiting.cpp 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* This file is part of mastodon-cpp.
  2. * In this example we look at HTTP headers to determine how many calls we are
  3. * allowed to make.
  4. */
  5. #include <iostream>
  6. #include <vector>
  7. #include <string>
  8. #include <cstdint>
  9. #ifdef MASTODON_CPP
  10. #include "mastodon-cpp.hpp"
  11. #else
  12. #include <mastodon-cpp/mastodon-cpp.hpp>
  13. #endif
  14. using Mastodon::API;
  15. int main(int argc, char *argv[])
  16. {
  17. if (argc < 3)
  18. {
  19. std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
  20. return 1;
  21. }
  22. Mastodon::API masto(argv[1], argv[2]);
  23. masto.set_useragent("mastodon-cpp-example/1.3.3.7");
  24. std::string answer;
  25. std::uint16_t ret;
  26. ret = masto.get(API::v1::accounts_verify_credentials, answer);
  27. if (ret == 0)
  28. {
  29. std::string remaining = masto.get_header("X-RateLimit-Remaining");
  30. std::string reset = masto.get_header("X-RateLimit-Reset");
  31. std::cout << "You are allowed to make " << remaining
  32. << " calls until the counter is reset at " << reset << '\n';
  33. if (std::stoi(remaining) <= 1)
  34. {
  35. std::cout << "🔥 Please let the server cool off a bit! 🔥\n";
  36. }
  37. }
  38. else if (ret == 13)
  39. {
  40. std::cerr << "The URL has permanently changed.\n" <<
  41. "New URL: " << answer << '\n';
  42. return ret;
  43. }
  44. else
  45. {
  46. std::cerr << "Error code: " << ret << '\n';
  47. return ret;
  48. }
  49. return 0;
  50. }