Merge branch 'develop' into main
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
commit
c2a33f6619
@ -26,7 +26,7 @@ steps:
|
|||||||
- alias apt-get='rm -f /var/cache/apt/archives/lock && apt-get'
|
- alias apt-get='rm -f /var/cache/apt/archives/lock && apt-get'
|
||||||
- apt-get update -q
|
- apt-get update -q
|
||||||
- apt-get install -qq build-essential cmake
|
- 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
|
- rm -rf build && mkdir -p build && cd build
|
||||||
- cmake -G "Unix Makefiles" -DWITH_TESTS=YES ..
|
- cmake -G "Unix Makefiles" -DWITH_TESTS=YES ..
|
||||||
- make VERBOSE=1
|
- make VERBOSE=1
|
||||||
@ -49,7 +49,7 @@ steps:
|
|||||||
- alias apt-get='rm -f /var/cache/apt/archives/lock && apt-get'
|
- alias apt-get='rm -f /var/cache/apt/archives/lock && apt-get'
|
||||||
- apt-get update -q
|
- apt-get update -q
|
||||||
- apt-get install -qq build-essential cmake
|
- 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
|
- rm -rf build && mkdir -p build && cd build
|
||||||
- cmake -G "Unix Makefiles" -DWITH_TESTS=YES ..
|
- cmake -G "Unix Makefiles" -DWITH_TESTS=YES ..
|
||||||
- make VERBOSE=1
|
- make VERBOSE=1
|
||||||
|
@ -19,7 +19,6 @@ list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
|
|||||||
|
|
||||||
# Project build options.
|
# Project build options.
|
||||||
option(WITH_TESTS "Compile tests." NO)
|
option(WITH_TESTS "Compile tests." NO)
|
||||||
# option(WITH_DOC "Generate HTML documentation." YES)
|
|
||||||
option(WITH_EXAMPLES "Compile examples." NO)
|
option(WITH_EXAMPLES "Compile examples." NO)
|
||||||
# option(WITH_DEB "Prepare for the building of .deb packages." 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_RPM "Prepare for the building of .rpm packages." NO)
|
||||||
@ -31,6 +30,10 @@ set(CMAKE_CXX_EXTENSIONS OFF)
|
|||||||
include(debug_flags)
|
include(debug_flags)
|
||||||
|
|
||||||
add_subdirectory(src)
|
add_subdirectory(src)
|
||||||
|
add_subdirectory(include)
|
||||||
|
|
||||||
|
add_subdirectory(cmake)
|
||||||
|
add_subdirectory(pkg-config)
|
||||||
|
|
||||||
if(WITH_TESTS)
|
if(WITH_TESTS)
|
||||||
add_subdirectory(tests)
|
add_subdirectory(tests)
|
||||||
|
68
README.adoc
68
README.adoc
@ -4,8 +4,76 @@
|
|||||||
:uri-base: https://schlomp.space/tastytea/{project}
|
:uri-base: https://schlomp.space/tastytea/{project}
|
||||||
:uri-branch-main: {uri-base}/src/branch/main
|
:uri-branch-main: {uri-base}/src/branch/main
|
||||||
:uri-mastodon-cpp: https://schlomp.space/tastytea/mastodon-cpp
|
: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
|
*{project}* is a C++ wrapper for the Mastodon API. It replaces
|
||||||
link:{uri-mastodon-cpp}[mastodon-cpp].
|
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[]
|
include::{uri-base}/raw/branch/main/CONTRIBUTING.adoc[]
|
||||||
|
19
cmake/CMakeLists.txt
Normal file
19
cmake/CMakeLists.txt
Normal file
@ -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}")
|
5
cmake/mastodonppConfig.cmake.in
Normal file
5
cmake/mastodonppConfig.cmake.in
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
include(CMakeFindDependencyMacro)
|
||||||
|
|
||||||
|
find_dependency(CURL REQUIRED)
|
||||||
|
|
||||||
|
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
|
6
include/CMakeLists.txt
Normal file
6
include/CMakeLists.txt
Normal file
@ -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")
|
94
include/api.hpp
Normal file
94
include/api.hpp
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
/* This file is part of mastodonpp.
|
||||||
|
* Copyright © 2020 tastytea <tastytea@tastytea.de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MASTODONPP_API_HPP
|
||||||
|
#define MASTODONPP_API_HPP
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
#include <variant>
|
||||||
|
|
||||||
|
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<v1,v2>;
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @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
|
82
include/exceptions.hpp
Normal file
82
include/exceptions.hpp
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
/* This file is part of mastodonpp.
|
||||||
|
* Copyright © 2020 tastytea <tastytea@tastytea.de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MASTODONPP_EXCEPTIONS_HPP
|
||||||
|
#define MASTODONPP_EXCEPTIONS_HPP
|
||||||
|
|
||||||
|
#include <curl/curl.h>
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <exception>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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
|
76
include/instance.hpp
Normal file
76
include/instance.hpp
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
/* This file is part of mastodonpp.
|
||||||
|
* Copyright © 2020 tastytea <tastytea@tastytea.de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MASTODONPP_INSTANCE_HPP
|
||||||
|
#define MASTODONPP_INSTANCE_HPP
|
||||||
|
|
||||||
|
#include <curl/curl.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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
|
@ -17,10 +17,15 @@
|
|||||||
#ifndef MASTODONPP_HPP
|
#ifndef MASTODONPP_HPP
|
||||||
#define MASTODONPP_HPP
|
#define MASTODONPP_HPP
|
||||||
|
|
||||||
|
#include "api.hpp"
|
||||||
|
#include "exceptions.hpp"
|
||||||
|
#include "instance.hpp"
|
||||||
|
#include "request.hpp"
|
||||||
#include "return_types.hpp"
|
#include "return_types.hpp"
|
||||||
#include <string>
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
* @headerfile mastodonpp.hpp mastodonpp/mastodonpp.hpp
|
||||||
|
*
|
||||||
* @mainpage mastodonpp Reference
|
* @mainpage mastodonpp Reference
|
||||||
*
|
*
|
||||||
* @section using Using the library
|
* @section using Using the library
|
||||||
@ -40,47 +45,39 @@
|
|||||||
*
|
*
|
||||||
* Or compile your code with `g++ $(pkg-config --cflags --libs mastodonpp)`.
|
* Or compile your code with `g++ $(pkg-config --cflags --libs mastodonpp)`.
|
||||||
*
|
*
|
||||||
* @section Example
|
* @subsection Example
|
||||||
*
|
*
|
||||||
* @code
|
* @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
|
* @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.
|
* @brief C++ wrapper for the Mastodon API.
|
||||||
*
|
*
|
||||||
* All text input is expected to be UTF-8.
|
* All text input is expected to be UTF-8.
|
||||||
*
|
*
|
||||||
* @since 0.1.0
|
* @since 0.1.0
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
namespace mastodonpp
|
||||||
class API
|
|
||||||
{
|
{
|
||||||
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
|
} // namespace mastodonpp
|
||||||
|
|
||||||
|
63
include/request.hpp
Normal file
63
include/request.hpp
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
/* This file is part of mastodonpp.
|
||||||
|
* Copyright © 2020 tastytea <tastytea@tastytea.de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MASTODONPP_REQUEST_HPP
|
||||||
|
#define MASTODONPP_REQUEST_HPP
|
||||||
|
|
||||||
|
#include "api.hpp"
|
||||||
|
#include "instance.hpp"
|
||||||
|
#include "return_types.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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
|
@ -30,16 +30,18 @@ using std::string;
|
|||||||
using std::string_view;
|
using std::string_view;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @brief Return type for API calls.
|
* @brief Return type for Request%s.
|
||||||
*
|
*
|
||||||
* @since 0.1.0
|
* @since 0.1.0
|
||||||
*
|
*
|
||||||
|
* @headerfile return_types.hpp mastodonpp/return_types.hpp
|
||||||
|
*
|
||||||
* @section error Error codes
|
* @section error Error codes
|
||||||
* | Code | Explanation |
|
* | Code | Explanation |
|
||||||
* | --------: |:-------------------------------------------------------------|
|
* | --------: |:-------------------------------------------------------------|
|
||||||
* | 0 | No error. |
|
* | 0 | No error. |
|
||||||
*/
|
*/
|
||||||
struct answer
|
struct answer_type
|
||||||
{
|
{
|
||||||
/*!
|
/*!
|
||||||
* @brief @ref error "Error code".
|
* @brief @ref error "Error code".
|
||||||
@ -67,25 +69,26 @@ struct answer
|
|||||||
string body;
|
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
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
explicit operator bool() const;
|
explicit operator bool() const;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @brief Returns answer::body as std::string_view.
|
* @brief Returns #body as `std::string_view`.
|
||||||
*
|
*
|
||||||
* @since 0.1.0
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
explicit operator string_view() const;
|
explicit operator string_view() const;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @brief Returns answer::body as std::ostream.
|
* @brief Returns #body as `std::ostream`.
|
||||||
*
|
*
|
||||||
* @since 0.1.0
|
* @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
|
} // namespace mastodonpp
|
||||||
|
7
pkg-config/CMakeLists.txt
Normal file
7
pkg-config/CMakeLists.txt
Normal file
@ -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")
|
12
pkg-config/mastodonpp.pc.in
Normal file
12
pkg-config/mastodonpp.pc.in
Normal file
@ -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
|
@ -1,9 +1,17 @@
|
|||||||
include(GNUInstallDirs)
|
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 sources_lib *.cpp)
|
||||||
file(GLOB_RECURSE headers_lib ../include/*.hpp)
|
file(GLOB_RECURSE headers_lib ../include/*.hpp)
|
||||||
|
target_sources(${PROJECT_NAME}
|
||||||
add_library(${PROJECT_NAME} "${sources_lib}" "${headers_lib}")
|
PRIVATE "${sources_lib}" "${headers_lib}")
|
||||||
|
|
||||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||||
VERSION ${PROJECT_VERSION}
|
VERSION ${PROJECT_VERSION}
|
||||||
@ -16,9 +24,16 @@ target_include_directories(${PROJECT_NAME}
|
|||||||
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
|
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
|
||||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
|
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
|
||||||
|
|
||||||
# target_link_libraries(${PROJECT_NAME}
|
# FindCURL provides an IMPORTED target since CMake 3.12.
|
||||||
# PRIVATE
|
if(NOT ${CMAKE_VERSION} VERSION_LESS 3.12)
|
||||||
# PUBLIC)
|
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}
|
install(TARGETS ${PROJECT_NAME}
|
||||||
|
43
src/api.cpp
Normal file
43
src/api.cpp
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/* This file is part of mastodonpp.
|
||||||
|
* Copyright © 2020 tastytea <tastytea@tastytea.de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "api.hpp"
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <string_view>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
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_type,string_view> endpoint_map
|
||||||
|
{
|
||||||
|
{v1::instance, "/api/v1/instance"},
|
||||||
|
{v2::search, "/api/v2/search"}
|
||||||
|
};
|
||||||
|
return endpoint_map.at(_endpoint).data();
|
||||||
|
}
|
||||||
|
} // namespace mastodonpp
|
50
src/exceptions.cpp
Normal file
50
src/exceptions.cpp
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
/* This file is part of mastodonpp.
|
||||||
|
* Copyright © 2020 tastytea <tastytea@tastytea.de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "exceptions.hpp"
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
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
|
79
src/instance.cpp
Normal file
79
src/instance.cpp
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
/* This file is part of mastodonpp.
|
||||||
|
* Copyright © 2020 tastytea <tastytea@tastytea.de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "instance.hpp"
|
||||||
|
#include "exceptions.hpp"
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
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<int>(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
|
@ -16,16 +16,9 @@
|
|||||||
|
|
||||||
#include "mastodonpp.hpp"
|
#include "mastodonpp.hpp"
|
||||||
|
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
namespace mastodonpp
|
namespace mastodonpp
|
||||||
{
|
{
|
||||||
|
|
||||||
using std::move;
|
|
||||||
|
|
||||||
API::API(string instance, string access_token)
|
|
||||||
: _instance{move(instance)}
|
|
||||||
, _access_token{move(access_token)}
|
|
||||||
{}
|
|
||||||
|
|
||||||
} // namespace mastodonpp
|
} // namespace mastodonpp
|
||||||
|
33
src/request.cpp
Normal file
33
src/request.cpp
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/* This file is part of mastodonpp.
|
||||||
|
* Copyright © 2020 tastytea <tastytea@tastytea.de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#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
|
@ -19,17 +19,17 @@
|
|||||||
namespace mastodonpp
|
namespace mastodonpp
|
||||||
{
|
{
|
||||||
|
|
||||||
answer::operator bool() const
|
answer_type::operator bool() const
|
||||||
{
|
{
|
||||||
return (error_code == 0);
|
return (error_code == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
answer::operator string_view() const
|
answer_type::operator string_view() const
|
||||||
{
|
{
|
||||||
return body;
|
return body;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream &operator <<(std::ostream &out, const answer &answer)
|
std::ostream &operator <<(std::ostream &out, const answer_type &answer)
|
||||||
{
|
{
|
||||||
out << answer.body;
|
out << answer.body;
|
||||||
return out;
|
return out;
|
||||||
|
15
src/version.hpp.in
Normal file
15
src/version.hpp.in
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#ifndef MASTODONPP_VERSION_HPP
|
||||||
|
#define MASTODONPP_VERSION_HPP
|
||||||
|
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
namespace mastodonpp
|
||||||
|
{
|
||||||
|
|
||||||
|
using std::string_view;
|
||||||
|
|
||||||
|
static constexpr string_view version = "@PROJECT_VERSION@";
|
||||||
|
|
||||||
|
} // namespace mastodonpp
|
||||||
|
|
||||||
|
#endif // MASTODONPP_VERSION_HPP
|
@ -14,7 +14,8 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "mastodonpp.hpp"
|
#include "instance.hpp"
|
||||||
|
#include "request.hpp"
|
||||||
|
|
||||||
#include <catch.hpp>
|
#include <catch.hpp>
|
||||||
|
|
||||||
@ -26,15 +27,33 @@ namespace mastodonpp
|
|||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
SCENARIO ("API can be instantiated.")
|
SCENARIO ("Instantiations.")
|
||||||
{
|
{
|
||||||
bool exception = false;
|
bool exception = false;
|
||||||
|
|
||||||
GIVEN ("One instanciation.")
|
WHEN ("Instance is instantiated.")
|
||||||
{
|
{
|
||||||
try
|
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)
|
catch (const std::exception &e)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user