mastodon-cpp  0.110.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_accounts, lists_id,
  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,
  polls_id, polls_id_votes, reports, statuses_id,
  statuses_id_context, statuses_id_card, statuses_id_reblogged_by, statuses_id_favourited_by,
  statuses, statuses_id_reblog, statuses_id_unreblog, statuses_id_pin,
  statuses_id_unpin, timelines_home, conversations, timelines_public,
  timelines_tag_hashtag, timelines_list_list_id, streaming_health, streaming_user,
  streaming_public, streaming_public_local, streaming_hashtag, streaming_hashtag_local,
  streaming_list, streaming_direct, 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 &hostport, const string &userpw="")
 Sets the proxy. 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, unique_ptr< Mastodon::API::http > &ptr, string &stream)
 Make a streaming GET request. More...
 
void get_stream (const Mastodon::API::v1 &call, unique_ptr< Mastodon::API::http > &ptr, string &stream)
 Make a streaming GET request. More...
 
void get_stream (const string &call, 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
1 Invalid argument
10 URL changed (HTTP 301 or 308)
11 Connection timed out
12 Connection refused (check http_error_code)
13 No route to host / Could not resolve host
14 Encryption error
127 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
182  {
183  // Mastodon
184  accounts_id,
185  accounts,
186  accounts_verify_credentials,
187  accounts_update_credentials,
188  accounts_id_followers,
189  accounts_id_following,
190  accounts_id_statuses,
191  accounts_id_follow,
192  accounts_id_unfollow,
193  accounts_relationships,
194  accounts_search,
195 
196  apps,
197  apps_verify_credentials,
198 
199  blocks,
200  accounts_id_block,
201  accounts_id_unblock,
202 
203  custom_emojis,
204 
205  domain_blocks,
206 
207  endorsements,
208  accounts_id_pin,
209  accounts_id_unpin,
210 
211  favourites,
212  statuses_id_favourite,
213  statuses_id_unfavourite,
214 
215  filters,
216  filters_id,
217 
218  follow_requests,
219  follow_requests_id_authorize,
220  follow_requests_id_reject,
221 
222  suggestions,
223  suggestions_accountid,
224 
225  instance,
226 
227  lists,
228  accounts_id_lists,
229  lists_id_accounts,
230  lists_id,
231 
232  media,
233  media_id,
234 
235  mutes,
236  accounts_id_mute,
237  accounts_id_unmute,
238  statuses_id_mute,
239  statuses_id_unmute,
240 
241  notifications,
242  notifications_id,
243  notifications_clear,
244  notifications_dismiss,
245  push_subscription,
246 
247  polls_id,
248  polls_id_votes,
249 
250  reports,
251 
252  // scheduled_statuses,
253  // scheduled_statuses_id,
254 
255  statuses_id,
256  statuses_id_context,
257  statuses_id_card,
258  statuses_id_reblogged_by,
259  statuses_id_favourited_by,
260  statuses,
261  statuses_id_reblog,
262  statuses_id_unreblog,
263  statuses_id_pin,
264  statuses_id_unpin,
265 
266  timelines_home,
267  conversations,
268  timelines_public,
269  timelines_tag_hashtag,
270  timelines_list_list_id,
271 
272  streaming_health,
273  streaming_user,
274  streaming_public,
275  streaming_public_local,
276  streaming_hashtag,
277  streaming_hashtag_local,
278  streaming_list,
279  streaming_direct,
280 
281  // Glitch-Soc
282  bookmarks,
283  statuses_id_bookmark,
284  statuses_id_unbookmark
285  };

◆ v2

enum Mastodon::API::v2
strong

A list of all v2 API calls.

    The original `/` are substituted by `_`.
Since
0.16.0
295  {
296  search
297  };

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

◆ ~API()

API::~API ( )
virtual

Destroys the object.

Since
0.100.0
60 {}

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 { error::INVALID_ARGUMENT, "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.

    Most exceptions will be thrown at you to handle if on.
Parameters
valuetrue for on, false for off
Returns
true if exceptions are turned on, false otherwise
Since
before 0.11.0
284 {
285  _exceptions = value;
286  return _exceptions;
287 }

◆ 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.
299 {
300  return get(call, {});
301 }
const return_call get(const Mastodon::API::v1 &call)
Make a GET request that doesn't require parameters.
Definition: get.cpp:298

◆ 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::timelines_home:
90  {
91  strcall = "/api/v1/timelines/home";
92  break;
93  }
94  case v1::timelines_public:
95  {
96  strcall = "/api/v1/timelines/public";
97  break;
98  }
99  case v1::accounts_relationships:
100  {
101  strcall = "/api/v1/accounts/relationships";
102  break;
103  }
104  case v1::accounts_id:
105  {
106  strcall = "/api/v1/accounts/" + strid;
107  break;
108  }
109  case v1::accounts_id_followers:
110  {
111  strcall = "/api/v1/accounts/" + strid + "/followers";
112  break;
113  }
114  case v1::accounts_id_following:
115  {
116  strcall = "/api/v1/accounts/" + strid + "/following";
117  break;
118  }
119  case v1::accounts_id_statuses:
120  {
121  strcall = "/api/v1/accounts/" + strid + "/statuses";
122  break;
123  }
124  case v1::accounts_search:
125  {
126  strcall = "/api/v1/accounts/search";
127  break;
128  }
129  case v1::accounts_id_lists:
130  {
131  strcall = "/api/v1/accounts/" + strid + "/lists";
132  break;
133  }
134  case v1::lists_id_accounts:
135  {
136  strcall = "/api/v1/lists/" + strid + "/accounts";
137  break;
138  }
139  case v1::lists_id:
140  {
141  strcall = "/api/v1/lists/" + strid;
142  break;
143  }
144  case v1::notifications_id:
145  {
146  strcall = "/api/v1/notifications/" + strid;
147  break;
148  }
149  case v1::statuses_id:
150  {
151  strcall = "/api/v1/statuses/" + strid;
152  break;
153  }
154  case v1::statuses_id_context:
155  {
156  strcall = "/api/v1/statuses/" + strid + "/context";
157  break;
158  }
159  case v1::statuses_id_card:
160  {
161  strcall = "/api/v1/statuses/" + strid + "/card";
162  break;
163  }
164  case v1::statuses_id_reblogged_by:
165  {
166  strcall = "/api/v1/statuses/" + strid + "/reblogged_by";
167  break;
168  }
169  case v1::statuses_id_favourited_by:
170  {
171  strcall = "/api/v1/statuses/" + strid + "/favourited_by";
172  break;
173  }
174  case v1::timelines_tag_hashtag:
175  {
176  // The tag is part of the path
177  const auto &it = params.find("tag");
178  if (it != params.end())
179  {
180  strcall = "/api/v1/timelines/tag/" + ::urlencode(it->values[0]);
181  }
182  else
183  {
184  ttdebug << "ERROR: Invalid argument.\n";
185  return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" };
186  }
187  break;
188  }
189  case v1::timelines_list_list_id:
190  {
191  strcall = "/api/v1/timelines/list/" + strid;
192  break;
193  }
194  case v1::push_subscription:
195  {
196  strcall = "/api/v1/push/subscription";
197  break;
198  }
199  case v1::endorsements:
200  {
201  strcall = "/api/v1/endorsements";
202  break;
203  }
204  case v1::bookmarks:
205  {
206  strcall = "/api/v1/bookmarks";
207  break;
208  }
209  case v1::apps_verify_credentials:
210  {
211  strcall = "/api/v1/apps/verify_credentials";
212  break;
213  }
214  case v1::filters:
215  {
216  strcall = "/api/v1/filters";
217  break;
218  }
219  case v1::filters_id:
220  {
221  strcall = "/api/v1/filters/" + strid;
222  break;
223  }
224  case v1::suggestions:
225  {
226  strcall = "/api/v1/suggestions";
227  break;
228  }
229  case v1::polls_id:
230  {
231  strcall = "/api/v1/polls/" + strid;
232  break;
233  }
234  case v1::conversations:
235  {
236  strcall = "/api/v1/conversations";
237  break;
238  }
239  case v1::streaming_health:
240  {
241  strcall = "/api/v1/streaming/health";
242  break;
243  }
244  default:
245  {
246  ttdebug << "ERROR: Invalid argument.\n";
247  return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" };
248  }
249  }
250 
251  if (params.size() > 0)
252  {
253  // Delete the parameters that are already in strcall
254  const parameters newparams = delete_params(params, { "id", "tag" });
255  strcall += maptostr(newparams);
256  }
257 
258  return get(strcall);
259 }
const return_call get(const Mastodon::API::v1 &call)
Make a GET request that doesn't require parameters.
Definition: get.cpp:298
Vector of Mastodon::param, used for passing parameters in calls.
Definition: types.hpp:62
static const string urlencode(const string &str)
Alias for Mastodon::urlencode.
Definition: mastodon-cpp.cpp:638

◆ 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
263 {
264  string strcall = "";
265  string strid = "";
266 
267  // The ID is part of the path
268  const auto &it = params.find("id");
269  if (it != params.end())
270  {
271  strid = it->values[0];
272  }
273 
274  switch (call)
275  {
276  case v2::search:
277  {
278  strcall = "/api/v2/search";
279  break;
280  }
281  default:
282  {
283  ttdebug << "ERROR: Invalid argument.\n";
284  return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" };
285  }
286  }
287 
288  if (params.size() > 0)
289  {
290  // Delete the parameterss that are already in strcall
291  const parameters newparams = delete_params(params, { "id", "tag" });
292  strcall += maptostr(newparams);
293  }
294 
295  return get(strcall);
296 }
const return_call get(const Mastodon::API::v1 &call)
Make a GET request that doesn't require parameters.
Definition: get.cpp:298
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
269 {
270  string headers;
271  _http.get_headers(headers);
272  size_t startpos = headers.find(header + ':');
273  if (startpos != std::string::npos)
274  {
275  startpos = headers.find(':', startpos) + 2;
276  size_t endpos = headers.find("\r\n", startpos);
277  return headers.substr(startpos, endpos - startpos);
278  }
279 
280  return "";
281 }
void get_headers(string &headers) const
Get all headers in a string.
Definition: http.cpp:347

◆ get_instance()

const string API::get_instance ( ) const

Returns the instance.

Returns
The instance.
Since
before 0.11.0
73 {
74  return _instance;
75 }

◆ get_stream() [1/3]

void Mastodon::API::get_stream ( const Mastodon::API::v1 call,
const parameters parameters,
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.

◆ get_stream() [2/3]

void Mastodon::API::get_stream ( const Mastodon::API::v1 call,
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

◆ get_stream() [3/3]

void Mastodon::API::get_stream ( const string &  call,
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
68 {
69  return _useragent;
70 }

◆ 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 { error::INVALID_ARGUMENT, "Invalid argument", 0, "" };
36  break;
37  }
38 
39  return _http.request(http_method::PATCH, strcall, *maptoformdata(params));
40 }

◆ 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
214 {
215  const parameters p;
216  return post(call, p);
217 }
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't require parameters.
Definition: post.cpp:213

◆ 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  case v1::polls_id_votes:
199  {
200  strcall = "/api/v1/polls/" + strid + "/votes";
201  break;
202  }
203  default:
204  {
205  ttdebug << "ERROR: Invalid argument.\n";
206  return { error::INVALID_ARGUMENT, "Invalid argument", 0, ""};
207  }
208  }
209 
210  return post(strcall, params);
211 }
return_call post(const Mastodon::API::v1 &call)
Make a POST request that doesn't require parameters.
Definition: post.cpp:213

◆ 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
220 {
221 
222  return _http.request(http_method::POST, call, *maptoformdata(params));
223 }

◆ 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 { error::INVALID_ARGUMENT, "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
192 {
193  parameters params =
194  {
195  { "client_name", { client_name } },
196  { "redirect_uris", { redirect_uri } },
197  { "scopes", { scopes } },
198  { "website", { website } }
199  };
200 
201  return_call ret = post(API::v1::apps, params);
202 
203  if (ret.error_code == 0)
204  {
205  std::smatch match;
206  std::regex reid("client_id\":\"([^\"]+)\"");
207  std::regex resecret("client_secret\":\"([^\"]+)\"");
208 
209  std::regex_search(ret.answer, match, reid);
210  client_id = match[1].str();
211  std::regex_search(ret.answer, match, resecret);
212  client_secret = match[1].str();
213 
214  url = "https://" + _instance + "/oauth/authorize" +
215  "?scope=" + ::urlencode(scopes) + "&response_type=code" +
216  "&redirect_uri=" + ::urlencode(redirect_uri) +
217  "&client_id=" + client_id;
218  if (!website.empty())
219  {
220  url += "&website=" + ::urlencode(website);
221  }
222  }
223  else if (ret.error_code == 78)
224  {
225  url = ret.answer;
226  }
227  else
228  {
229  std::cerr << "Error code: " << std::to_string(ret.error_code) << '\n';
230  }
231 
232  return ret;
233 }
Return type for API calls.
Definition: return_types.hpp:93
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:100
uint8_t error_code
Error code.
Definition: return_types.hpp:42
return_call post(const Mastodon::API::v1 &call)
Make a POST request that doesn't require parameters.
Definition: post.cpp:213
static const string urlencode(const string &str)
Alias for Mastodon::urlencode.
Definition: mastodon-cpp.cpp:638

◆ 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
240 {
241  parameters params =
242  {
243  { "client_id", { client_id } },
244  { "client_secret", { client_secret } },
245  { "grant_type", { "authorization_code" } },
246  { "redirect_uri", { redirect_uri } },
247  { "code", { code } },
248  };
249 
250  return_call ret = post("/oauth/token", params);
251  if (ret.error_code == 0)
252  {
253  std::smatch match;
254  std::regex retoken("access_token\":\"([^\"]+)\"");
255 
256  std::regex_search(ret.answer, match, retoken);
257  access_token = match[1].str();
258  _access_token = access_token;
259  }
260  else
261  {
262  std::cerr << "Error code: " << std::to_string(ret.error_code) << '\n';
263  }
264 
265  return ret;
266 }
Return type for API calls.
Definition: return_types.hpp:93
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:100
uint8_t error_code
Error code.
Definition: return_types.hpp:42
return_call post(const Mastodon::API::v1 &call)
Make a POST request that doesn't require parameters.
Definition: post.cpp:213

◆ set_proxy()

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

Sets the proxy.

    Both the username and the password will be URL decoded
    before use.
Parameters
hostporthost[:port]
userpwusername[:password] (optional)
Since
0.15.0
295 {
296  _http.set_proxy(hostport, userpw);
297 }
void set_proxy(const string &hostport, const string &userpw="")
Set proxy. Do not call this directly.
Definition: http.cpp:96

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

◆ unescape_html()

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

Alias for Mastodon::unescape_html.

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

◆ urldecode()

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

Alias for Mastodon::urldecode.

Parameters
strThe string
Returns
The decoded string
Since
0.18.0
644 {
645  return Mastodon::urldecode(str);
646 }
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
639 {
640  return Mastodon::urlencode(str);
641 }
const string urlencode(const string &str)
Percent-encodes a string.

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