mastodon-cpp  0.106.0
Public Member Functions | List of all members
Mastodon::Easy::API Class Reference

Child of Mastodon::API with abstract methods. More...

#include <easy.hpp>

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

Public Member Functions

 API (const string &instance, const string &access_token)
 Constructs a new Easy object. More...
 
const Link get_link () const
 Gets the links from the last answer. More...
 
const return_entity< Easy::Statussend_post (const Status &status)
 Sends a post. More...
 
const return_entity< Easy::Statussend_toot (const Status &status)
 Alias for send_post() More...
 
const return_entity_vector< Easy::Notificationget_notifications (const uint16_t limit=20, const string since_id="", const string max_id="")
 Gets notifications. More...
 
- Public Member Functions inherited from Mastodon::API
 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...
 

Additional Inherited Members

- Public Types inherited from Mastodon::API
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...
 
- Static Public Member Functions inherited from Mastodon::API
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

Child of Mastodon::API with abstract methods.

Provides convenient functions to deal with the responses you get.

Examples:
example01_get_public_timeline.cpp, and example02_stream.cpp.

Constructor & Destructor Documentation

◆ API()

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

Constructs a new Easy object.

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

Parameters
instanceThe hostname of your instance
access_tokenThe access token
Since
0.100.0
28  : Mastodon::API(instance, access_token)
29 {}
Interface to the Mastodon API.
Definition: mastodon-cpp.hpp:69

Member Function Documentation

◆ get_link()

const Easy::Link Easy::API::get_link ( ) const

Gets the links from the last answer.

Since
0.100.0
93 {
94  return Link(get_header("Link"));
95 }
const string get_header(const string &header) const
Gets the header from the last answer.
Definition: mastodon-cpp.cpp:259

◆ get_notifications()

const return_entity_vector< Notification > API::get_notifications ( const uint16_t  limit = 20,
const string  since_id = "",
const string  max_id = "" 
)

Gets notifications.

Parameters
limitMaximum number of notifications
since_idReturn notifications newer than ID
max_idReturn notifications older than ID
Returns
vector of Easy::Notification.
Since
0.100.0
135 {
136  parameters params;
137 
138  params.push_back({ "limit", { std::to_string(limit) } });
139  if (!since_id.empty())
140  {
141  params.push_back({ "since_id", { since_id } });
142  }
143  if (!max_id.empty())
144  {
145  params.push_back({ "max_id", { max_id } });
146  }
147 
148  return_call ret = API::get(API::v1::notifications, params);
149 
150  if (ret.error_code == 0)
151  {
152  const vector<string> &answer_v = json_array_to_vector(ret.answer);
153  vector<Notification> notifications;
154  notifications.resize(answer_v.size());
155 
156  // Transform vector of strings to vector of Notification.
157  std::transform(answer_v.begin(), answer_v.end(), notifications.begin(),
158  [](const string &s)
159  { return Notification(s); });
160 
161  return { ret.error_code, ret.error_message, ret.http_error_code,
162  notifications };
163  }
164  else
165  {
166  ttdebug << "ERROR: Could not get notifications.\n";
167  return { ret.error_code, ret.error_message, ret.http_error_code, {} };
168  }
169 }
const return_call get(const Mastodon::API::v1 &call)
Make a GET request that doesn&#39;t require parameters.
Definition: get.cpp:293
Mastodon::return_call return_call
Return type for API calls.
Class to hold notifications.
Definition: notification.hpp:40
Mastodon::parameters parameters
Vector of Mastodon::param, used for passing parameters in calls.
const vector< string > json_array_to_vector(const string &json)
Turns a JSON array into a vector of strings.
Definition: easy.cpp:31

◆ send_post()

const return_entity< Status > API::send_post ( const Status status)

Sends a post.

Parameters
statusThe status to send
Returns
The new Easy::Status
Since
0.100.0
32 {
33  parameters params;
34 
35  if (!status.content().empty())
36  {
37  params.push_back({ "status", { status.content() }});
38  }
39  else
40  {
41  ttdebug << "ERROR: Easy::Status::content can not be empty.\n";
42  return { 22, "Easy::Status::content can not be empty", 0, Status() };
43  }
44 
45  if (!status.in_reply_to_id().empty())
46  {
47  params.push_back({ "in_reply_to_id",
48  { status.in_reply_to_id() }});
49  }
50  if (status.sensitive())
51  {
52  params.push_back({ "sensitive", { "true" }});
53  }
54  if (!status.spoiler_text().empty())
55  {
56  params.push_back({ "spoiler_text", { status.spoiler_text() }});
57  }
58  if (status.visibility() != visibility_type::Undefined)
59  {
60  string visibility;
61  switch (status.visibility())
62  {
63  case visibility_type::Direct:
64  visibility = "direct";
65  break;
66  case visibility_type::Private:
67  visibility = "private";
68  break;
69  case visibility_type::Unlisted:
70  visibility = "unlisted";
71  break;
72  case visibility_type::Public:
73  visibility = "public";
74  break;
75  default:
76  break;
77  };
78  params.push_back({ "visibility", { visibility }});
79  }
80  if (!status.language().empty())
81  {
82  params.push_back({ "language", { status.language() }});
83  }
84  if (!status.media_attachments().empty())
85  {
86  std::vector<string> media_ids;
87  for (const Attachment &att : status.media_attachments())
88  {
89  parameters param_att;
90  if (!att.file().empty())
91  {
92  param_att.push_back({ "file", { att.file() }});
93  }
94  else
95  {
96  ttdebug << "ERROR: Easy::Attachment::file can not be empty.\n";
97  return { 22, "Easy::Attachment::file can not be empty",
98  0, Status() };
99  }
100  if (!att.description().empty())
101  {
102  param_att.push_back({ "description", { att.description() }});
103  }
104  if (!att.focus().empty())
105  {
106  param_att.push_back({ "focus",
107  { std::to_string(att.focus()[0]) + ',' +
108  std::to_string(att.focus()[1]) }});
109  }
110 
111  return_call ret = post(API::v1::media, param_att);
112  if (ret.error_code == 0)
113  {
114  Attachment attachment(ret.answer);
115  media_ids.push_back(attachment.id());
116  }
117  else
118  {
119  ttdebug << "ERROR: Could not upload file.\n";
120  return { ret.error_code, ret.error_message,
121  ret.http_error_code, Status(ret.answer) };
122  }
123  }
124 
125  params.push_back({ "media_ids", media_ids });
126  }
127 
128  return_call ret = post(API::v1::statuses, params);
129  return { ret.error_code, ret.error_message, ret.http_error_code,
130  Status(ret.answer) };
131 }
Mastodon::return_call return_call
Return type for API calls.
bool sensitive() const
Returns true if the attachments should be hidden by default.
Definition: status.cpp:233
Class to hold attachments.
Definition: attachment.hpp:40
Mastodon::parameters parameters
Vector of Mastodon::param, used for passing parameters in calls.
return_call post(const Mastodon::API::v1 &call)
Make a POST request that doesn&#39;t require parameters.
Definition: post.cpp:208
const std::vector< Attachment > media_attachments() const
Returns the attachments.
Definition: status.cpp:154
const string language() const
Returns the language of the status.
Definition: status.cpp:143
Class to hold statuses.
Definition: status.hpp:46
visibility_type visibility() const
Returns the visibility of the status.
Definition: status.cpp:280
const string content() const
Returns content of status.
Definition: status.cpp:86
const string spoiler_text() const
Returns the spoiler text.
Definition: status.cpp:244
const string in_reply_to_id() const
Returns the ID of the status it replies to.
Definition: status.cpp:127

◆ send_toot()

const return_entity< Status > API::send_toot ( const Status status)

Alias for send_post()

Since
0.100.0
27 {
28  return send_post(status);
29 }
const return_entity< Easy::Status > send_post(const Status &status)
Sends a post.
Definition: simple_calls.cpp:31

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