diff --git a/.drone.yml b/.drone.yml index 6f08113..cfe0244 100644 --- a/.drone.yml +++ b/.drone.yml @@ -26,7 +26,7 @@ steps: - alias apt-get='rm -f /var/cache/apt/archives/lock && apt-get' - apt-get update -q - apt-get install -qq build-essential cmake - - apt-get install -qq catch + - apt-get install -qq catch libcurl4-openssl-dev - rm -rf build && mkdir -p build && cd build - cmake -G "Unix Makefiles" -DWITH_TESTS=YES .. - make VERBOSE=1 @@ -49,7 +49,7 @@ steps: - alias apt-get='rm -f /var/cache/apt/archives/lock && apt-get' - apt-get update -q - apt-get install -qq build-essential cmake - - apt-get install -qq catch + - apt-get install -qq catch libcurl4-openssl-dev - rm -rf build && mkdir -p build && cd build - cmake -G "Unix Makefiles" -DWITH_TESTS=YES .. - make VERBOSE=1 diff --git a/CMakeLists.txt b/CMakeLists.txt index ae44169..e64a888 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,6 @@ list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") # Project build options. option(WITH_TESTS "Compile tests." NO) -# option(WITH_DOC "Generate HTML documentation." YES) 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) @@ -31,6 +30,10 @@ set(CMAKE_CXX_EXTENSIONS OFF) include(debug_flags) add_subdirectory(src) +add_subdirectory(include) + +add_subdirectory(cmake) +add_subdirectory(pkg-config) if(WITH_TESTS) add_subdirectory(tests) diff --git a/README.adoc b/README.adoc index d9dbafc..56f4e53 100644 --- a/README.adoc +++ b/README.adoc @@ -4,8 +4,76 @@ :uri-base: https://schlomp.space/tastytea/{project} :uri-branch-main: {uri-base}/src/branch/main :uri-mastodon-cpp: https://schlomp.space/tastytea/mastodon-cpp +:uri-reference: https://doc.schlomp.space/{project}/ +:uri-gcc: https://gcc.gnu.org/ +:uri-cmake: https://cmake.org/ +:uri-doxygen: http://www.doxygen.nl/ +:uri-catch: https://github.com/catchorg/Catch2 +:uri-dpkg: https://packages.qa.debian.org/dpkg +:uri-rpm-build: http://www.rpm.org *{project}* is a C++ wrapper for the Mastodon API. It replaces link:{uri-mastodon-cpp}[mastodon-cpp]. +== Usage + +Have a look at the link:{uri-reference}[reference]. + +// === Examples + +== Install + +// === Gentoo + +// [source,shell] +// -------------------------------------------------------------------------------- +// eselect repository enable tastytea +// echo 'dev-cpp/mastodonpp' >> /etc/portage/package.accept_keywords/mastodonpp +// emaint sync -r tastytea +// emerge -a dev-cpp/mastodonpp +// -------------------------------------------------------------------------------- + +=== From source + +==== Dependencies + +* Tested OS: Linux +* C++ compiler (tested: link:{uri-gcc}[GCC] 7/8/9) +* link:{uri-cmake}[CMake] (at least: 3.9) +* Optional + ** Documentation: link:{uri-doxygen}[Doxygen] (tested: 1.8) + ** Tests: link:{uri-catch}[Catch] (tested: 2.5 / 1.2) +// ** DEB package: link:{uri-dpkg}[dpkg] (tested: 1.18) +// ** RPM package: link:{uri-rpm-build}[rpm-build] (tested: 4.11) + +==== Get sourcecode + +===== Release + +Download the current release at link:{uri-base}/releases[schlomp.space]. + +===== Development version + +[source,shell] +-------------------------------------------------------------------------------- +git clone https://schlomp.space/tastytea/mastodonpp.git +-------------------------------------------------------------------------------- + +==== Compile + +[source,shell] +-------------------------------------------------------------------------------- +mkdir -p build && cd build +cmake .. +cmake --build . -- -j$(nproc --ignore=1) +-------------------------------------------------------------------------------- + +.CMake options: +* `-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. +// * 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. + include::{uri-base}/raw/branch/main/CONTRIBUTING.adoc[] diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt new file mode 100644 index 0000000..fd94154 --- /dev/null +++ b/cmake/CMakeLists.txt @@ -0,0 +1,19 @@ +include(CMakePackageConfigHelpers) +include(GNUInstallDirs) + +write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake" + VERSION ${PACKAGE_VERSION} + COMPATIBILITY SameMajorVersion) + +configure_file("${PROJECT_NAME}Config.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" @ONLY) + +install(EXPORT ${PROJECT_NAME}Targets + FILE "${PROJECT_NAME}Targets.cmake" + NAMESPACE "${PROJECT_NAME}::" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") + +install(FILES + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") diff --git a/cmake/mastodonppConfig.cmake.in b/cmake/mastodonppConfig.cmake.in new file mode 100644 index 0000000..8c1180a --- /dev/null +++ b/cmake/mastodonppConfig.cmake.in @@ -0,0 +1,5 @@ +include(CMakeFindDependencyMacro) + +find_dependency(CURL REQUIRED) + +include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake") diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt new file mode 100644 index 0000000..5594349 --- /dev/null +++ b/include/CMakeLists.txt @@ -0,0 +1,6 @@ +include(GNUInstallDirs) + +# The trailing / is important. +install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/" + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}" + FILES_MATCHING PATTERN "*.hpp") diff --git a/include/api.hpp b/include/api.hpp new file mode 100644 index 0000000..f4d5a14 --- /dev/null +++ b/include/api.hpp @@ -0,0 +1,94 @@ +/* This file is part of mastodonpp. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef MASTODONPP_API_HPP +#define MASTODONPP_API_HPP + +#include +#include +#include + +namespace mastodonpp +{ + +using std::string; +using std::variant; + +/*! + * @brief Holds API endpoints. + * + * @since 0.1.0 + * + * @headerfile api.hpp mastodonpp/api.hpp + */ +class API +{ +public: + /*! + * @brief An enumeration of all v1 %API endpoints. + * + * The original `/` are substituted with `_`. + * + * @since 0.1.0 + */ + enum class v1 + { + instance + }; + + /*! + * @brief An enumeration of all v2 %API endpoints. + * + * The original `/` are substituted with `_`. + * + * @since 0.1.0 + */ + enum class v2 + { + search + }; + + /*! + * @brief Type for endpoints. Either API::v1 or API::v2. + * + * @since 0.1.0 + */ + using endpoint_type = variant; + + /*! + * @brief Constructs an API object. You should never need this. + * + * This constructor exists to hide away the class members, which are used + * internally. + * + * @since 0.1.0 + */ + explicit API(endpoint_type &endpoint); + + /*! + * @brief Convert #endpoint_type to string. + * + * @since 0.1.0 + */ + string to_string() const; + +private: + const endpoint_type _endpoint; +}; + +} // namespace mastodonpp + +#endif // MASTODONPP_API_HPP diff --git a/include/exceptions.hpp b/include/exceptions.hpp new file mode 100644 index 0000000..a70e563 --- /dev/null +++ b/include/exceptions.hpp @@ -0,0 +1,82 @@ +/* This file is part of mastodonpp. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef MASTODONPP_EXCEPTIONS_HPP +#define MASTODONPP_EXCEPTIONS_HPP + +#include + +#include +#include +#include + +namespace mastodonpp +{ + +using std::uint16_t; +using std::exception; +using std::string; + +/*! + * @brief Exception for libcurl errors. + * + * @since 0.1.0 + * + * @headerfile exceptions.hpp mastodonpp/exceptions.hpp + */ +class CURLException : public exception +{ +public: + /*! + * @brief Constructor with error code and message. + * + * @since 0.1.0 + */ + explicit CURLException(const CURLcode &error, string message); + + /*! + * @brief Constructor with error code, message and error buffer. + * + * @since 0.1.0 + */ + explicit CURLException(const CURLcode &error, string message, + string error_buffer); + + /*! + * @brief The error code returned by libcurl. + * + * For more information consult libcurl-errors(3). + * + * @since 0.1.0 + */ + const CURLcode error_code; + + /*! + * @brief Returns the error code, message and error buffer. + * + * @since 0.1.0 + */ + [[nodiscard]] + const char *what() const noexcept override; + +private: + const string _message; + const string _error_buffer; +}; + +} // namespace mastodonpp + +#endif // MASTODONPP_EXCEPTIONS_HPP diff --git a/include/instance.hpp b/include/instance.hpp new file mode 100644 index 0000000..cc9cd50 --- /dev/null +++ b/include/instance.hpp @@ -0,0 +1,76 @@ +/* This file is part of mastodonpp. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef MASTODONPP_INSTANCE_HPP +#define MASTODONPP_INSTANCE_HPP + +#include + +#include + +namespace mastodonpp +{ + +using std::string; + +/*! + * @brief Holds the hostname and access token of an instance. + * + * @since 0.1.0 + * + * @headerfile instance.hpp mastodonpp/instance.hpp + */ +class Instance +{ +public: + /*! + * @brief Construct a new Instance object. + * + * @param instance The hostname of the instance. + * @param access_token Your access token. + * + * @since 0.1.0 + */ + explicit Instance(string instance, string access_token); + ~Instance(); + +private: + const string _instance; + string _access_token; + CURL *_connection; + char _curl_buffer_error[CURL_ERROR_SIZE]; + string _curl_buffer; + + + /*! + * @brief libcurl write callback function. + * + * @since 0.1.0 + */ + static int writer(char *data, size_t size, size_t nmemb, + string *writerData); + + /*! + * @brief Setup libcurl connection. + * + * @since 0.1.0 + */ + void setup_curl(); +}; + +} // namespace mastodonpp + +#endif // MASTODONPP_INSTANCE_HPP diff --git a/include/mastodonpp.hpp b/include/mastodonpp.hpp index 080d4b2..1362ad3 100644 --- a/include/mastodonpp.hpp +++ b/include/mastodonpp.hpp @@ -17,10 +17,15 @@ #ifndef MASTODONPP_HPP #define MASTODONPP_HPP +#include "api.hpp" +#include "exceptions.hpp" +#include "instance.hpp" +#include "request.hpp" #include "return_types.hpp" -#include /*! + * @headerfile mastodonpp.hpp mastodonpp/mastodonpp.hpp + * * @mainpage mastodonpp Reference * * @section using Using the library @@ -40,47 +45,39 @@ * * Or compile your code with `g++ $(pkg-config --cflags --libs mastodonpp)`. * - * @section Example + * @subsection Example * * @code - * mastodonpp::API masto("example.com", ""); + * try + * { + * mastodonpp::Instance instance{"example.com", ""}; + * mastodonpp::Request request{instance}; + * auto answer{request.get(mastodonpp::API::v1::instance)}; + * std::cout << answer << std::endl; + * } + * catch (const mastodonpp::CURLException &e) + * { + * std::cerr << e.what() << std::endl; + * } * @endcode + * + * @section exceptions Exceptions + * + * Any unrecoverable libcurl error will be thrown as a + * mastodonpp::CURLException. */ -namespace mastodonpp -{ - -using std::string; - /*! * @brief C++ wrapper for the Mastodon API. * * All text input is expected to be UTF-8. * * @since 0.1.0 - * */ - -class API +namespace mastodonpp { -public: - /*! - * @brief Construct a new API object. - * - * To register your application, leave access_token blank and call - * API::register_app1() and API::register_app2(). - * - * @param instance The hostname of your instance. - * @param access_token Your access token. - * - * @since 0.1.0 - */ - explicit API(string instance, string access_token); -private: - const string _instance; - const string _access_token; -}; + } // namespace mastodonpp diff --git a/include/request.hpp b/include/request.hpp new file mode 100644 index 0000000..c953f63 --- /dev/null +++ b/include/request.hpp @@ -0,0 +1,63 @@ +/* This file is part of mastodonpp. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef MASTODONPP_REQUEST_HPP +#define MASTODONPP_REQUEST_HPP + +#include "api.hpp" +#include "instance.hpp" +#include "return_types.hpp" + +#include + +namespace mastodonpp +{ + +using std::string; + +/*! + * @brief Used to make a request to the Mastodon API. + * + * @since 0.1.0 + * + * @headerfile request.hpp mastodonpp/request.hpp + */ +class Request +{ +public: + /*! + * @brief Construct a new Request object. + * + * @param instance An Instance with the access data. + * + * @since 0.1.0 + */ + explicit Request(Instance &instance); + + /*! + * @brief Make a HTTP GET call. + * + * @since 0.1.0 + */ + answer_type get(API::endpoint_type endpoint) const; + +private: + Instance &_instance; +}; + +} // namespace mastodonpp + +#endif // MASTODONPP_REQUEST_HPP diff --git a/include/return_types.hpp b/include/return_types.hpp index 7d09519..9f6e53b 100644 --- a/include/return_types.hpp +++ b/include/return_types.hpp @@ -30,16 +30,18 @@ using std::string; using std::string_view; /*! - * @brief Return type for API calls. + * @brief Return type for Request%s. * * @since 0.1.0 * + * @headerfile return_types.hpp mastodonpp/return_types.hpp + * * @section error Error codes * | Code | Explanation | * | --------: |:-------------------------------------------------------------| * | 0 | No error. | */ -struct answer +struct answer_type { /*! * @brief @ref error "Error code". @@ -67,25 +69,26 @@ struct answer string body; /*! - * @brief Returns true if answer::error_code is 0, false otherwise. + * @brief Returns true if #error_code is 0, false otherwise. * * @since 0.1.0 */ explicit operator bool() const; /*! - * @brief Returns answer::body as std::string_view. + * @brief Returns #body as `std::string_view`. * * @since 0.1.0 */ explicit operator string_view() const; /*! - * @brief Returns answer::body as std::ostream. + * @brief Returns #body as `std::ostream`. * * @since 0.1.0 */ - friend std::ostream &operator <<(std::ostream &out, const answer &answer); + friend std::ostream &operator <<(std::ostream &out, + const answer_type &answer); }; } // namespace mastodonpp diff --git a/pkg-config/CMakeLists.txt b/pkg-config/CMakeLists.txt new file mode 100644 index 0000000..4640f17 --- /dev/null +++ b/pkg-config/CMakeLists.txt @@ -0,0 +1,7 @@ +include(GNUInstallDirs) + +configure_file("${PROJECT_NAME}.pc.in" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" @ONLY) + +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") diff --git a/pkg-config/mastodonpp.pc.in b/pkg-config/mastodonpp.pc.in new file mode 100644 index 0000000..49a4fde --- /dev/null +++ b/pkg-config/mastodonpp.pc.in @@ -0,0 +1,12 @@ +name=@PROJECT_NAME@ +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=${prefix} +libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@ +includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ + +Name: ${name} +Description: @PROJECT_DESCRIPTION@ +Version: @PROJECT_VERSION@ +Cflags: -I${includedir} +Libs: -L${libdir} -l${name} +Requires: libcurl diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e5c64f6..e6d20ee 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,9 +1,17 @@ include(GNUInstallDirs) +find_package(CURL REQUIRED) + +# Write version in header. +configure_file ("version.hpp.in" + "${PROJECT_BINARY_DIR}/version.hpp" @ONLY) + +add_library(${PROJECT_NAME}) + file(GLOB_RECURSE sources_lib *.cpp) file(GLOB_RECURSE headers_lib ../include/*.hpp) - -add_library(${PROJECT_NAME} "${sources_lib}" "${headers_lib}") +target_sources(${PROJECT_NAME} + PRIVATE "${sources_lib}" "${headers_lib}") set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION} @@ -16,9 +24,16 @@ target_include_directories(${PROJECT_NAME} "$" "$") -# target_link_libraries(${PROJECT_NAME} -# PRIVATE -# PUBLIC) +# FindCURL provides an IMPORTED target since CMake 3.12. +if(NOT ${CMAKE_VERSION} VERSION_LESS 3.12) + target_link_libraries(${PROJECT_NAME} + PUBLIC CURL::libcurl) +else() + target_include_directories(${PROJECT_NAME} + PUBLIC ${CURL_INCLUDE_DIRS}) + target_link_libraries(${PROJECT_NAME} + PUBLIC ${CURL_LIBRARIES}) +endif() install(TARGETS ${PROJECT_NAME} diff --git a/src/api.cpp b/src/api.cpp new file mode 100644 index 0000000..30a11b9 --- /dev/null +++ b/src/api.cpp @@ -0,0 +1,43 @@ +/* This file is part of mastodonpp. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "api.hpp" + +#include +#include +#include + +namespace mastodonpp +{ + +using std::map; +using std::string_view; +using std::move; + +API::API(endpoint_type &endpoint) + : _endpoint{move(endpoint)} +{} + +string API::to_string() const +{ + static const map endpoint_map + { + {v1::instance, "/api/v1/instance"}, + {v2::search, "/api/v2/search"} + }; + return endpoint_map.at(_endpoint).data(); +} +} // namespace mastodonpp diff --git a/src/exceptions.cpp b/src/exceptions.cpp new file mode 100644 index 0000000..381b6d8 --- /dev/null +++ b/src/exceptions.cpp @@ -0,0 +1,50 @@ +/* This file is part of mastodonpp. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "exceptions.hpp" + +#include + +namespace mastodonpp +{ + +using std::to_string; +using std::move; + +CURLException::CURLException(const CURLcode &error, string message) + : error_code{error} + , _message{move(message)} +{} + +CURLException::CURLException(const CURLcode &error, string message, + string error_buffer) + : error_code{error} + , _message{move(message)} + , _error_buffer{move(error_buffer)} +{} + +const char *CURLException::what() const noexcept +{ + static string error_string{"libCURL error: " + to_string(error_code) + + " - " + _message}; + if (!_error_buffer.empty()) + { + error_string.append("[" + _error_buffer + "]"); + } + return error_string.c_str(); +} + +} // namespace mastodonpp diff --git a/src/instance.cpp b/src/instance.cpp new file mode 100644 index 0000000..6cdc956 --- /dev/null +++ b/src/instance.cpp @@ -0,0 +1,79 @@ +/* This file is part of mastodonpp. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "instance.hpp" +#include "exceptions.hpp" + +#include + +namespace mastodonpp +{ + +using std::move; + +Instance::Instance(string instance, string access_token) + : _instance{move(instance)} + , _access_token{move(access_token)} + , _connection{curl_easy_init()} +{ + setup_curl(); +} +Instance::~Instance() +{ + curl_easy_cleanup(_connection); +} + +int Instance::writer(char *data, size_t size, size_t nmemb, string *writerData) +{ + if(writerData == nullptr) + { + return 0; + } + + writerData->append(data, size*nmemb); + + return static_cast(size * nmemb); +} + +void Instance::setup_curl() +{ + if (_connection == nullptr) + { + throw CURLException{CURLE_FAILED_INIT, "Failed to initialize curl."}; + } + + CURLcode code{curl_easy_setopt(_connection, CURLOPT_ERRORBUFFER, + _curl_buffer_error)}; + if (code != CURLE_OK) + { + throw CURLException{code, "Failed to set error buffer."}; + } + + code = curl_easy_setopt(_connection, CURLOPT_WRITEFUNCTION, writer); + if (code != CURLE_OK) + { + throw CURLException{code, "Failed to set writer", _curl_buffer_error}; + } + + code = curl_easy_setopt(_connection, CURLOPT_WRITEDATA, &_curl_buffer); + if (code != CURLE_OK) + { + throw CURLException{code, "Failed to set write data", + _curl_buffer_error}; + } +} + +} // namespace mastodonpp diff --git a/src/mastodonpp.cpp b/src/mastodonpp.cpp index 7890ed6..99f2b27 100644 --- a/src/mastodonpp.cpp +++ b/src/mastodonpp.cpp @@ -16,16 +16,9 @@ #include "mastodonpp.hpp" -#include - namespace mastodonpp { -using std::move; -API::API(string instance, string access_token) - : _instance{move(instance)} - , _access_token{move(access_token)} -{} } // namespace mastodonpp diff --git a/src/request.cpp b/src/request.cpp new file mode 100644 index 0000000..e2ad4be --- /dev/null +++ b/src/request.cpp @@ -0,0 +1,33 @@ +/* This file is part of mastodonpp. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "request.hpp" + +namespace mastodonpp +{ + +Request::Request(Instance &instance) + : _instance{instance} +{} + +answer_type Request::get(API::endpoint_type endpoint) const +{ + answer_type answer; + answer.body = API{endpoint}.to_string(); + return answer; +} + +} // namespace mastodonpp diff --git a/src/return_types.cpp b/src/return_types.cpp index 6367f7c..3a7b30f 100644 --- a/src/return_types.cpp +++ b/src/return_types.cpp @@ -19,17 +19,17 @@ namespace mastodonpp { -answer::operator bool() const +answer_type::operator bool() const { return (error_code == 0); } -answer::operator string_view() const +answer_type::operator string_view() const { return body; } -std::ostream &operator <<(std::ostream &out, const answer &answer) +std::ostream &operator <<(std::ostream &out, const answer_type &answer) { out << answer.body; return out; diff --git a/src/version.hpp.in b/src/version.hpp.in new file mode 100644 index 0000000..c9ea040 --- /dev/null +++ b/src/version.hpp.in @@ -0,0 +1,15 @@ +#ifndef MASTODONPP_VERSION_HPP +#define MASTODONPP_VERSION_HPP + +#include + +namespace mastodonpp +{ + +using std::string_view; + +static constexpr string_view version = "@PROJECT_VERSION@"; + +} // namespace mastodonpp + +#endif // MASTODONPP_VERSION_HPP diff --git a/tests/test_instanciation.cpp b/tests/test_instanciation.cpp index 3fbc0f3..3bd73ec 100644 --- a/tests/test_instanciation.cpp +++ b/tests/test_instanciation.cpp @@ -14,7 +14,8 @@ * along with this program. If not, see . */ -#include "mastodonpp.hpp" +#include "instance.hpp" +#include "request.hpp" #include @@ -26,15 +27,33 @@ namespace mastodonpp using std::string; -SCENARIO ("API can be instantiated.") +SCENARIO ("Instantiations.") { bool exception = false; - GIVEN ("One instanciation.") + WHEN ("Instance is instantiated.") { try { - API masto("example.com", ""); + Instance instance{"example.com", ""}; + } + catch (const std::exception &e) + { + exception = true; + } + + THEN ("No exception is thrown") + { + REQUIRE_FALSE(exception); + } + } + + WHEN ("Request is instantiated.") + { + try + { + Instance instance{"example.com", ""}; + Request request{instance}; } catch (const std::exception &e) {