diff --git a/examples/example09_nlohmann_json.cpp b/examples/example09_nlohmann_json.cpp new file mode 100644 index 0000000..bf8db5c --- /dev/null +++ b/examples/example09_nlohmann_json.cpp @@ -0,0 +1,145 @@ +/* This file is part of mastodonpp. + * Copyright © 2020 tastytea + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +// Get the last 4 public statuses of an instance and process them with +// nlohmann-json. + +#if __has_include("mastodonpp.hpp") +# include "mastodonpp.hpp" // We're building mastodonpp. +#else +# include // We're building outside mastodonpp. +#endif + +// Don't compile if nlohmann-json can't be found. +#if __has_include() +# include + +#include +#include +#include +#include +#include + +namespace masto = mastodonpp; +using json = nlohmann::json; +using std::exit; +using std::cout; +using std::cerr; +using std::to_string; +using std::string_view; +using std::vector; + +void handle_error(const masto::answer_type &answer); + +int main(int argc, char *argv[]) +{ + const vector args(argv, argv + argc); + if (args.size() <= 1) + { + cerr << "Usage: " << args[0] << " \n"; + return 1; + } + + try + { + // Initialize Instance and Connection. + masto::Instance instance{args[1], {}}; + masto::Connection connection{instance}; + + // Get the last 4 public statuses of the instance. + auto answer{connection.get(masto::API::v1::timelines_public, + { + {"limit", "4"}, + {"local", "true"} + })}; + if (answer) + { + // Parse JSON string. + auto statuses{json::parse(answer.body)}; + for (const auto &status : statuses) + { + // Extract the info we want and print it. + const auto acct{status["account"]["acct"].get()}; + const auto content{status["content"].get()}; + const auto id{status["id"].get()}; + cout << acct << " wrote status " << id << ": \n"; + cout << " " << content.substr(0, 76) << " …\n"; + + // Print tags if there are any. + const auto tags{status["tags"]}; + if (!tags.empty()) + { + cout << " Tags: "; + for (const auto &tag : tags) + { + cout << '#' << tag["name"].get() << " "; + } + cout << '\n'; + } + + // Print the number of attachments. + const auto n_attachments{status["media_attachments"].size()}; + if (n_attachments > 0) + { + cout << " " << n_attachments << " attachment"; + if (n_attachments > 1) + { + cout << "s"; + } + cout << ".\n"; + } + } + } + else + { + handle_error(answer); + } + + } + catch (const masto::CURLException &e) + { + // Only libcurl errors that are not network errors will be thrown. + // There went probably something wrong with the initialization. + cerr << e.what() << '\n'; + } +} + +void handle_error(const masto::answer_type &answer) +{ + if (answer.curl_error_code == 0) + { + // If it is no libcurl error, it must be an HTTP error. + cerr << "HTTP status: " << answer.http_status << '\n'; + } + else + { + // Network errors like “Couldn't resolve host.”. + cerr << "libcurl error " << to_string(answer.curl_error_code) + << ": " << answer.error_message << '\n'; + } + + exit(1); +} + +#else + +#include +int main() +{ + std::cout << "Example could not be compiled " + "because nlohmann-json was not found.\n"; +} + +#endif // __has_include() diff --git a/include/mastodonpp.hpp b/include/mastodonpp.hpp index 1b3055b..98733d6 100644 --- a/include/mastodonpp.hpp +++ b/include/mastodonpp.hpp @@ -106,6 +106,7 @@ * @example example06_update_name.cpp * @example example07_delete_status.cpp * @example example08_obtain_token.cpp + * @example example09_nlohmann_json.cpp */ /*!