mastodon-cpp  0.104.0
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, 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,
  apps_verify_credentials, 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,
  filters, filters_id, 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  {
40  strcall = "/api/v1/domain_blocks";
41  break;
42  }
43  case v1::lists_id:
44  {
45  strcall = "/api/v1/lists/" + strid;
46  break;
47  }
48  case v1::lists_id_accounts:
49  {
50  strcall = "/api/v1/lists/" + strid + "/accounts";
51  break;
52  }
53  case v1::statuses_id:
54  {
55  strcall = "/api/v1/statuses/" + strid;
56  break;
57  }
58  case v1::push_subscription:
59  {
60  strcall = "/api/v1/push/subscription";
61  break;
62  }
63  case v1::filters_id:
64  {
65  strcall = "/api/v1/filters/" + strid;
66  break;
67  }
68  default:
69  {
70  ttdebug << "ERROR: Invalid argument.\n";
71  return { 22, "Invalid argument", 0, "" };
72  }
73  }
74 
75  return del(strcall, params);
76 }
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
255 {
256  _exceptions = value;
257  return _exceptions;
258 }

◆ 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.
289 {
290  return get(call, {});
291 }

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

◆ 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
253 {
254  string strcall = "";
255  string strid = "";
256 
257  // The ID is part of the path
258  const auto &it = params.find("id");
259  if (it != params.end())
260  {
261  strid = it->values[0];
262  }
263 
264  switch (call)
265  {
266  case v2::search:
267  {
268  strcall = "/api/v2/search";
269  break;
270  }
271  default:
272  {
273  ttdebug << "ERROR: Invalid argument.\n";
274  return { 22, "Invalid argument", 0, "" };
275  }
276  }
277 
278  if (params.size() > 0)
279  {
280  // Delete the parameterss that are already in strcall
281  const parameters newparams = delete_params(params, { "id", "tag" });
282  strcall += maptostr(newparams);
283  }
284 
285  return get(strcall);
286 }
Vector of Mastodon::param, used for passing parameters in calls.
Definition: types.hpp:62

◆ 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
240 {
241  string headers;
242  _http.get_headers(headers);
243  size_t startpos = headers.find(header + ':');
244  if (startpos != std::string::npos)
245  {
246  startpos = headers.find(':', startpos) + 2;
247  size_t endpos = headers.find("\r\n", startpos);
248  return headers.substr(startpos, endpos - startpos);
249  }
250 
251  return "";
252 }
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
573 {
574  if (!_proxy.empty())
575  {
576  proxy = _proxy;
577  if (!_proxy_userpw.empty())
578  {
579  userpw = _proxy_userpw;
580  }
581  }
582 }

◆ 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
209 {
210  const parameters p;
211  return post(call, p);
212 }
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:208

◆ 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  {
40  strcall = "/api/v1/apps";
41  break;
42  }
43  case v1::domain_blocks:
44  {
45  strcall = "/api/v1/domain_blocks";
46  break;
47  }
48  case v1::lists:
49  {
50  strcall = "/api/v1/lists";
51  break;
52  }
53  case v1::media:
54  {
55  strcall = "/api/v1/media";
56  break;
57  }
58  case v1::notifications_clear:
59  {
60  strcall = "/api/v1/notifications/clear";
61  break;
62  }
63  case v1::notifications_dismiss:
64  {
65  strcall = "/api/v1/notifications/dismiss";
66  break;
67  }
68  case v1::reports:
69  {
70  strcall = "/api/v1/reports";
71  break;
72  }
73  case v1::statuses:
74  {
75  strcall = "/api/v1/statuses";
76  break;
77  }
78  case v1::accounts:
79  {
80  strcall = "/api/v1/accounts";
81  break;
82  }
83  case v1::accounts_id_follow:
84  {
85  strcall = "/api/v1/accounts/" + strid + "/follow";
86  break;
87  }
88  case v1::accounts_id_unfollow:
89  {
90  strcall = "/api/v1/accounts/" + strid + "/unfollow";
91  break;
92  }
93  case v1::accounts_id_block:
94  {
95  strcall = "/api/v1/accounts/" + strid + "/block";
96  break;
97  }
98  case v1::accounts_id_unblock:
99  {
100  strcall = "/api/v1/accounts/" + strid + "/unblock";
101  break;
102  }
103  case v1::accounts_id_mute:
104  {
105  strcall = "/api/v1/accounts/" + strid + "/mute";
106  break;
107  }
108  case v1::accounts_id_unmute:
109  {
110  strcall = "/api/v1/accounts/" + strid + "/unmute";
111  break;
112  }
113  case v1::follow_requests_id_authorize:
114  {
115  strcall = "/api/v1/folow_requests/" + strid + "/authorize";
116  break;
117  }
118  case v1::follow_requests_id_reject:
119  {
120  strcall = "/api/v1/folow_requests/" + strid + "/reject";
121  break;
122  }
123  case v1::lists_id_accounts:
124  {
125  strcall = "/api/v1/lists/" + strid + "/accounts";
126  break;
127  }
128  case v1::statuses_id_reblog:
129  {
130  strcall = "/api/v1/statuses/" + strid + "/reblog";
131  break;
132  }
133  case v1::statuses_id_unreblog:
134  {
135  strcall = "/api/v1/statuses/" + strid + "/unreblog";
136  break;
137  }
138  case v1::statuses_id_favourite:
139  {
140  strcall = "/api/v1/statuses/" + strid + "/favourite";
141  break;
142  }
143  case v1::statuses_id_unfavourite:
144  {
145  strcall = "/api/v1/statuses/" + strid + "/unfavourite";
146  break;
147  }
148  case v1::statuses_id_pin:
149  {
150  strcall = "/api/v1/statuses/" + strid + "/pin";
151  break;
152  }
153  case v1::statuses_id_unpin:
154  {
155  strcall = "/api/v1/statuses/" + strid + "/unpin";
156  break;
157  }
158  case v1::statuses_id_mute:
159  {
160  strcall = "/api/v1/statuses/" + strid + "/mute";
161  break;
162  }
163  case v1::statuses_id_unmute:
164  {
165  strcall = "/api/v1/statuses/" + strid + "/unmute";
166  break;
167  }
168  case v1::push_subscription:
169  {
170  strcall = "/api/v1/push/subscription";
171  break;
172  }
173  case v1::accounts_id_pin:
174  {
175  strcall = "/api/v1/accounts/" + strid + "/pin";
176  break;
177  }
178  case v1::accounts_id_unpin:
179  {
180  strcall = "/api/v1/accounts/" + strid + "/unpin";
181  break;
182  }
183  case v1::statuses_id_bookmark:
184  {
185  strcall = "/api/v1/statuses/" + strid + "/bookmark";
186  break;
187  }
188  case v1::statuses_id_unbookmark:
189  {
190  strcall = "/api/v1/statuses/" + strid + "/unbookmark";
191  break;
192  }
193  case v1::filters:
194  {
195  strcall = "/api/v1/filters";
196  break;
197  }
198  default:
199  {
200  ttdebug << "ERROR: Invalid argument.\n";
201  return { 22, "Invalid argument", 0, ""};
202  }
203  }
204 
205  return post(strcall, params);
206 }
return_call post(const Mastodon::API::v1 &call)
Make a POST request which doesn&#39;t require parameters.
Definition: post.cpp:208

◆ 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
215 {
216 
217  return _http.request(http_method::POST, call, maptoformdata(params));
218 }

◆ 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  {
40  strcall = "/api/v1/lists/" + strid;
41  break;
42  }
43  case v1::media_id:
44  {
45  strcall = "/api/v1/media/" + strid;
46  break;
47  }
48  case v1::push_subscription:
49  {
50  strcall = "/api/v1/push/subscription";
51  break;
52  }
53  case v1::filters_id:
54  {
55  strcall = "/api/v1/filters/" + strid;
56  break;
57  }
58  default:
59  {
60  ttdebug << "ERROR: Invalid argument.\n";
61  return { 22, "Invalid argument", 0, "" };
62  }
63  }
64 
65  return put(strcall, params);
66 }
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
69 {
70 
71  return _http.request(http_method::PUT, call, maptoformdata(params));
72 }

◆ 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
163 {
164  parameters params =
165  {
166  { "client_name", { client_name } },
167  { "redirect_uris", { redirect_uri } },
168  { "scopes", { scopes } },
169  { "website", { website } }
170  };
171 
172  return_call ret = post(API::v1::apps, params);
173 
174  if (ret.error_code == 0)
175  {
176  std::smatch match;
177  std::regex reid("client_id\":\"([^\"]+)\"");
178  std::regex resecret("client_secret\":\"([^\"]+)\"");
179 
180  std::regex_search(ret.answer, match, reid);
181  client_id = match[1].str();
182  std::regex_search(ret.answer, match, resecret);
183  client_secret = match[1].str();
184 
185  url = "https://" + _instance + "/oauth/authorize" +
186  "?scope=" + urlencode(scopes) + "&response_type=code" +
187  "&redirect_uri=" + urlencode(redirect_uri) +
188  "&client_id=" + client_id;
189  if (!website.empty())
190  {
191  url += "&website=" + urlencode(website);
192  }
193  }
194  else if (ret.error_code == 78)
195  {
196  url = ret.answer;
197  }
198  else
199  {
200  std::cerr << "Error code: " << std::to_string(ret.error_code) << '\n';
201  }
202 
203  return ret;
204 }
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:146
Return type for API calls.
Definition: return_types.hpp:92
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:208

◆ 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
211 {
212  parameters params =
213  {
214  { "client_id", { client_id } },
215  { "client_secret", { client_secret } },
216  { "grant_type", { "authorization_code" } },
217  { "redirect_uri", { redirect_uri } },
218  { "code", { code } },
219  };
220 
221  return_call ret = post("/oauth/token", params);
222  if (ret.error_code == 0)
223  {
224  std::smatch match;
225  std::regex retoken("access_token\":\"([^\"]+)\"");
226 
227  std::regex_search(ret.answer, match, retoken);
228  access_token = match[1].str();
229  _access_token = access_token;
230  }
231  else
232  {
233  std::cerr << "Error code: " << std::to_string(ret.error_code) << '\n';
234  }
235 
236  return ret;
237 }
Return type for API calls.
Definition: return_types.hpp:92
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:208

◆ 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
567 {
568  _proxy = proxy;
569  _proxy_userpw = userpw;
570 }

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

◆ 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
152 {
153  return curlpp::unescape(str);
154 }

◆ 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
147 {
148  return curlpp::escape(str);
149 }

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