C++ wrapper for the Mastodon API.
This repository has been archived on 2020-05-10. You can view files and clone it, but cannot push or open issues or pull requests.
Go to file
tastytea 5ddc0f20fe
the build was successful Details
Added dependency-install examples for Debian Stretch and CentOS 7.
2019-04-22 04:01:18 +02:00
examples Added comment to example02. 2019-04-10 20:55:23 +02:00
src Fixed operator << warnings. 2019-04-21 13:59:31 +02:00
tests Added tests for API::v1::suggestions_accountid. 2019-04-20 00:50:28 +02:00
.drone.yml Added the variable EXTRA_TEST_ARGS to cmake. 2019-04-16 18:40:35 +02:00
.gitignore Updated .gitignore, deleted build.sh. 2019-03-30 22:35:15 +01:00
CMakeLists.txt Added dependency-install examples for Debian Stretch and CentOS 7. 2019-04-22 04:01:18 +02:00
CONTRIBUTING.md Updated fediverse address. 2019-03-30 22:32:24 +01:00
Doxyfile fixed doxygen recipe 2018-04-19 01:22:39 +02:00
ISSUE_TEMPLATE.md reduced issue template 2018-06-14 11:52:56 +02:00
LICENSE initial commit 2018-01-09 12:36:05 +01:00
README.md Added dependency-install examples for Debian Stretch and CentOS 7. 2019-04-22 04:01:18 +02:00
build_doc.sh Parameter support, added documentation. 2018-01-13 01:41:30 +01:00
packages.CMakeLists.txt Changed CMAKE_PROJECT_NAME to PROJECT_NAME. 2019-04-14 04:38:59 +02:00
tests.CMakeLists.txt Added the variable EXTRA_TEST_ARGS to cmake. 2019-04-16 18:40:35 +02:00

README.md

mastodon-cpp is a C++ wrapper for the Mastodon API. You submit an API call and get the raw JSON or easy to use abstractions.

The ABI will be unstable in versions < 1.0.0

Subscribe to mastodon-cpp releases.

Usage

The HTML reference can be generated with build_doc.sh, if doxygen is installed. It is also available at doc.schlomp.space/mastodon-cpp/. There are examples in examples/.

Most basic example

#include <iostream>
#include <mastodon-cpp/mastodon-cpp.hpp>

int main()
{
    Mastodon::API masto("social.example.com", "auth_token");
    std::cout << masto.get(Mastodon::API::v1::accounts_verify_credentials);
    std::cout << std::endl;
}

Another simple example

Using the Easy interface.

#include <iostream>
#include <string>
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/all.hpp>

using Mastodon;

int main()
{
    Easy::API masto("social.example", "");
    return_call ret = masto.get(API::v1::timelines_public);

    for (const std::string &str : Easy::json_array_to_vector(ret.answer))
    {
        Easy::Status status(str);
        std::cout << "  " << status.account().acct() << " wrote:\n";
        std::cout << status.content() << '\n';
    }
}

Compiling your project

A project consisting of one file can be compiled as follows:

g++ -std=c++14 -lmastodon-cpp example.cpp

List of types

Learn more at https://doc.schlomp.space/mastodon-cpp/namespaceMastodon.html and https://doc.schlomp.space/mastodon-cpp/namespaceMastodon_1_1Easy.html.

Return types

  • Mastodon::return_call: Contains the response from Mastodon::API calls.
  • Mastodon::Easy::return_entity: Contains the response from Mastodon::Easy::API calls that return a single Mastodon::Easy::Entity.
  • Mastodon::Easy::return_entities_vector: Contains the response from Mastodon::Easy::API calls that return multiple Mastodon::Easy::Entity.

Other types

  • Mastodon::param: A single parameter to an Mastodon::API call. Normally you don't need this.
  • Mastodon::parameters: All parameters to an Mastodon::API call.
  • Mastodon::http_method: HTTP method of an Mastodon::API call.
  • Mastodon::Easy::event_type: Event types returned in streams.
  • Mastodon::Easy::visibility_type: Describes the visibility of a post.
  • Mastodon::Easy::attachment_type: Describes the type of attachment.
  • Mastodon::Easy::card_type: Describes the type of card.
  • Mastodon::Easy::notification_type: The type of the notification.
  • Mastodon::Easy::context_type: Describes the context of a filter.
  • Mastodon::Easy::stream_event: Type and data of an events returned in streams.
  • Mastodon::Easy::alert_type: Type for a single alert.
  • Mastodon::Easy::alerts: Vector of Mastodon::Easy::alert_type, used for push subscriptions.
  • Mastodon::Easy::time: Type for time, can be converted to time_point and string.

Error codes

Code Explanation
0 No error
22 Invalid argument
78 URL changed (HTTP 301 or 308)
110 Connection timed out
111 Connection refused (check http_error_code)
113 No route to host / Could not resolve host
192 curlpp runtime error
193 curlpp logic error
255 Unknown error

If you use a debug build, you get more verbose error messages.

Install

Upgrading from versions below 0.100.0

Starting with version 0.100.0, large parts of the library have been rewritten. Upgrading from previous versions will require extensive code changes. You can keep using the old version, it is archived in the branch pre-0.100.0. It will receive bug-fixes for a while but no new features.

Packages

Every release includes packages for Debian and Centos. Gentoo packages are available in my overlay.

Gentoo

Add my repository and install it from there.

eselect repository enable tastytea
echo 'dev-cpp/mastodon-cpp ~amd64' >> /etc/portage/package.accept_keywords/mastodon-cpp
emaint sync -r tastytea
emerge -a dev-cpp/mastodon-cpp

DEB and RPM

Prebuilt DEB and RPM packages for x86_64(amd64) are provided with each release. .deb packages are built on Debian stretch and .rpm packages are built on CentOS 7. These packages are automatically built and not tested.

To use the .deb package on Debian stretch, you will need libcurlpp0 from sid.

To use the .rpm package on CentOS 7, you will need libcurlpp from EPEL 6.

From source

Dependencies

  • Tested OS: Linux
  • C++ compiler (tested: gcc 6/7/8, clang 5/6)
  • cmake (at least: 3.6)
  • pkgconfig (tested: 0.29 / 0.27)
  • curlpp (tested: 0.8)
  • Optional
    • Easy interface & Examples: jsoncpp (tested: 1.8 / 1.7)
    • Documentation: doxygen (tested: 1.8)
    • DEB package: dpkg (tested: 1.18)
    • RPM package: rpm-build (tested: 4.11)
    • Tests: catch (tested: 2.5 / 1.2)

Debian stretch

echo "APT::Default-Release \"stretch\";" >> /etc/apt/apt.conf.d/00default_release
echo "deb http://deb.debian.org/debian sid main" >> /etc/apt/sources.list.d/sid.list
apt-get update
apt-get install build-essential cmake pkg-config libcurl4-openssl-dev libjsoncpp-dev doxygen file
apt-get install -t sid libcurlpp-dev

Centos 7

wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -i epel-release-latest-7.noarch.rpm
yum install centos-release-scl
yum install devtoolset-6
scl enable devtoolset-6 bash
yum install libcurl-devel doxygen rpm-build
yum --enablerepo=epel install cmake3 jsoncpp-devel
wget https://download.fedoraproject.org/pub/epel/6/x86_64/Packages/c/curlpp-devel-0.7.3-5.el6.x86_64.rpm
wget https://download.fedoraproject.org/pub/epel/6/x86_64/Packages/c/curlpp-0.7.3-5.el6.x86_64.rpm
yum localinstall curlpp-devel-0.7.3-5.el6.x86_64.rpm curlpp-0.7.3-5.el6.x86_64.rpm

Get sourcecode

Release

Download the current release at schlomp.space.

Development version

git clone https://schlomp.space/tastytea/mastodon-cpp.git

Compile

mkdir build
cd build/
cmake ..
cmake --build . -- -j$(nproc --ignore=1)

cmake options:

  • -DCMAKE_BUILD_TYPE=Debug for a debug build
  • -DWITH_EASY=NO to not build the Easy abstractions and to get rid of the jsoncpp-dependency (not recommended)
  • -DWITH_EXAMPLES=YES if you want to compile the examples
  • -DWITH_TESTS=YES if you want to compile the tests
  • -DEXTRA_TEST_ARGS to run only some tests
    • Possible values: [api], [mastodon], [glitch-soc], [pleroma]
    • Example: -DEXTRA_TEST_ARGS=[pleroma]![mastodon] to run the tests for features in Pleroma that are not in Mastodon.
  • -DWITH_DOC=NO if you don't want to compile the HTML reference
  • 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

Install with make install.

Tests

You can run the tests with ctest inside the build directory. You need to set the environment variable MASTODON_CPP_ACCESS_TOKEN to an access token with the scopes read, write and follow for some tests. You can select the instance to use with MASTODON_CPP_INSTANCE, the default is likeable.space. You can select the user ID with MASTODON_CPP_USER_ID, the default is 9hnrrVPriLiLVAhfVo. You can select the status ID with MASTODON_CPP_STATUS_ID, the default is 9hwnuJMq3eTdO4s1PU. You can select the filter ID with MASTODON_CPP_FILTER_ID.

Requirements for the test-user:

  • Have at least 1 follower.
  • Follow at least 1 account.
  • Have at least 1 account endorsed.
  • Have at least 1 public or unlisted status.
  • Have at least 1 post favourited.
  • Have no follow requests.

Packages

DEB and RPM

Compile with -DWITH_DEB=ON or -DWITH_RPM=ON. Run make package from the build directory to generate a DEB/RPM package.

Other

Run make package from the build directory to generate a tar.gz archive.

Status of implementation

Feature complete as of Mastodon 2.6.1

  • GET /api/v1/accounts/:id
  • POST /api/v1/accounts
  • GET /api/v1/accounts/verify_credentials
  • PATCH /api/v1/accounts/update_credentials
  • GET /api/v1/accounts/:id/followers
  • GET /api/v1/accounts/:id/following
  • GET /api/v1/accounts/:id/statuses
  • POST /api/v1/accounts/:id/follow
  • POST /api/v1/accounts/:id/unfollow
  • GET /api/v1/accounts/relationships
  • GET /api/v1/accounts/search
  • POST /api/v1/apps
  • GET /api/v1/apps/verify_credentials
  • GET /api/v1/blocks
  • POST /api/v1/accounts/:id/block
  • POST /api/v1/accounts/:id/unblock
  • GET /api/v1/custom_emojis
  • GET /api/v1/domain_blocks
  • POST /api/v1/domain_blocks
  • DELETE /api/v1/domain_blocks
  • GET /api/v1/endorsements
  • POST /api/v1/accounts/:id/pin
  • POST /api/v1/accounts/:id/unpin
  • GET /api/v1/favourites
  • POST /api/v1/statuses/:id/favourite
  • POST /api/v1/statuses/:id/unfavourite
  • GET /api/v1/filters
  • POST /api/v1/filters
  • GET /api/v1/filters/:id
  • PUT /api/v1/filters/:id
  • DELETE /api/v1/filters/:id
  • GET /api/v1/follow_requests
  • POST /api/v1/follow_requests/:id/authorize
  • POST /api/v1/follow_requests/:id/reject
  • GET /api/v1/suggestions
  • DELETE /api/v1/suggestions/:account_id
  • GET /api/v1/instance
  • GET /api/v1/lists
  • GET /api/v1/accounts/:id/lists
  • GET /api/v1/lists/:id/accounts
  • GET /api/v1/lists/:id
  • POST /api/v1/lists
  • PUT /api/v1/lists/:id
  • DELETE /api/v1/lists/:id
  • POST /api/v1/lists/:id/accounts
  • DELETE /api/v1/lists/:id/accounts
  • POST /api/v1/media
  • PUT /api/v1/media/:id
  • GET /api/v1/mutes
  • POST /api/v1/accounts/:id/mute
  • POST /api/v1/accounts/:id/unmute
  • POST /api/v1/statuses/:id/mute
  • POST /api/v1/statuses/:id/unmute
  • GET /api/v1/notifications
  • GET /api/v1/notifications/:id
  • POST /api/v1/notifications/clear
  • POST /api/v1/notifications/dismiss
  • POST /api/v1/push/subscription
  • GET /api/v1/push/subscription
  • PUT /api/v1/push/subscription
  • DELETE /api/v1/push/subscription
  • GET /api/v1/reports (Deprecated)
  • POST /api/v1/reports
  • GET /api/v1/scheduled_statuses
  • GET /api/v1/scheduled_statuses/:id
  • PUT /api/v1/scheduled_statuses/:id
  • DELETE /api/v1/scheduled_statuses/:id
  • GET /api/v1/search (Deprecated)
  • GET /api/v2/search
  • GET /api/v1/statuses/:id
  • GET /api/v1/statuses/:id/context
  • GET /api/v1/statuses/:id/card
  • GET /api/v1/statuses/:id/reblogged_by
  • GET /api/v1/statuses/:id/favourited_by
  • POST /api/v1/statuses
  • DELETE /api/v1/statuses/:id
  • POST /api/v1/statuses/:id/reblog
  • POST /api/v1/statuses/:id/unreblog
  • POST /api/v1/statuses/:id/pin
  • POST /api/v1/statuses/:id/unpin
  • GET /api/v1/timelines/home
  • GET /api/v1/conversations
  • GET /api/v1/timelines/public
  • GET /api/v1/timelines/tag/:hashtag
  • GET /api/v1/timelines/list/:list_id
  • GET /api/v1/streaming/user
  • GET /api/v1/streaming/public
  • GET /api/v1/streaming/public/local
  • GET /api/v1/streaming/hashtag
  • GET /api/v1/streaming/hashtag/local
  • GET /api/v1/streaming/list
  • GET /api/v1/streaming/direct

Glitch-Soc support

  • max_toot_chars in /api/v1/instance
  • GET /api/v1/bookmarks
  • POST /api/v1/statuses/:id/bookmark
  • POST /api/v1/statuses/:id/unbookmark

Copyright

Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>.
License GPLv3: GNU GPL version 3 <https://www.gnu.org/licenses/gpl-3.0.html>.
This program comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.