mastodon-cpp  0.106.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, suggestions, suggestions_accountid, 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 that doesn't require parameters. More...
 
const return_call get (const Mastodon::API::v1 &call, const parameters &parameters)
 Make a GET request that requires parameters. More...
 
const return_call get (const Mastodon::API::v2 &call, const parameters &parameters)
 Make a GET request that 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 that doesn't require parameters. More...
 
return_call post (const Mastodon::API::v1 &call, const parameters &parameters)
 Make a POST request that 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 that requires 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 that 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)
 Alias for Mastodon::urlencode. More...
 
static const string urldecode (const string &str)
 Alias for Mastodon::urldecode. More...
 
static const string unescape_html (const string &html)
 Alias for Mastodon::unescape_html. More...
 

Detailed Description

Interface to the Mastodon API.

All input is expected to be UTF-8. Binary data must be a filename.

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

◆ v2

enum Mastodon::API::v2
strong

A list of all v2 API calls.

The original / are substituted by _.

Since
0.16.0
283  {
284  search
285  };

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
33 : _instance(instance)
34 , _access_token(access_token)
35 , _useragent(string("mastodon-cpp/") + global::version)
36 , _http(*this, instance, access_token)
37 , _exceptions(false)
38 , _proxy("")
39 , _proxy_userpw("")
40 {
41  bool fash = false;
42  const std::regex re_gab("(?:\\.|^)gab\\.[^\\.]+$");
43  const std::regex re_kiwifarms("(?:\\.|^)kiwifarms\\.[^\\.]+$");
44 
45  for (const std::regex &re : { re_gab, re_kiwifarms })
46  {
47  if (std::regex_search(_instance, re))
48  {
49  fash = true;
50  break;
51  }
52  }
53 
54  if (fash)
55  {
56  throw std::runtime_error("Fascist instance detected: " + _instance);
57  }
58 }

◆ ~API()

API::~API ( )
virtual

Destroys the object.

Since
0.100.0
61 {}

Member Function Documentation

◆ del() [1/2]

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

Make a DELETE request that 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 parameters::const_iterator &it_id = params.find("id");
31  const parameters::const_iterator &it_aid = params.find("accountid");
32  if (it_id != params.end())
33  {
34  strid = it_id->values[0];
35  }
36  else if (it_aid != params.end())
37  {
38  strid = it_aid->values[0];
39  }
40 
41  switch (call)
42  {
43  case v1::domain_blocks:
44  {
45  strcall = "/api/v1/domain_blocks";
46  break;
47  }
48  case v1::lists_id:
49  {
50  strcall = "/api/v1/lists/" + strid;
51  break;
52  }
53  case v1::lists_id_accounts:
54  {
55  strcall = "/api/v1/lists/" + strid + "/accounts";
56  break;
57  }
58  case v1::statuses_id:
59  {
60  strcall = "/api/v1/statuses/" + strid;
61  break;
62  }
63  case v1::push_subscription:
64  {
65  strcall = "/api/v1/push/subscription";
66  break;
67  }
68  case v1::filters_id:
69  {
70  strcall = "/api/v1/filters/" + strid;
71  break;
72  }
73  case v1::suggestions_accountid:
74  {
75  strcall = "/api/v1/suggestions/" + strid;
76  break;
77  }
78  default:
79  {
80  ttdebug << "ERROR: Invalid argument.\n";
81  return { 22, "Invalid argument", 0, "" };
82  }
83  }
84 
85  return del(strcall, params);
86 }
return_call del(const Mastodon::API::v1 &call, const parameters &parameters)
Make a DELETE request that 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
275 {
276  _exceptions = value;
277  return _exceptions;
278 }

◆ get() [1/4]

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

Make a GET request that doesn't require parameters.

Parameters
callA call defined in Mastodon::API::v1
Since
0.100.0
Examples:
example01_get_public_timeline.cpp.
294 {
295  return get(call, {});
296 }

◆ get() [2/4]

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

Make a GET request that 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  case v1::suggestions:
235  {
236  strcall = "/api/v1/suggestions";
237  break;
238  }
239  default:
240  {
241  ttdebug << "ERROR: Invalid argument.\n";
242  return { 22, "Invalid argument", 0, "" };
243  }
244  }
245 
246  if (params.size() > 0)
247  {
248  // Delete the parameters that are already in strcall
249  const parameters newparams = delete_params(params, { "id", "tag" });
250  strcall += maptostr(newparams);
251  }
252 
253  return get(strcall);
254 }
static const string urlencode(const string &str)
Alias for Mastodon::urlencode.
Definition: mastodon-cpp.cpp:637

◆ get() [3/4]

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

Make a GET request that requires parameters.

Parameters
callA call defined in Mastodon::API::v2
parametersA Mastodon::parametermap containing parameters
Since
0.100.0
258 {
259  string strcall = "";
260  string strid = "";
261 
262  // The ID is part of the path
263  const auto &it = params.find("id");
264  if (it != params.end())
265  {
266  strid = it->values[0];
267  }
268 
269  switch (call)
270  {
271  case v2::search:
272  {
273  strcall = "/api/v2/search";
274  break;
275  }
276  default:
277  {
278  ttdebug << "ERROR: Invalid argument.\n";
279  return { 22, "Invalid argument", 0, "" };
280  }
281  }
282 
283  if (params.size() > 0)
284  {
285  // Delete the parameterss that are already in strcall
286  const parameters newparams = delete_params(params, { "id", "tag" });
287  strcall += maptostr(newparams);
288  }
289 
290  return get(strcall);
291 }

◆ 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
260 {
261  string headers;
262  _http.get_headers(headers);
263  size_t startpos = headers.find(header + ':');
264  if (startpos != std::string::npos)
265  {
266  startpos = headers.find(':', startpos) + 2;
267  size_t endpos = headers.find("\r\n", startpos);
268  return headers.substr(startpos, endpos - startpos);
269  }
270 
271  return "";
272 }
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
74 {
75  return _instance;
76 }

◆ get_proxy()

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

For internal use.

Parameters
proxyURL
userpwusername:password
Since
0.15.1
292 {
293  if (!_proxy.empty())
294  {
295  proxy = _proxy;
296  if (!_proxy_userpw.empty())
297  {
298  userpw = _proxy_userpw;
299  }
300  }
301 }

◆ 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 Mastodon::API::v1::streaming_user:
34  {
35  strcall = "/api/v1/streaming/user";
36  break;
37  }
38  case v1::streaming_public:
39  {
40  strcall = "/api/v1/streaming/public";
41  break;
42  }
43  case v1::streaming_public_local:
44  {
45  strcall = "/api/v1/streaming/public/local";
46  break;
47  }
48  case v1::streaming_hashtag:
49  {
50  strcall = "/api/v1/streaming/hashtag";
51  break;
52  }
53  case v1::streaming_list:
54  {
55  strcall = "/api/v1/streaming/list";
56  break;
57  }
58  default:
59  {
60  ttdebug << "ERROR: Invalid call.\n";
61  stream = "event: ERROR\ndata: {\"error_code\":22}\n";
62  return;
63  }
64  }
65 
66  if (params.size() > 0)
67  {
68  strcall += maptostr(params);
69  }
70 
71  return get_stream(strcall, ptr, stream);
72 }
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
77 {
78  return get_stream(call, {}, ptr, stream);
79 }
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
69 {
70  return _useragent;
71 }

◆ patch()

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

Make a PATCH request.

Binary data must be 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 that 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 that 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 that requires parameters.

Binary data must be 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 that 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 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 that 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 = 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 that requires 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
183 {
184  parameters params =
185  {
186  { "client_name", { client_name } },
187  { "redirect_uris", { redirect_uri } },
188  { "scopes", { scopes } },
189  { "website", { website } }
190  };
191 
192  return_call ret = post(API::v1::apps, params);
193 
194  if (ret.error_code == 0)
195  {
196  std::smatch match;
197  std::regex reid("client_id\":\"([^\"]+)\"");
198  std::regex resecret("client_secret\":\"([^\"]+)\"");
199 
200  std::regex_search(ret.answer, match, reid);
201  client_id = match[1].str();
202  std::regex_search(ret.answer, match, resecret);
203  client_secret = match[1].str();
204 
205  url = "https://" + _instance + "/oauth/authorize" +
206  "?scope=" + ::urlencode(scopes) + "&response_type=code" +
207  "&redirect_uri=" + ::urlencode(redirect_uri) +
208  "&client_id=" + client_id;
209  if (!website.empty())
210  {
211  url += "&website=" + ::urlencode(website);
212  }
213  }
214  else if (ret.error_code == 78)
215  {
216  url = ret.answer;
217  }
218  else
219  {
220  std::cerr << "Error code: " << std::to_string(ret.error_code) << '\n';
221  }
222 
223  return ret;
224 }
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 that doesn&#39;t require parameters.
Definition: post.cpp:208
static const string urlencode(const string &str)
Alias for Mastodon::urlencode.
Definition: mastodon-cpp.cpp:637

◆ 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
231 {
232  parameters params =
233  {
234  { "client_id", { client_id } },
235  { "client_secret", { client_secret } },
236  { "grant_type", { "authorization_code" } },
237  { "redirect_uri", { redirect_uri } },
238  { "code", { code } },
239  };
240 
241  return_call ret = post("/oauth/token", params);
242  if (ret.error_code == 0)
243  {
244  std::smatch match;
245  std::regex retoken("access_token\":\"([^\"]+)\"");
246 
247  std::regex_search(ret.answer, match, retoken);
248  access_token = match[1].str();
249  _access_token = access_token;
250  }
251  else
252  {
253  std::cerr << "Error code: " << std::to_string(ret.error_code) << '\n';
254  }
255 
256  return ret;
257 }
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 that 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
286 {
287  _proxy = proxy;
288  _proxy_userpw = userpw;
289 }

◆ 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
64 {
65  _useragent = useragent;
66 }

◆ unescape_html()

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

Alias for Mastodon::unescape_html.

Since
0.12.0
648 {
649  return Mastodon::unescape_html(html);
650 }
const string unescape_html(const string &html)
Replaces HTML entities with UTF-8 characters.
Definition: mastodon-cpp.cpp:333

◆ urldecode()

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

Alias for Mastodon::urldecode.

Parameters
strThe string
Returns
The decoded string
Since
0.18.0
643 {
644  return Mastodon::urldecode(str);
645 }
const string urldecode(const string &str)
Decodes a percent-encoded string.

◆ urlencode()

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

Alias for Mastodon::urlencode.

Parameters
strThe string
Returns
The percent-encoded string
Since
before 0.11.0
638 {
639  return Mastodon::urlencode(str);
640 }
const string urlencode(const string &str)
Percent-encodes a string. This is done automatically, unless you make a custom request.

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