mastodon-cpp  0.101.1
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
Mastodon::API Class Reference

Interface to the Mastodon API. More...

#include <mastodon-cpp.hpp>

Inheritance diagram for Mastodon::API:
Mastodon::Easy::API

Classes

class  http
 http class. Do not use this directly. More...
 

Public Types

enum  v1 {
  accounts_id, accounts_verify_credentials, accounts_update_credentials, accounts_id_followers,
  accounts_id_following, accounts_id_statuses, accounts_id_follow, accounts_id_unfollow,
  accounts_relationships, accounts_search, apps, blocks,
  accounts_id_block, accounts_id_unblock, custom_emojis, domain_blocks,
  endorsements, accounts_id_pin, accounts_id_unpin, favourites,
  statuses_id_favourite, statuses_id_unfavourite, follow_requests, follow_requests_id_authorize,
  follow_requests_id_reject, instance, lists, accounts_id_lists,
  lists_id, lists_id_accounts, media, media_id,
  mutes, accounts_id_mute, accounts_id_unmute, statuses_id_mute,
  statuses_id_unmute, notifications, notifications_id, notifications_clear,
  notifications_dismiss, push_subscription, reports, search,
  statuses, statuses_id, statuses_id_context, statuses_id_card,
  statuses_id_reblogged_by, statuses_id_favourited_by, statuses_id_reblog, statuses_id_unreblog,
  statuses_id_pin, statuses_id_unpin, timelines_home, timelines_public,
  timelines_tag_hashtag, timelines_list_list_id, streaming_user, streaming_public,
  streaming_public_local, streaming_hashtag, streaming_list, bookmarks,
  statuses_id_bookmark, statuses_id_unbookmark
}
 A list of all v1 API calls. More...
 
enum  v2 { search }
 A list of all v2 API calls. More...
 

Public Member Functions

 API (const string &instance, const string &access_token)
 Constructs a new API object. More...
 
virtual ~API ()
 Destroys the object. More...
 
void set_useragent (const string &useragent)
 Sets the useragent. Default is mastodon-cpp/version. More...
 
const string get_useragent () const
 Gets the useragent. More...
 
const string get_instance () const
 Returns the instance. More...
 
return_call register_app1 (const string &client_name, const string &redirect_uri, const string &scopes, const string &website, string &client_id, string &client_secret, string &url)
 Register application, step 1/2. More...
 
return_call register_app2 (const string &client_id, const string &client_secret, const string &redirect_uri, const string &code, string &access_token)
 Register application, step 2/2. More...
 
const string get_header (const string &header) const
 Gets the header from the last answer. More...
 
bool exceptions (const bool &value)
 Turn exceptions on or off. Defaults to off. More...
 
bool exceptions () const
 Returns true if exceptions are turned on, false otherwise.
 
void set_proxy (const string &proxy, const string &userpw="")
 Sets the proxy. More...
 
void get_proxy (string &proxy, string &userpw) const
 For internal use. More...
 
const return_call get (const Mastodon::API::v1 &call)
 Make a GET request which doesn't require parameters. More...
 
const return_call get (const Mastodon::API::v1 &call, const parameters &parameters)
 Make a GET request which requires parameters. More...
 
const return_call get (const Mastodon::API::v2 &call, const parameters &parameters)
 Make a GET request which requires parameters. More...
 
const return_call get (const string &call)
 Make a custom GET request. More...
 
void get_stream (const Mastodon::API::v1 &call, const parameters &parameters, std::unique_ptr< Mastodon::API::http > &ptr, string &stream)
 Make a streaming GET request. More...
 
void get_stream (const Mastodon::API::v1 &call, std::unique_ptr< Mastodon::API::http > &ptr, string &stream)
 Make a streaming GET request. More...
 
void get_stream (const string &call, std::unique_ptr< Mastodon::API::http > &ptr, string &stream)
 Make a streaming GET request. More...
 
return_call patch (const Mastodon::API::v1 &call, const parameters &parameters)
 Make a PATCH request. More...
 
return_call post (const Mastodon::API::v1 &call)
 Make a POST request which doesn't require parameters. More...
 
return_call post (const Mastodon::API::v1 &call, const parameters &parameters)
 Make a POST request which requires parameters. More...
 
return_call post (const string &call, const parameters &parameters)
 Make a custom POST request. More...
 
return_call put (const Mastodon::API::v1 &call, const parameters &parameters)
 Make a PUT request which requires a parameters. More...
 
return_call put (const string &call, const parameters &parameters)
 Make a custom PUT request. More...
 
return_call del (const Mastodon::API::v1 &call, const parameters &parameters)
 Make a DELETE request which requires parameters. More...
 
return_call del (const string &call, const parameters &parameters)
 Make a custom DELETE request. More...
 

Static Public Member Functions

static const string urlencode (const string &str)
 Percent-encodes a string. This is done automatically, unless you make a custom request. More...
 
static const string urldecode (const string &str)
 Decodes a percent-encoded string. More...
 
static const string unescape_html (const string &html)
 Replaces HTML entities with UTF-8 characters. More...
 

Detailed Description

Interface to the Mastodon API.

All input is expected to be UTF-8. Binary data must be base64-encoded or a filename. It appears that media attachements can not be base64 encoded.

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
Since
before 0.11.0

Member Enumeration Documentation

◆ v1

enum Mastodon::API::v1
strong

A list of all v1 API calls.

The original / are substituted by _.

Since
before 0.11.0
179  {
180  // Mastodon
181  accounts_id,
182  // accounts,
183  accounts_verify_credentials,
184  accounts_update_credentials,
185  accounts_id_followers,
186  accounts_id_following,
187  accounts_id_statuses,
188  accounts_id_follow,
189  accounts_id_unfollow,
190  accounts_relationships,
191  accounts_search,
192 
193  apps,
194  // apps_verify_credentials,
195 
196  blocks,
197  accounts_id_block,
198  accounts_id_unblock,
199 
200  custom_emojis,
201 
202  domain_blocks,
203 
204  endorsements,
205  accounts_id_pin,
206  accounts_id_unpin,
207 
208  favourites,
209  statuses_id_favourite,
210  statuses_id_unfavourite,
211 
212  // filters,
213  // filters_id,
214 
215  follow_requests,
216  follow_requests_id_authorize,
217  follow_requests_id_reject,
218 
219  // suggestions,
220  // suggestions_id,
221 
222  instance,
223 
224  lists,
225  accounts_id_lists,
226  lists_id,
227  lists_id_accounts,
228 
229  media,
230  media_id,
231 
232  mutes,
233  accounts_id_mute,
234  accounts_id_unmute,
235  statuses_id_mute,
236  statuses_id_unmute,
237 
238  notifications,
239  notifications_id,
240  notifications_clear,
241  notifications_dismiss,
242  push_subscription,
243 
244  reports,
245 
246  // scheduled_statuses,
247  // scheduled_statuses_id,
248 
249  search,
250 
251  statuses,
252  statuses_id,
253  statuses_id_context,
254  statuses_id_card,
255  statuses_id_reblogged_by,
256  statuses_id_favourited_by,
257  statuses_id_reblog,
258  statuses_id_unreblog,
259  statuses_id_pin,
260  statuses_id_unpin,
261 
262  timelines_home,
263  // timelines_conversations,
264  timelines_public,
265  timelines_tag_hashtag,
266  timelines_list_list_id,
267 
268  streaming_user,
269  streaming_public,
270  streaming_public_local,
271  streaming_hashtag,
272  // streaming_hashtag_local,
273  streaming_list,
274  // streaming_direct,
275 
276  // Glitch-Soc
277  bookmarks,
278  statuses_id_bookmark,
279  statuses_id_unbookmark
280  };

◆ v2

enum Mastodon::API::v2
strong

A list of all v2 API calls.

The original / are substituted by _.

Since
0.16.0
290  {
291  search
292  };

Constructor & Destructor Documentation

◆ API()

API::API ( const string &  instance,
const string &  access_token 
)
explicit

Constructs a new API object.

To register your application, leave access_token blank and call register_app1() and register_app2().

Parameters
instanceThe hostname of your instance
access_tokenYour access token.
Since
before 0.11.0
31 : _instance(instance)
32 , _access_token(access_token)
33 , _useragent(string("mastodon-cpp/") + global::version)
34 , _http(*this, instance, access_token)
35 , _exceptions(false)
36 , _proxy("")
37 , _proxy_userpw("")
38 {}

◆ ~API()

API::~API ( )
virtual

Destroys the object.

Since
0.100.0
41 {}

Member Function Documentation

◆ del() [1/2]

return_call API::del ( const Mastodon::API::v1 call,
const parameters parameters 
)

Make a DELETE request which requires parameters.

Parameters
callA call defined in Mastodon::API::v1
parametersA Mastodon::parametermap containing parameters
Since
0.100.0
25 {
26  string strcall = "";
27  string strid = "";
28 
29  // The ID is part of the path
30  auto &it = params.find("id");
31  if (it != params.end())
32  {
33  strid = it->values[0];
34  }
35 
36  switch (call)
37  {
38  case v1::domain_blocks:
39  strcall = "/api/v1/domain_blocks";
40  break;
41  case v1::lists_id:
42  strcall = "/api/v1/lists/" + strid;
43  break;
44  case v1::lists_id_accounts:
45  strcall = "/api/v1/lists/" + strid + "/accounts";
46  break;
47  case v1::statuses_id:
48  strcall = "/api/v1/statuses/" + strid;
49  break;
50  case v1::push_subscription:
51  strcall = "/api/v1/push/subscription";
52  break;
53  default:
54  ttdebug << "ERROR: Invalid argument.\n";
55  return { 22, "Invalid argument", 0, "" };
56  break;
57  }
58 
59  return del(strcall, params);
60 }
return_call del(const Mastodon::API::v1 &call, const parameters &parameters)
Make a DELETE request which requires parameters.
Definition: delete.cpp:23

◆ del() [2/2]

return_call Mastodon::API::del ( const string &  call,
const parameters parameters 
)

Make a custom DELETE request.

Parameters
callString in the form /api/v1/example
parametersA Mastodon::parametermap containing parameters
Since
0.100.0

◆ exceptions()

bool API::exceptions ( const bool &  value)

Turn exceptions on or off. Defaults to off.

This applies to exceptions from curlpp. curlpp::RuntimeError and curlpp::LogicError.

Parameters
valuetrue for on, false for off
Returns
true if exceptions are turned on, false otherwise
Since
before 0.11.0
253 {
254  _exceptions = value;
255  return _exceptions;
256 }

◆ get() [1/4]

const return_call API::get ( const Mastodon::API::v1 call)

Make a GET request which doesn't require parameters.

Parameters
callA call defined in Mastodon::API::v1
Since
0.100.0
Examples:
example01_get_public_timeline.cpp.
277 {
278  return get(call, {});
279 }

◆ get() [2/4]

const return_call API::get ( const Mastodon::API::v1 call,
const parameters parameters 
)

Make a GET request which requires parameters.

Parameters
callA call defined in Mastodon::API::v1
parametersA Mastodon::parametermap containing parameters
Since
0.100.0
25 {
26  string strcall = "";
27  string strid = "";
28 
29  // The ID is part of the path
30  const auto &it_id = params.find("id");
31  if (it_id != params.end())
32  {
33  strid = it_id->values[0];
34  }
35 
36  switch (call)
37  {
38  case v1::accounts_verify_credentials:
39  {
40  strcall = "/api/v1/accounts/verify_credentials";
41  break;
42  }
43  case v1::blocks:
44  {
45  strcall = "/api/v1/blocks";
46  break;
47  }
48  case v1::domain_blocks:
49  {
50  strcall = "/api/v1/domain_blocks";
51  break;
52  }
53  case v1::favourites:
54  {
55  strcall = "/api/v1/favourites";
56  break;
57  }
58  case v1::follow_requests:
59  {
60  strcall = "/api/v1/follow_requests";
61  break;
62  }
63  case v1::instance:
64  {
65  strcall = "/api/v1/instance";
66  break;
67  }
68  case v1::custom_emojis:
69  {
70  strcall = "/api/v1/custom_emojis";
71  break;
72  }
73  case v1::lists:
74  {
75  strcall = "/api/v1/lists";
76  break;
77  }
78  case v1::mutes:
79  {
80  strcall = "/api/v1/mutes";
81  break;
82  }
83  case v1::notifications:
84  {
85  strcall = "/api/v1/notifications";
86  break;
87  }
88  case v1::reports:
89  {
90  strcall = "/api/v1/reports";
91  break;
92  }
93  case v1::timelines_home:
94  {
95  strcall = "/api/v1/timelines/home";
96  break;
97  }
98  case v1::timelines_public:
99  {
100  strcall = "/api/v1/timelines/public";
101  break;
102  }
103  case v1::accounts_relationships:
104  {
105  strcall = "/api/v1/accounts/relationships";
106  break;
107  }
108  case v1::accounts_id:
109  {
110  strcall = "/api/v1/accounts/" + strid;
111  break;
112  }
113  case v1::accounts_id_followers:
114  {
115  strcall = "/api/v1/accounts/" + strid + "/followers";
116  break;
117  }
118  case v1::accounts_id_following:
119  {
120  strcall = "/api/v1/accounts/" + strid + "/following";
121  break;
122  }
123  case v1::accounts_id_statuses:
124  {
125  strcall = "/api/v1/accounts/" + strid + "/statuses";
126  break;
127  }
128  case v1::accounts_search:
129  {
130  strcall = "/api/v1/accounts/search";
131  break;
132  }
133  case v1::accounts_id_lists:
134  {
135  strcall = "/api/v1/accounts/" + strid + "/lists";
136  break;
137  }
138  case v1::lists_id_accounts:
139  {
140  strcall = "/api/v1/lists/" + strid + "/accounts";
141  break;
142  }
143  case v1::lists_id:
144  {
145  strcall = "/api/v1/lists/" + strid;
146  break;
147  }
148  case v1::notifications_id:
149  {
150  strcall = "/api/v1/notifications/" + strid;
151  break;
152  }
153  case v1::search:
154  {
155  strcall = "/api/v1/search";
156  break;
157  }
158  case v1::statuses_id:
159  {
160  strcall = "/api/v1/statuses/" + strid;
161  break;
162  }
163  case v1::statuses_id_context:
164  {
165  strcall = "/api/v1/statuses/" + strid + "/context";
166  break;
167  }
168  case v1::statuses_id_card:
169  {
170  strcall = "/api/v1/statuses/" + strid + "/card";
171  break;
172  }
173  case v1::statuses_id_reblogged_by:
174  {
175  strcall = "/api/v1/statuses/" + strid + "/reblogged_by";
176  break;
177  }
178  case v1::statuses_id_favourited_by:
179  {
180  strcall = "/api/v1/statuses/" + strid + "/favourited_by";
181  break;
182  }
183  case v1::timelines_tag_hashtag:
184  {
185  // The tag is part of the path
186  const auto &it = params.find("tag");
187  if (it != params.end())
188  {
189  strcall = "/api/v1/timelines/tag/" + urlencode(it->values[0]);
190  }
191  else
192  {
193  ttdebug << "ERROR: Invalid argument.\n";
194  return { 22, "Invalid argument", 0, "" };
195  }
196  break;
197  }
198  case v1::timelines_list_list_id:
199  {
200  strcall = "/api/v1/timelines/list/" + strid;
201  break;
202  }
203  case v1::push_subscription:
204  {
205  strcall = "/api/v1/push/subscription";
206  break;
207  }
208  case v1::endorsements:
209  {
210  strcall = "/api/v1/endorsements";
211  break;
212  }
213  case v1::bookmarks:
214  {
215  strcall = "/api/v1/bookmarks";
216  break;
217  }
218  default:
219  {
220  ttdebug << "ERROR: Invalid argument.\n";
221  return { 22, "Invalid argument", 0, "" };
222  }
223  }
224 
225  if (params.size() > 0)
226  {
227  // Delete the params that are already in strcall
228  parameters newparams = params;
229  newparams.erase(newparams.find("id"));
230  newparams.erase(newparams.find("tag"));
231  strcall += maptostr(newparams);
232  }
233 
234  return get(strcall);
235 }
static const string urlencode(const string &str)
Percent-encodes a string. This is done automatically, unless you make a custom request.
Definition: mastodon-cpp.cpp:144
Vector of Mastodon::param, used for passing parameters in calls.
Definition: types.hpp:62
const std::vector< param >::const_iterator find(const string &key) const
Get iterator to element with key.
Definition: types.cpp:27

◆ get() [3/4]

const return_call API::get ( const Mastodon::API::v2 call,
const parameters parameters 
)

Make a GET request which requires parameters.

Parameters
callA call defined in Mastodon::API::v2
parametersA Mastodon::parametermap containing parameters
Since
0.100.0
239 {
240  string strcall = "";
241  string strid = "";
242 
243  // The ID is part of the path
244  const auto &it = params.find("id");
245  if (it != params.end())
246  {
247  strid = it->values[0];
248  }
249 
250  switch (call)
251  {
252  case v2::search:
253  {
254  strcall = "/api/v2/search";
255  break;
256  }
257  default:
258  {
259  ttdebug << "ERROR: Invalid argument.\n";
260  return { 22, "Invalid argument", 0, "" };
261  }
262  }
263 
264  if (params.size() > 0)
265  {
266  // Delete the params that are already in strcall
267  parameters newparams = params;
268  newparams.erase(newparams.find("id"));
269  newparams.erase(newparams.find("tag"));
270  strcall += maptostr(newparams);
271  }
272 
273  return get(strcall);
274 }
Vector of Mastodon::param, used for passing parameters in calls.
Definition: types.hpp:62
const std::vector< param >::const_iterator find(const string &key) const
Get iterator to element with key.
Definition: types.cpp:27

◆ get() [4/4]

const return_call Mastodon::API::get ( const string &  call)

Make a custom GET request.

Parameters
callString in the form /api/v1/example
Since
0.100.0

◆ get_header()

const string API::get_header ( const string &  header) const

Gets the header from the last answer.

Parameters
headerThe header to get
Returns
The header, or "" on error.
Since
before 0.11.0
238 {
239  string headers;
240  _http.get_headers(headers);
241  size_t startpos = headers.find(header + ':');
242  if (startpos != std::string::npos)
243  {
244  startpos = headers.find(':', startpos) + 2;
245  size_t endpos = headers.find("\r\n", startpos);
246  return headers.substr(startpos, endpos - startpos);
247  }
248 
249  return "";
250 }
void get_headers(string &headers) const
Get all headers in a string.
Definition: http.cpp:227

◆ get_instance()

const string API::get_instance ( ) const

Returns the instance.

Returns
The instance.
Since
before 0.11.0
54 {
55  return _instance;
56 }

◆ get_proxy()

void API::get_proxy ( string &  proxy,
string &  userpw 
) const

For internal use.

Parameters
proxyURL
userpwusername:password
Since
0.15.1
571 {
572  if (!_proxy.empty())
573  {
574  proxy = _proxy;
575  if (!_proxy_userpw.empty())
576  {
577  userpw = _proxy_userpw;
578  }
579  }
580 }

◆ get_stream() [1/3]

void API::get_stream ( const Mastodon::API::v1 call,
const parameters parameters,
std::unique_ptr< Mastodon::API::http > &  ptr,
string &  stream 
)

Make a streaming GET request.

Parameters
callA call defined in Mastodon::API::v1
parametersA Mastodon::parametermap containing parameters
ptrPointer to the http object. Can be used to call ptr->cancel_stream()
Since
0.100.0
Examples:
example02_stream.cpp.
28 {
29  string strcall = "";
30 
31  switch (call)
32  {
33  case v1::streaming_user:
34  strcall = "/api/v1/streaming/user";
35  break;
36  case v1::streaming_public:
37  strcall = "/api/v1/streaming/public";
38  break;
39  case v1::streaming_public_local:
40  strcall = "/api/v1/streaming/public/local";
41  break;
42  case v1::streaming_hashtag:
43  strcall = "/api/v1/streaming/hashtag";
44  break;
45  case v1::streaming_list:
46  strcall = "/api/v1/streaming/list";
47  break;
48  default:
49  ttdebug << "ERROR: Invalid call.\n";
50  stream = "event: ERROR\ndata: {\"error_code\":22}\n";
51  return;
52  break;
53  }
54 
55  if (params.size() > 0)
56  {
57  strcall += maptostr(params);
58  }
59 
60  return get_stream(strcall, ptr, stream);
61 }
void get_stream(const Mastodon::API::v1 &call, const parameters &parameters, std::unique_ptr< Mastodon::API::http > &ptr, string &stream)
Make a streaming GET request.
Definition: get_stream.cpp:24

◆ get_stream() [2/3]

void API::get_stream ( const Mastodon::API::v1 call,
std::unique_ptr< Mastodon::API::http > &  ptr,
string &  stream 
)

Make a streaming GET request.

Parameters
callA call defined in Mastodon::API::v1
ptrPointer to the http object. Can be used to call ptr->cancel_stream()
Since
0.100.0
66 {
67  return get_stream(call, {}, ptr, stream);
68 }
void get_stream(const Mastodon::API::v1 &call, const parameters &parameters, std::unique_ptr< Mastodon::API::http > &ptr, string &stream)
Make a streaming GET request.
Definition: get_stream.cpp:24

◆ get_stream() [3/3]

void Mastodon::API::get_stream ( const string &  call,
std::unique_ptr< Mastodon::API::http > &  ptr,
string &  stream 
)

Make a streaming GET request.

Parameters
callString in the form /api/v1/example
ptrPointer to the http object. Can be used to call ptr->cancel_stream()
Since
0.100.0

◆ get_useragent()

const string API::get_useragent ( ) const

Gets the useragent.

Returns
The useragent.
Since
before 0.11.0
49 {
50  return _useragent;
51 }

◆ patch()

return_call API::patch ( const Mastodon::API::v1 call,
const parameters parameters 
)

Make a PATCH request.

Binary data must be base64-encoded or a filename.

Parameters
callA call defined in Mastodon::API::v1
parametersA Mastodon::parametermap containing parameters
Since
0.100.0
26 {
27  string strcall = "";
28  switch (call)
29  {
30  case v1::accounts_update_credentials:
31  strcall = "/api/v1/accounts/update_credentials";
32  break;
33  default:
34  ttdebug << "ERROR: Invalid argument.\n";
35  return { 22, "Invalid argument", 0, "" };
36  break;
37  }
38 
39  return _http.request(http_method::PATCH,
40  strcall, maptoformdata(params));
41 }

◆ post() [1/3]

return_call API::post ( const Mastodon::API::v1 call)

Make a POST request which doesn't require parameters.

Parameters
callA call defined in Mastodon::API::v1
Since
0.100.0
138 {
139  const parameters p;
140  return post(call, p);
141 }
Vector of Mastodon::param, used for passing parameters in calls.
Definition: types.hpp:62
return_call post(const Mastodon::API::v1 &call)
Make a POST request which doesn&#39;t require parameters.
Definition: post.cpp:137

◆ post() [2/3]

return_call API::post ( const Mastodon::API::v1 call,
const parameters parameters 
)

Make a POST request which requires parameters.

Binary data must be base64-encoded or a filename.

Parameters
callA call defined in Mastodon::API::v1
parametersA Mastodon::parametermap containing parameters
Since
0.100.0
25 {
26  string strcall = "";
27  string strid = "";
28 
29  // The ID is part of the path
30  const auto &it = params.find("id");
31  if (it != params.end())
32  {
33  strid = it->values[0];
34  }
35 
36  switch (call)
37  {
38  case v1::apps:
39  strcall = "/api/v1/apps";
40  break;
41  case v1::domain_blocks:
42  strcall = "/api/v1/domain_blocks";
43  break;
44  case v1::lists:
45  strcall = "/api/v1/lists";
46  break;
47  case v1::media:
48  strcall = "/api/v1/media";
49  break;
50  case v1::notifications_clear:
51  strcall = "/api/v1/notifications/clear";
52  break;
53  case v1::notifications_dismiss:
54  strcall = "/api/v1/notifications/dismiss";
55  break;
56  case v1::reports:
57  strcall = "/api/v1/reports";
58  break;
59  case v1::statuses:
60  strcall = "/api/v1/statuses";
61  break;
62  case v1::accounts_id_follow:
63  strcall = "/api/v1/accounts/" + strid + "/follow";
64  break;
65  case v1::accounts_id_unfollow:
66  strcall = "/api/v1/accounts/" + strid + "/unfollow";
67  break;
68  case v1::accounts_id_block:
69  strcall = "/api/v1/accounts/" + strid + "/block";
70  break;
71  case v1::accounts_id_unblock:
72  strcall = "/api/v1/accounts/" + strid + "/unblock";
73  break;
74  case v1::accounts_id_mute:
75  strcall = "/api/v1/accounts/" + strid + "/mute";
76  break;
77  case v1::accounts_id_unmute:
78  strcall = "/api/v1/accounts/" + strid + "/unmute";
79  break;
80  case v1::follow_requests_id_authorize:
81  strcall = "/api/v1/folow_requests/" + strid + "/authorize";
82  break;
83  case v1::follow_requests_id_reject:
84  strcall = "/api/v1/folow_requests/" + strid + "/reject";
85  break;
86  case v1::lists_id_accounts:
87  strcall = "/api/v1/lists/" + strid + "/accounts";
88  break;
89  case v1::statuses_id_reblog:
90  strcall = "/api/v1/statuses/" + strid + "/reblog";
91  break;
92  case v1::statuses_id_unreblog:
93  strcall = "/api/v1/statuses/" + strid + "/unreblog";
94  break;
95  case v1::statuses_id_favourite:
96  strcall = "/api/v1/statuses/" + strid + "/favourite";
97  break;
98  case v1::statuses_id_unfavourite:
99  strcall = "/api/v1/statuses/" + strid + "/unfavourite";
100  break;
101  case v1::statuses_id_pin:
102  strcall = "/api/v1/statuses/" + strid + "/pin";
103  break;
104  case v1::statuses_id_unpin:
105  strcall = "/api/v1/statuses/" + strid + "/unpin";
106  break;
107  case v1::statuses_id_mute:
108  strcall = "/api/v1/statuses/" + strid + "/mute";
109  break;
110  case v1::statuses_id_unmute:
111  strcall = "/api/v1/statuses/" + strid + "/unmute";
112  break;
113  case v1::push_subscription:
114  strcall = "/api/v1/push/subscription";
115  break;
116  case v1::accounts_id_pin:
117  strcall = "/api/v1/accounts/" + strid + "/pin";
118  break;
119  case v1::accounts_id_unpin:
120  strcall = "/api/v1/accounts/" + strid + "/unpin";
121  break;
122  case v1::statuses_id_bookmark:
123  strcall = "/api/v1/statuses/" + strid + "/bookmark";
124  break;
125  case v1::statuses_id_unbookmark:
126  strcall = "/api/v1/statuses/" + strid + "/unbookmark";
127  break;
128  default:
129  ttdebug << "ERROR: Invalid argument.\n";
130  return { 22, "Invalid argument", 0, ""};
131  break;
132  }
133 
134  return post(strcall, params);
135 }
return_call post(const Mastodon::API::v1 &call)
Make a POST request which doesn&#39;t require parameters.
Definition: post.cpp:137

◆ post() [3/3]

return_call API::post ( const string &  call,
const parameters parameters 
)

Make a custom POST request.

Binary data must be base64-encoded or a filename.

Parameters
callString in the form /api/v1/example
parametersA Mastodon::parametermap containing parameters
Since
0.100.0
144 {
145 
146  return _http.request(http_method::POST, call, maptoformdata(params));
147 }

◆ put() [1/2]

return_call API::put ( const Mastodon::API::v1 call,
const parameters parameters 
)

Make a PUT request which requires a parameters.

Parameters
callA call defined in Mastodon::API::v1
parametersA Mastodon::parametermap containing parameters
Since
0.100.0
25 {
26  string strcall = "";
27  string strid = "";
28 
29  // The ID is part of the path
30  const auto &it = params.find("id");
31  if (it != params.end())
32  {
33  strid = it->values[0];
34  }
35 
36  switch (call)
37  {
38  case v1::lists_id:
39  strcall = "/api/v1/lists/" + strid;
40  break;
41  case v1::media_id:
42  strcall = "/api/v1/media/" + strid;
43  break;
44  case v1::push_subscription:
45  strcall = "/api/v1/push/subscription";
46  break;
47  default:
48  ttdebug << "ERROR: Invalid argument.\n";
49  return { 22, "Invalid argument", 0, "" };
50  break;
51  }
52 
53  return put(strcall, params);
54 }
return_call put(const Mastodon::API::v1 &call, const parameters &parameters)
Make a PUT request which requires a parameters.
Definition: put.cpp:23

◆ put() [2/2]

return_call API::put ( const string &  call,
const parameters parameters 
)

Make a custom PUT request.

Parameters
callString in the form /api/v1/example
parametersA Mastodon::parametermap containing parameters
Since
0.100.0
57 {
58 
59  return _http.request(http_method::PUT, call, maptoformdata(params));
60 }

◆ register_app1()

return_call API::register_app1 ( const string &  client_name,
const string &  redirect_uri,
const string &  scopes,
const string &  website,
string &  client_id,
string &  client_secret,
string &  url 
)

Register application, step 1/2.

Parameters
client_nameThe name of the application
redirect_uriurn:ietf:wg:oauth:2.0:oob for none
scopesScopes (read, write, follow, push; space separated)
websiteThe website of the application
client_idReturned
client_secretReturned
urlReturned, used to generate code for register_app2
Since
before 0.100.0
161 {
162  parameters params =
163  {
164  { "client_name", { client_name } },
165  { "redirect_uris", { redirect_uri } },
166  { "scopes", { scopes } },
167  { "website", { website } }
168  };
169 
170  return_call ret = post(API::v1::apps, params);
171 
172  if (ret.error_code == 0)
173  {
174  std::smatch match;
175  std::regex reid("client_id\":\"([^\"]+)\"");
176  std::regex resecret("client_secret\":\"([^\"]+)\"");
177 
178  std::regex_search(ret.answer, match, reid);
179  client_id = match[1].str();
180  std::regex_search(ret.answer, match, resecret);
181  client_secret = match[1].str();
182 
183  url = "https://" + _instance + "/oauth/authorize" +
184  "?scope=" + urlencode(scopes) + "&response_type=code" +
185  "&redirect_uri=" + urlencode(redirect_uri) +
186  "&client_id=" + client_id;
187  if (!website.empty())
188  {
189  url += "&website=" + urlencode(website);
190  }
191  }
192  else if (ret.error_code == 78)
193  {
194  url = ret.answer;
195  }
196  else
197  {
198  std::cerr << "Error code: " << std::to_string(ret.error_code) << '\n';
199  }
200 
201  return ret;
202 }
static const string urlencode(const string &str)
Percent-encodes a string. This is done automatically, unless you make a custom request.
Definition: mastodon-cpp.cpp:144
Return type for API calls.
Definition: return_types.hpp:85
Vector of Mastodon::param, used for passing parameters in calls.
Definition: types.hpp:62
string answer
The response from the server.
Definition: return_types.hpp:99
uint8_t error_code
Error code.
Definition: return_types.hpp:41
return_call post(const Mastodon::API::v1 &call)
Make a POST request which doesn&#39;t require parameters.
Definition: post.cpp:137

◆ register_app2()

return_call API::register_app2 ( const string &  client_id,
const string &  client_secret,
const string &  redirect_uri,
const string &  code,
string &  access_token 
)

Register application, step 2/2.

The access token will be used in all subsequent calls.

Parameters
client_id
client_secret
redirect_uriurn:ietf:wg:oauth:2.0:oob for none
codeThe code generated by the website
access_tokenReturned
Since
before 0.100.0
209 {
210  parameters params =
211  {
212  { "client_id", { client_id } },
213  { "client_secret", { client_secret } },
214  { "grant_type", { "authorization_code" } },
215  { "redirect_uri", { redirect_uri } },
216  { "code", { code } },
217  };
218 
219  return_call ret = post("/oauth/token", params);
220  if (ret.error_code == 0)
221  {
222  std::smatch match;
223  std::regex retoken("access_token\":\"([^\"]+)\"");
224 
225  std::regex_search(ret.answer, match, retoken);
226  access_token = match[1].str();
227  _access_token = access_token;
228  }
229  else
230  {
231  std::cerr << "Error code: " << std::to_string(ret.error_code) << '\n';
232  }
233 
234  return ret;
235 }
Return type for API calls.
Definition: return_types.hpp:85
Vector of Mastodon::param, used for passing parameters in calls.
Definition: types.hpp:62
string answer
The response from the server.
Definition: return_types.hpp:99
uint8_t error_code
Error code.
Definition: return_types.hpp:41
return_call post(const Mastodon::API::v1 &call)
Make a POST request which doesn&#39;t require parameters.
Definition: post.cpp:137

◆ set_proxy()

void API::set_proxy ( const string &  proxy,
const string &  userpw = "" 
)

Sets the proxy.

Since mastodon-cpp is built on libcurl, it respects the same proxy environment variables. See man curl.

Parameters
proxySee man 3 CURLOPT_PROXY
userpwSee man 3 CURLOPT_PROXYUSERPWD (optional)
Since
0.15.0
565 {
566  _proxy = proxy;
567  _proxy_userpw = userpw;
568 }

◆ set_useragent()

void API::set_useragent ( const string &  useragent)

Sets the useragent. Default is mastodon-cpp/version.

Parameters
useragentThe useragent
Since
before 0.11.0
44 {
45  _useragent = useragent;
46 }

◆ unescape_html()

const string API::unescape_html ( const string &  html)
static

Replaces HTML entities with UTF-8 characters.

Supports named and numbered entities, decimal and hexadecimal.

Since
0.12.0
264 {
265  string buffer = html;
266  string output = "";
267 
268  // Used to convert int to utf-8 char
269  std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> u8c;
270  // Matches numbered entities between 1 and 8 digits, decimal or hexadecimal
271  std::regex re_entity("&#(x)?(\\d{1,8});");
272  std::smatch match;
273 
274  while (std::regex_search(buffer, match, re_entity))
275  {
276  char32_t codepoint = 0;
277  // 'x' in front of the number means it's hexadecimal, else decimal.
278  if (match[1].length() == 1)
279  {
280  codepoint = std::stoi(match[2].str(), nullptr, 16);
281  }
282  else
283  {
284  codepoint = std::stoi(match[2].str(), nullptr, 10);
285  }
286  output += match.prefix().str() + u8c.to_bytes(codepoint);
287  buffer = match.suffix().str();
288  }
289  output += buffer;
290 
291  // Source: https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_
292  // entity_references#Character_entity_references_in_HTML
293  const std::array<const std::pair<const string, const char32_t>, 258> names =
294  {{
295  { "exclamation", 0x0021 },
296  { "quot", 0x0022 },
297  { "percent", 0x0025 },
298  { "amp", 0x0026 },
299  { "apos", 0x0027 },
300  { "add", 0x002B },
301  { "lt", 0x003C },
302  { "equal", 0x003D },
303  { "gt", 0x003E },
304  { "nbsp", 0x00A0 },
305  { "iexcl", 0x00A1 },
306  { "cent", 0x00A2 },
307  { "pound", 0x00A3 },
308  { "curren", 0x00A4 },
309  { "yen", 0x00A5 },
310  { "brvbar", 0x00A6 },
311  { "sect", 0x00A7 },
312  { "uml", 0x00A8 },
313  { "copy", 0x00A9 },
314  { "ordf", 0x00AA },
315  { "laquo", 0x00AB },
316  { "not", 0x00AC },
317  { "shy", 0x00AD },
318  { "reg", 0x00AE },
319  { "macr", 0x00AF },
320  { "deg", 0x00B0 },
321  { "plusmn", 0x00B1 },
322  { "sup2", 0x00B2 },
323  { "sup3", 0x00B3 },
324  { "acute", 0x00B4 },
325  { "micro", 0x00B5 },
326  { "para", 0x00B6 },
327  { "middot", 0x00B7 },
328  { "cedil", 0x00B8 },
329  { "sup1", 0x00B9 },
330  { "ordm", 0x00BA },
331  { "raquo", 0x00BB },
332  { "frac14", 0x00BC },
333  { "frac12", 0x00BD },
334  { "frac34", 0x00BE },
335  { "iquest", 0x00BF },
336  { "Agrave", 0x00C0 },
337  { "Aacute", 0x00C1 },
338  { "Acirc", 0x00C2 },
339  { "Atilde", 0x00C3 },
340  { "Auml", 0x00C4 },
341  { "Aring", 0x00C5 },
342  { "AElig", 0x00C6 },
343  { "Ccedil", 0x00C7 },
344  { "Egrave", 0x00C8 },
345  { "Eacute", 0x00C9 },
346  { "Ecirc", 0x00CA },
347  { "Euml", 0x00CB },
348  { "Igrave", 0x00CC },
349  { "Iacute", 0x00CD },
350  { "Icirc", 0x00CE },
351  { "Iuml", 0x00CF },
352  { "ETH", 0x00D0 },
353  { "Ntilde", 0x00D1 },
354  { "Ograve", 0x00D2 },
355  { "Oacute", 0x00D3 },
356  { "Ocirc", 0x00D4 },
357  { "Otilde", 0x00D5 },
358  { "Ouml", 0x00D6 },
359  { "times", 0x00D7 },
360  { "Oslash", 0x00D8 },
361  { "Ugrave", 0x00D9 },
362  { "Uacute", 0x00DA },
363  { "Ucirc", 0x00DB },
364  { "Uuml", 0x00DC },
365  { "Yacute", 0x00DD },
366  { "THORN", 0x00DE },
367  { "szlig", 0x00DF },
368  { "agrave", 0x00E0 },
369  { "aacute", 0x00E1 },
370  { "acirc", 0x00E2 },
371  { "atilde", 0x00E3 },
372  { "auml", 0x00E4 },
373  { "aring", 0x00E5 },
374  { "aelig", 0x00E6 },
375  { "ccedil", 0x00E7 },
376  { "egrave", 0x00E8 },
377  { "eacute", 0x00E9 },
378  { "ecirc", 0x00EA },
379  { "euml", 0x00EB },
380  { "igrave", 0x00EC },
381  { "iacute", 0x00ED },
382  { "icirc", 0x00EE },
383  { "iuml", 0x00EF },
384  { "eth", 0x00F0 },
385  { "ntilde", 0x00F1 },
386  { "ograve", 0x00F2 },
387  { "oacute", 0x00F3 },
388  { "ocirc", 0x00F4 },
389  { "otilde", 0x00F5 },
390  { "ouml", 0x00F6 },
391  { "divide", 0x00F7 },
392  { "oslash", 0x00F8 },
393  { "ugrave", 0x00F9 },
394  { "uacute", 0x00FA },
395  { "ucirc", 0x00FB },
396  { "uuml", 0x00FC },
397  { "yacute", 0x00FD },
398  { "thorn", 0x00FE },
399  { "yuml", 0x00FF },
400  { "OElig", 0x0152 },
401  { "oelig", 0x0153 },
402  { "Scaron", 0x0160 },
403  { "scaron", 0x0161 },
404  { "Yuml", 0x0178 },
405  { "fnof", 0x0192 },
406  { "circ", 0x02C6 },
407  { "tilde", 0x02DC },
408  { "Alpha", 0x0391 },
409  { "Beta", 0x0392 },
410  { "Gamma", 0x0393 },
411  { "Delta", 0x0394 },
412  { "Epsilon", 0x0395 },
413  { "Zeta", 0x0396 },
414  { "Eta", 0x0397 },
415  { "Theta", 0x0398 },
416  { "Iota", 0x0399 },
417  { "Kappa", 0x039A },
418  { "Lambda", 0x039B },
419  { "Mu", 0x039C },
420  { "Nu", 0x039D },
421  { "Xi", 0x039E },
422  { "Omicron", 0x039F },
423  { "Pi", 0x03A0 },
424  { "Rho", 0x03A1 },
425  { "Sigma", 0x03A3 },
426  { "Tau", 0x03A4 },
427  { "Upsilon", 0x03A5 },
428  { "Phi", 0x03A6 },
429  { "Chi", 0x03A7 },
430  { "Psi", 0x03A8 },
431  { "Omega", 0x03A9 },
432  { "alpha", 0x03B1 },
433  { "beta", 0x03B2 },
434  { "gamma", 0x03B3 },
435  { "delta", 0x03B4 },
436  { "epsilon", 0x03B5 },
437  { "zeta", 0x03B6 },
438  { "eta", 0x03B7 },
439  { "theta", 0x03B8 },
440  { "iota", 0x03B9 },
441  { "kappa", 0x03BA },
442  { "lambda", 0x03BB },
443  { "mu", 0x03BC },
444  { "nu", 0x03BD },
445  { "xi", 0x03BE },
446  { "omicron", 0x03BF },
447  { "pi", 0x03C0 },
448  { "rho", 0x03C1 },
449  { "sigmaf", 0x03C2 },
450  { "sigma", 0x03C3 },
451  { "tau", 0x03C4 },
452  { "upsilon", 0x03C5 },
453  { "phi", 0x03C6 },
454  { "chi", 0x03C7 },
455  { "psi", 0x03C8 },
456  { "omega", 0x03C9 },
457  { "thetasym", 0x03D1 },
458  { "upsih", 0x03D2 },
459  { "piv", 0x03D6 },
460  { "ensp", 0x2002 },
461  { "emsp", 0x2003 },
462  { "thinsp", 0x2009 },
463  { "zwnj", 0x200C },
464  { "zwj", 0x200D },
465  { "lrm", 0x200E },
466  { "rlm", 0x200F },
467  { "ndash", 0x2013 },
468  { "mdash", 0x2014 },
469  { "horbar", 0x2015 },
470  { "lsquo", 0x2018 },
471  { "rsquo", 0x2019 },
472  { "sbquo", 0x201A },
473  { "ldquo", 0x201C },
474  { "rdquo", 0x201D },
475  { "bdquo", 0x201E },
476  { "dagger", 0x2020 },
477  { "Dagger", 0x2021 },
478  { "bull", 0x2022 },
479  { "hellip", 0x2026 },
480  { "permil", 0x2030 },
481  { "prime", 0x2032 },
482  { "Prime", 0x2033 },
483  { "lsaquo", 0x2039 },
484  { "rsaquo", 0x203A },
485  { "oline", 0x203E },
486  { "frasl", 0x2044 },
487  { "euro", 0x20AC },
488  { "image", 0x2111 },
489  { "weierp", 0x2118 },
490  { "real", 0x211C },
491  { "trade", 0x2122 },
492  { "alefsym", 0x2135 },
493  { "larr", 0x2190 },
494  { "uarr", 0x2191 },
495  { "rarr", 0x2192 },
496  { "darr", 0x2193 },
497  { "harr", 0x2194 },
498  { "crarr", 0x21B5 },
499  { "lArr", 0x21D0 },
500  { "uArr", 0x21D1 },
501  { "rArr", 0x21D2 },
502  { "dArr", 0x21D3 },
503  { "hArr", 0x21D4 },
504  { "forall", 0x2200 },
505  { "part", 0x2202 },
506  { "exist", 0x2203 },
507  { "empty", 0x2205 },
508  { "nabla", 0x2207 },
509  { "isin", 0x2208 },
510  { "notin", 0x2209 },
511  { "ni", 0x220B },
512  { "prod", 0x220F },
513  { "sum", 0x2211 },
514  { "minus", 0x2212 },
515  { "lowast", 0x2217 },
516  { "radic", 0x221A },
517  { "prop", 0x221D },
518  { "infin", 0x221E },
519  { "ang", 0x2220 },
520  { "and", 0x2227 },
521  { "or", 0x2228 },
522  { "cap", 0x2229 },
523  { "cup", 0x222A },
524  { "int", 0x222B },
525  { "there4", 0x2234 },
526  { "sim", 0x223C },
527  { "cong", 0x2245 },
528  { "asymp", 0x2248 },
529  { "ne", 0x2260 },
530  { "equiv", 0x2261 },
531  { "le", 0x2264 },
532  { "ge", 0x2265 },
533  { "sub", 0x2282 },
534  { "sup", 0x2283 },
535  { "nsub", 0x2284 },
536  { "sube", 0x2286 },
537  { "supe", 0x2287 },
538  { "oplus", 0x2295 },
539  { "otimes", 0x2297 },
540  { "perp", 0x22A5 },
541  { "sdot", 0x22C5 },
542  { "lceil", 0x2308 },
543  { "rceil", 0x2309 },
544  { "lfloor", 0x230A },
545  { "rfloor", 0x230B },
546  { "lang", 0x2329 },
547  { "rang", 0x232A },
548  { "loz", 0x25CA },
549  { "spades", 0x2660 },
550  { "clubs", 0x2663 },
551  { "hearts", 0x2665 },
552  { "diams", 0x2666 }
553  }};
554 
555  for (auto &pair : names)
556  {
557  const std::regex re('&' + pair.first + ';');
558  output = std::regex_replace(output, re, u8c.to_bytes(pair.second));
559  }
560 
561  return output;
562 }

◆ urldecode()

const std::string API::urldecode ( const string &  str)
static

Decodes a percent-encoded string.

Calls curlpp::unescape(str)

See RFC 3986 section 2.1 for more info.

Parameters
strThe string
Returns
The decoded string
Since
0.18.0
150 {
151  return curlpp::unescape(str);
152 }

◆ urlencode()

const std::string API::urlencode ( const string &  str)
static

Percent-encodes a string. This is done automatically, unless you make a custom request.

Calls curlpp::escape(str)

The only time you should use this, is if you use get(const string &call, string &answer).

See RFC 3986 section 2.1 for more info.

Parameters
strThe string
Returns
The percent-encoded string
Since
before 0.11.0
145 {
146  return curlpp::escape(str);
147 }

The documentation for this class was generated from the following files: