mastodon-cpp  0.104.0
mastodon-cpp.hpp
1 /* This file is part of mastodon-cpp.
2  * Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef MASTODON_CPP_HPP
18 #define MASTODON_CPP_HPP
19 
20 #include <string>
21 #include <vector>
22 #include <memory>
23 #include <array>
24 #include <mutex>
25 #include <ostream>
26 #include <thread>
27 #include <cstdint>
28 #include <curlpp/cURLpp.hpp>
29 #include <curlpp/Easy.hpp>
30 
31 // If we are compiling mastodon-cpp, use another include path
32 #ifdef MASTODON_CPP
33  #include "return_types.hpp"
34  #include "types.hpp"
35 #else
36  #include <mastodon-cpp/return_types.hpp>
37  #include <mastodon-cpp/types.hpp>
38 #endif
39 
40 using std::string;
41 using std::uint8_t;
42 
52 namespace Mastodon
53 {
76  class API
77  {
78  public:
84  class http
85  {
86  public:
96  explicit http(const API &api, const string &instance,
97  const string &access_token);
98  ~http();
99  return_call request(const http_method &meth, const string &path);
100 
110  return_call request(const http_method &meth,
111  const string &path,
112  const curlpp::Forms &formdata);
113 
122  void request_stream(const string &path, string &stream);
123 
127  void get_headers(string &headers) const;
128 
138  void cancel_stream();
139 
150  std::mutex &get_mutex();
151 
152  private:
153  const API &parent;
154  const string _instance;
155  const string _access_token;
156  string _headers;
157  bool _cancel_stream;
158  std::mutex _mutex;
159  std::thread _streamthread;
160 
161  return_call request_common(const http_method &meth,
162  const string &path,
163  const curlpp::Forms &formdata,
164  string &answer);
165  size_t callback_write(char* data, size_t size, size_t nmemb,
166  string *oss);
167  double callback_progress(double /* dltotal */, double /* dlnow */,
168  double /* ultotal */, double /* ulnow */);
169  };
170 
178  enum class v1
179  {
180  // Mastodon
181  accounts_id,
182  accounts,
183  accounts_verify_credentials,
184  accounts_update_credentials,
185  accounts_id_followers,
186  accounts_id_following,
187  accounts_id_statuses,
188  accounts_id_follow,
189  accounts_id_unfollow,
190  accounts_relationships,
191  accounts_search,
192 
193  apps,
194  apps_verify_credentials,
195 
196  blocks,
197  accounts_id_block,
198  accounts_id_unblock,
199 
200  custom_emojis,
201 
202  domain_blocks,
203 
204  endorsements,
205  accounts_id_pin,
206  accounts_id_unpin,
207 
208  favourites,
209  statuses_id_favourite,
210  statuses_id_unfavourite,
211 
212  filters,
213  filters_id,
214 
215  follow_requests,
216  follow_requests_id_authorize,
217  follow_requests_id_reject,
218 
219  // suggestions,
220  // suggestions_id,
221 
222  instance,
223 
224  lists,
225  accounts_id_lists,
226  lists_id,
227  lists_id_accounts,
228 
229  media,
230  media_id,
231 
232  mutes,
233  accounts_id_mute,
234  accounts_id_unmute,
235  statuses_id_mute,
236  statuses_id_unmute,
237 
238  notifications,
239  notifications_id,
240  notifications_clear,
241  notifications_dismiss,
242  push_subscription,
243 
244  reports,
245 
246  // scheduled_statuses,
247  // scheduled_statuses_id,
248 
249  search,
250 
251  statuses,
252  statuses_id,
253  statuses_id_context,
254  statuses_id_card,
255  statuses_id_reblogged_by,
256  statuses_id_favourited_by,
257  statuses_id_reblog,
258  statuses_id_unreblog,
259  statuses_id_pin,
260  statuses_id_unpin,
261 
262  timelines_home,
263  // timelines_conversations,
264  timelines_public,
265  timelines_tag_hashtag,
266  timelines_list_list_id,
267 
268  streaming_user,
269  streaming_public,
270  streaming_public_local,
271  streaming_hashtag,
272  // streaming_hashtag_local,
273  streaming_list,
274  // streaming_direct,
275 
276  // Glitch-Soc
277  bookmarks,
278  statuses_id_bookmark,
279  statuses_id_unbookmark
280  };
281 
289  enum class v2
290  {
291  search
292  };
293 
305  explicit API(const string &instance, const string &access_token);
306 
312  virtual ~API();
313 
321  void set_useragent(const string &useragent);
322 
330  const string get_useragent() const;
331 
339  const string get_instance() const;
340 
358  static const string urlencode(const string &str);
359 
373  static const string urldecode(const string &str);
374 
390  return_call register_app1(const string &client_name,
391  const string &redirect_uri,
392  const string &scopes,
393  const string &website,
394  string &client_id,
395  string &client_secret,
396  string &url);
397 
398 
412  return_call register_app2(const string &client_id,
413  const string &client_secret,
414  const string &redirect_uri,
415  const string &code,
416  string &access_token);
417 
427  const string get_header(const string &header) const;
428 
441  bool exceptions(const bool &value);
442 
446  bool exceptions() const;
447 
456  static const string unescape_html(const string &html);
457 
469  void set_proxy(const string &proxy, const string &userpw = "");
470 
479  void get_proxy(string &proxy, string &userpw) const;
480 
488  const return_call get(const Mastodon::API::v1 &call);
489 
499  const return_call get(const Mastodon::API::v1 &call,
500  const parameters &parameters);
501 
511  const return_call get(const Mastodon::API::v2 &call,
512  const parameters &parameters);
513 
521  const return_call get(const string &call);
522 
534  void get_stream(const Mastodon::API::v1 &call,
535  const parameters &parameters,
536  std::unique_ptr<Mastodon::API::http> &ptr,
537  string &stream);
538 
548  void get_stream(const Mastodon::API::v1 &call,
549  std::unique_ptr<Mastodon::API::http> &ptr,
550  string &stream);
551 
561  void get_stream(const string &call,
562  std::unique_ptr<Mastodon::API::http> &ptr,
563  string &stream);
564 
576  return_call patch(const Mastodon::API::v1 &call,
577  const parameters &parameters);
578 
586  return_call post(const Mastodon::API::v1 &call);
587 
599  return_call post(const Mastodon::API::v1 &call,
600  const parameters &parameters);
601 
613  return_call post(const string &call,
614  const parameters &parameters);
615 
625  return_call put(const Mastodon::API::v1 &call,
626  const parameters &parameters);
627 
637  return_call put(const string &call, const parameters &parameters);
638 
648  return_call del(const Mastodon::API::v1 &call,
649  const parameters &parameters);
650 
660  return_call del(const string &call, const parameters &parameters);
661 
662  private:
663  const string _instance;
664  string _access_token;
665  string _useragent;
666  http _http;
667  bool _exceptions;
668  string _proxy;
669  string _proxy_userpw;
670 
679  const string maptostr(const parameters &map,
680  const bool &firstparam = true);
681 
689  const curlpp::Forms maptoformdata(const parameters &map);
690 
701  const parameters delete_params(const parameters &params,
702  const vector<string> &keys);
703  };
704 }
705 
706 #endif
void set_proxy(const string &proxy, const string &userpw="")
Sets the proxy.
Definition: mastodon-cpp.cpp:566
void get_proxy(string &proxy, string &userpw) const
For internal use.
Definition: mastodon-cpp.cpp:572
Interface to the Mastodon API.
Definition: mastodon-cpp.hpp:76
static const string urlencode(const string &str)
Percent-encodes a string. This is done automatically, unless you make a custom request.
Definition: mastodon-cpp.cpp:146
void get_headers(string &headers) const
Get all headers in a string.
Definition: http.cpp:227
static const string urldecode(const string &str)
Decodes a percent-encoded string.
Definition: mastodon-cpp.cpp:151
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.
Definition: mastodon-cpp.cpp:206
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.
Definition: mastodon-cpp.cpp:156
v1
A list of all v1 API calls.
Definition: mastodon-cpp.hpp:178
Return type for API calls.
Definition: return_types.hpp:92
return_call patch(const Mastodon::API::v1 &call, const parameters &parameters)
Make a PATCH request.
Definition: patch.cpp:24
v2
A list of all v2 API calls.
Definition: mastodon-cpp.hpp:289
Vector of Mastodon::param, used for passing parameters in calls.
Definition: types.hpp:62
const string get_header(const string &header) const
Gets the header from the last answer.
Definition: mastodon-cpp.cpp:239
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
http_method
HTTP methods. Used in API calls.
Definition: types.hpp:87
virtual ~API()
Destroys the object.
Definition: mastodon-cpp.cpp:40
void request_stream(const string &path, string &stream)
HTTP Request for streams.
Definition: http.cpp:60
Mastodon::parameters parameters
Vector of Mastodon::param, used for passing parameters in calls.
API(const string &instance, const string &access_token)
Constructs a new API object.
Definition: mastodon-cpp.cpp:30
return_call post(const Mastodon::API::v1 &call)
Make a POST request which doesn&#39;t require parameters.
Definition: post.cpp:208
void cancel_stream()
Cancels the stream. Use only with streams.
Definition: http.cpp:252
bool exceptions() const
Returns true if exceptions are turned on, false otherwise.
Definition: mastodon-cpp.cpp:260
return_call del(const Mastodon::API::v1 &call, const parameters &parameters)
Make a DELETE request which requires parameters.
Definition: delete.cpp:23
return_call put(const Mastodon::API::v1 &call, const parameters &parameters)
Make a PUT request which requires a parameters.
Definition: put.cpp:23
void set_useragent(const string &useragent)
Sets the useragent. Default is mastodon-cpp/version.
Definition: mastodon-cpp.cpp:43
Collection of things to interface with server software that implements the Mastodon API...
Definition: mastodon-cpp.hpp:52
static const string unescape_html(const string &html)
Replaces HTML entities with UTF-8 characters.
Definition: mastodon-cpp.cpp:265
const string get_instance() const
Returns the instance.
Definition: mastodon-cpp.cpp:53
http(const API &api, const string &instance, const string &access_token)
Constructs new http object.
Definition: http.cpp:33
http class. Do not use this directly.
Definition: mastodon-cpp.hpp:84
const string get_useragent() const
Gets the useragent.
Definition: mastodon-cpp.cpp:48
std::mutex & get_mutex()
Gets the mutex guarding the string that is written to.
Definition: http.cpp:258