diff --git a/CMakeLists.txt b/CMakeLists.txt index a6e428b..4db25d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ option(WITH_TESTS "Compile tests." NO) option(WITH_EXAMPLES "Compile examples." NO) option(WITH_DEB "Prepare for the building of .deb packages." NO) option(WITH_RPM "Prepare for the building of .rpm packages." NO) +option(WITH_CLANG-TIDY "Check sourcecode with clang-tidy while compiling." NO) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -29,9 +30,12 @@ set(CMAKE_CXX_EXTENSIONS OFF) include(debug_flags) -# Disable debug log. -if(NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug") - add_definitions("-DNDEBUG") +if(WITH_CLANG-TIDY) + set(CMAKE_CXX_CLANG_TIDY + "clang-tidy" + "-p=build" + "-header-filter=${PROJECT_SOURCE_DIR}" + "-quiet") endif() add_subdirectory(src) diff --git a/README.adoc b/README.adoc index 9e1405c..506d6c8 100644 --- a/README.adoc +++ b/README.adoc @@ -3,6 +3,8 @@ :project: mastodonpp :uri-base: https://schlomp.space/tastytea/{project} :uri-branch-main: {uri-base}/src/branch/main +:uri-wp-mastodon: https://en.wikipedia.org/wiki/Mastodon_(software) +:uri-pleroma: https://pleroma.social/ :uri-mastodon-cpp: https://schlomp.space/tastytea/mastodon-cpp :uri-reference: https://doc.schlomp.space/{project}/ :uri-gcc: https://gcc.gnu.org/ @@ -14,17 +16,19 @@ :uri-rpm-build: http://www.rpm.org :uri-libcurl: https://curl.haxx.se/libcurl/ :uri-nodeinfo: https://nodeinfo.diaspora.software/ +:uri-clang-tidy: https://clang.llvm.org/extra/clang-tidy/ -*{project}* is a C++ wrapper for the Mastodon and Pleroma APIs. It replaces +*{project}* is a C++ wrapper for the link:{uri-wp-mastodon}[Mastodon] and +link:{uri-pleroma}[Pleroma] APIs. It replaces link:{uri-mastodon-cpp}[mastodon-cpp]. We aim to create a library that is comfortable, yet minimal. All API endpoints from Mastodon and Pleroma are stored in ``enum class``es, to counteract typos and make your life easier. The network-facing code is built on link:{uri-libcurl}[libcurl], a mature and stable library that is available on -virtually every operating system. The library does not parse the responses -itself, but returns to you the raw data, because we know everyone has their -favorite JSON library and we don't want to impose our choice on you! +most operating systems. The library does not parse the responses itself, but +returns to you the raw data, because we know everyone has their favorite JSON +library and we don't want to impose our choice on you! == Features @@ -147,6 +151,8 @@ cmake --build . -- -j$(nproc --ignore=1) * `-DCMAKE_BUILD_TYPE=Debug` for a debug build. * `-DWITH_TESTS=YES` if you want to compile the tests. * `-DWITH_EXAMPLES=YES` if you want to compile the examples. +* `-DWITH_CLANG-TIDY=YES` to check the sourcecode with + link:{uri-clang-tidy}[clang-tidy] while compiling. * One of: ** `-DWITH_DEB=YES` if you want to be able to generate a deb-package. ** `-DWITH_RPM=YES` if you want to be able to generate an rpm-package. diff --git a/examples/example09_nlohmann_json.cpp b/examples/example09_nlohmann_json.cpp new file mode 100644 index 0000000..ad94fac --- /dev/null +++ b/examples/example09_nlohmann_json.cpp @@ -0,0 +1,148 @@ +/* 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'; + } + catch (const nlohmann::detail::exception &e) + { + cerr << "JSON exception: " << 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/curl_wrapper.hpp b/include/curl_wrapper.hpp index 0088197..f72847e 100644 --- a/include/curl_wrapper.hpp +++ b/include/curl_wrapper.hpp @@ -320,7 +320,8 @@ private: * * @since 0.1.0 */ - bool replace_parameter_in_uri(string &uri, const parameterpair ¶meter); + static bool replace_parameter_in_uri(string &uri, + const parameterpair ¶meter); /*! * @brief Add parameters to URI. @@ -330,7 +331,8 @@ private: * * @since 0.1.0 */ - void add_parameters_to_uri(string &uri, const parametermap ¶meters); + static void add_parameters_to_uri(string &uri, + const parametermap ¶meters); /*! * @brief Add `*curl_mimepart` to `*curl_mime`. @@ -341,8 +343,8 @@ private: * * @since 0.2.0 */ - void add_mime_part(curl_mime *mime, - string_view name, string_view data) const; + static void add_mime_part(curl_mime *mime, + string_view name, string_view data); /*! * @brief Convert parametermap to `*curl_mime`. 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 */ /*! diff --git a/src/curl_wrapper.cpp b/src/curl_wrapper.cpp index bb22a5c..97c519d 100644 --- a/src/curl_wrapper.cpp +++ b/src/curl_wrapper.cpp @@ -40,7 +40,7 @@ static atomic curlwrapper_instances{0}; CURLWrapper::CURLWrapper() : _curl_buffer_error{} - , _stream_cancelled(false) + , _stream_cancelled{false} { if (curlwrapper_instances == 0) { @@ -337,7 +337,7 @@ void CURLWrapper::setup_curl() // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) curl_easy_setopt(_connection, CURLOPT_NOPROGRESS, 0L); - set_useragent((string("mastodonpp/") += version)); + CURLWrapper::set_useragent((string("mastodonpp/") += version)); // The next 2 only fail if HTTP is not supported. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) @@ -415,7 +415,7 @@ void CURLWrapper::add_parameters_to_uri(string &uri, } void CURLWrapper::add_mime_part(curl_mime *mime, - string_view name, string_view data) const + string_view name, string_view data) { curl_mimepart *part{curl_mime_addpart(mime)}; if (part == nullptr)