mastodon-cpp  0.110.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 &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...
 

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_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...
 
- 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
96 {
97  return Link(get_header("Link"));
98 }
const string get_header(const string &header) const
Gets the header from the last answer.
Definition: mastodon-cpp.cpp:268

◆ 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
137 {
138  parameters params;
139 
140  params.push_back({ "limit", { std::to_string(limit) } });
141  if (!since_id.empty())
142  {
143  params.push_back({ "since_id", { since_id } });
144  }
145  if (!max_id.empty())
146  {
147  params.push_back({ "max_id", { max_id } });
148  }
149 
150  return_call ret = API::get(API::v1::notifications, params);
151 
152  if (ret.error_code == 0)
153  {
154  const vector<string> &answer_v = json_array_to_vector(ret.answer);
155  vector<Notification> notifications;
156  notifications.resize(answer_v.size());
157 
158  // Transform vector of strings to vector of Notification.
159  std::transform(answer_v.begin(), answer_v.end(), notifications.begin(),
160  [](const string &s)
161  { return Notification(s); });
162 
163  return { ret.error_code, ret.error_message, ret.http_error_code,
164  notifications };
165  }
166  else
167  {
168  ttdebug << "ERROR: Could not get notifications.\n";
169  return { ret.error_code, ret.error_message, ret.http_error_code, {} };
170  }
171 }
const return_call get(const Mastodon::API::v1 &call)
Make a GET request that doesn't require parameters.
Definition: get.cpp:298
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 { error::INVALID_ARGUMENT,
43  "Easy::Status::content can not be empty", 0, Status() };
44  }
45 
46  if (!status.in_reply_to_id().empty())
47  {
48  params.push_back({ "in_reply_to_id",
49  { status.in_reply_to_id() }});
50  }
51  if (status.sensitive())
52  {
53  params.push_back({ "sensitive", { "true" }});
54  }
55  if (!status.spoiler_text().empty())
56  {
57  params.push_back({ "spoiler_text", { status.spoiler_text() }});
58  }
59  if (status.visibility() != visibility_type::Undefined)
60  {
61  string visibility;
62  switch (status.visibility())
63  {
64  case visibility_type::Direct:
65  visibility = "direct";
66  break;
67  case visibility_type::Private:
68  visibility = "private";
69  break;
70  case visibility_type::Unlisted:
71  visibility = "unlisted";
72  break;
73  case visibility_type::Public:
74  visibility = "public";
75  break;
76  default:
77  break;
78  };
79  params.push_back({ "visibility", { visibility }});
80  }
81  if (!status.language().empty())
82  {
83  params.push_back({ "language", { status.language() }});
84  }
85  if (!status.media_attachments().empty())
86  {
87  std::vector<string> media_ids;
88  for (const Attachment &att : status.media_attachments())
89  {
90  parameters param_att;
91  if (!att.file().empty())
92  {
93  param_att.push_back({ "file", { att.file() }});
94  }
95  else
96  {
97  ttdebug << "ERROR: Easy::Attachment::file can not be empty.\n";
98  return { error::INVALID_ARGUMENT,
99  "Easy::Attachment::file can not be empty", 0,
100  Status() };
101  }
102  if (!att.description().empty())
103  {
104  param_att.push_back({ "description", { att.description() }});
105  }
106  if (!att.focus().empty())
107  {
108  param_att.push_back({ "focus",
109  { std::to_string(att.focus()[0]) + ',' +
110  std::to_string(att.focus()[1]) }});
111  }
112 
113  return_call ret = post(API::v1::media, param_att);
114  if (ret.error_code == 0)
115  {
116  Attachment attachment(ret.answer);
117  media_ids.push_back(attachment.id());
118  }
119  else
120  {
121  ttdebug << "ERROR: Could not upload file.\n";
122  return { ret.error_code, ret.error_message,
123  ret.http_error_code, Status(ret.answer) };
124  }
125  }
126 
127  params.push_back({ "media_ids", media_ids });
128  }
129 
130  return_call ret = post(API::v1::statuses, params);
131  return { ret.error_code, ret.error_message, ret.http_error_code,
132  Status(ret.answer) };
133 }
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't require parameters.
Definition: post.cpp:213
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: