mastodon-cpp  0.30.0
mastodon-cpp.hpp
1 /* This file is part of mastodon-cpp.
2  * Copyright © 2018 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 <cstdint>
23 #include <map>
24 #include <memory>
25 #include <array>
26 #include <mutex>
27 #include <curlpp/cURLpp.hpp>
28 #include <curlpp/Easy.hpp>
29 
30 using std::uint_fast16_t;
31 using std::string;
32 
52 namespace Mastodon
53 {
77 class API
78 {
79 public:
85  class http
86  {
87  public:
91  enum class method
92  {
93  GET,
94  PATCH,
95  POST,
96  PUT,
97  DELETE,
98  GET_STREAM
99  };
100 
101  explicit http(const API &api, const string &instance,
102  const string &access_token);
103  ~http();
104  uint_fast16_t request(const method &meth,
105  const string &path,
106  string &answer);
107 
121  uint_fast16_t request(const method &meth,
122  const string &path,
123  const curlpp::Forms &formdata,
124  string &answer);
125 
129  void get_headers(string &headers) const;
130 
141  void cancel_stream();
142 
143  [[deprecated("Will vanish in 1.0.0. Use cancel_stream() instead.")]]
144  void abort_stream();
145 
156  std::mutex &get_mutex();
157 
158  private:
159  const API &parent;
160  const string _instance;
161  const string _access_token;
162  string _headers;
163  bool _cancel_stream;
164  std::mutex _mutex;
165 
166  size_t callback_write(char* data, size_t size, size_t nmemb,
167  string *oss);
168  [[deprecated("Will vanish in 1.0.0. Use callback_write() instead.")]]
169  size_t callback(char* data, size_t size, size_t nmemb,
170  string *oss);
171  double callback_progress(double /* dltotal */, double /* dlnow */,
172  double /* ultotal */, double /* ulnow */);
173  };
174 
189  typedef std::map<string, std::vector<string>> parametermap;
190 
198  enum class v1
199  {
200  accounts_id,
201  accounts_verify_credentials,
202  accounts_id_followers,
203  accounts_id_following,
204  accounts_id_statuses,
205  accounts_relationships,
206  accounts_search,
207  blocks,
208  domain_blocks,
209  endorsements,
210  favourites,
211  follow_requests,
212  instance,
213  custom_emojis,
214  lists,
215  accounts_id_lists,
216  lists_id_accounts,
217  lists_id,
218  mutes,
219  notifications,
220  notifications_id,
221  reports,
222  search,
223  statuses_id,
224  statuses_id_context,
225  statuses_id_card,
226  statuses_id_reblogged_by,
227  statuses_id_favourited_by,
228  timelines_home,
229  timelines_public,
230  timelines_tag_hashtag,
231  timelines_list_list_id,
232  // PATCH
233  accounts_update_credentials,
234  // POST
235  accounts_id_follow,
236  accounts_id_unfollow,
237  accounts_id_block,
238  accounts_id_unblock,
239  accounts_id_mute,
240  accounts_id_unmute,
241  accounts_id_pin,
242  accounts_id_unpin,
243  apps,
244  follow_requests_id_authorize,
245  follow_requests_id_reject,
246  follows,
247  media,
248  notifications_clear,
249  notifications_dismiss,
250  statuses,
251  statuses_id_reblog,
252  statuses_id_unreblog,
253  statuses_id_favourite,
254  statuses_id_unfavourite,
255  statuses_id_pin,
256  statuses_id_unpin,
257  statuses_id_mute,
258  statuses_id_unmute,
259  // PUT
260  media_id,
261  // Streaming
262  streaming_user,
263  streaming_public,
264  streaming_public_local,
265  streaming_hashtag,
266  streaming_list,
267  // Push
268  push_subscription,
269  // Glitch-Soc
270  bookmarks,
271  statuses_id_bookmark,
272  statuses_id_unbookmark
273  };
274 
282  enum class v2
283  {
284  search
285  };
286 
298  explicit API(const string &instance, const string &access_token);
299 
307  void set_useragent(const string &useragent);
308 
316  const string get_useragent() const;
317 
325  const string get_instance() const;
326 
344  static const string urlencode(const string &str);
345 
359  static const string urldecode(const string &str);
360 
378  uint_fast16_t register_app1(const string &client_name,
379  const string &redirect_uri,
380  const string &scopes,
381  const string &website,
382  string &client_id,
383  string &client_secret,
384  string &url);
385 
386  [[deprecated("Will vanish in 1.0.0")]]
387  uint_fast16_t register_app1(const string &instance,
388  const string &client_name,
389  const string &redirect_uri,
390  const string &scopes,
391  const string &website,
392  string &client_id,
393  string &client_secret,
394  string &url);
395 
411  uint_fast16_t register_app2(const string &client_id,
412  const string &client_secret,
413  const string &redirect_uri,
414  const string &code,
415  string &access_token);
416 
417  [[deprecated("Will vanish in 1.0.0")]]
418  uint_fast16_t register_app2(const string &instance,
419  const string &client_id,
420  const string &client_secret,
421  const string &redirect_uri,
422  const string &code,
423  string &access_token);
424 
434  const string get_header(const string &header) const;
435 
448  bool exceptions(const bool &value);
449 
453  bool exceptions() const;
454 
462  static const string unescape_html(const string &html);
463 
475  void set_proxy(const string &proxy, const string &userpw = "");
476 
485  void get_proxy(string &proxy, string &userpw) const;
486 
498  uint_fast16_t get(const Mastodon::API::v1 &call, string &answer);
499 
511  uint_fast16_t get(const Mastodon::API::v1 &call,
512  const parametermap &parameters,
513  string &answer);
514 
520  uint_fast16_t get(const Mastodon::API::v2 &call,
521  const parametermap &parameters,
522  string &answer);
523 
536  uint_fast16_t get(const string &call, string &answer);
537 
538  [[deprecated("Will vanish in 1.0.0, use get() without string argument "
539  "instead.")]]
540  uint_fast16_t get(const Mastodon::API::v1 &call,
541  const string &argument,
542  string &answer);
543 
544  [[deprecated("Will vanish in 1.0.0, use get() without string argument "
545  "instead.")]]
546  uint_fast16_t get(const Mastodon::API::v1 &call,
547  const string &argument,
548  const parametermap &parameters,
549  string &answer);
550 
565  uint_fast16_t get_stream(const Mastodon::API::v1 &call,
566  const parametermap &parameters,
567  string &answer,
568  std::unique_ptr<Mastodon::API::http> &ptr);
569 
583  uint_fast16_t get_stream(const Mastodon::API::v1 &call,
584  string &answer,
585  std::unique_ptr<Mastodon::API::http> &ptr);
586 
601  uint_fast16_t get_stream(const string &call,
602  string &answer,
603  std::unique_ptr<Mastodon::API::http> &ptr);
604 
605  [[deprecated("Will vanish in 1.0.0, use get_stream() without string "
606  "argument instead.")]]
607  uint_fast16_t get_stream(const Mastodon::API::v1 &call,
608  const string &argument,
609  string &answer,
610  std::unique_ptr<Mastodon::API::http> &ptr);
611 
627  uint_fast16_t patch(const Mastodon::API::v1 &call,
628  const parametermap &parameters,
629  string &answer);
630 
643  uint_fast16_t post(const Mastodon::API::v1 &call, string &answer);
644 
660  uint_fast16_t post(const Mastodon::API::v1 &call,
661  const parametermap &parameters,
662  string &answer);
663 
679  uint_fast16_t post(const string &call,
680  const parametermap &parameters,
681  string &answer);
682 
683  [[deprecated("Will vanish in 1.0.0, use post() without string argument "
684  "instead.")]]
685  uint_fast16_t post(const Mastodon::API::v1 &call,
686  const string &argument,
687  string &answer);
688 
689  [[deprecated("Will vanish in 1.0.0, use post() without string argument "
690  "instead.")]]
691  uint_fast16_t post(const Mastodon::API::v1 &call,
692  const string &argument,
693  const parametermap &parameters,
694  string &answer);
695 
710  uint_fast16_t put(const Mastodon::API::v1 &call,
711  const parametermap &parameters,
712  string &answer);
713 
728  uint_fast16_t put(const string &call,
729  const parametermap &parameters,
730  string &answer);
731 
732  [[deprecated("Will vanish in 1.0.0, use put() without string argument "
733  "instead.")]]
734  uint_fast16_t put(const Mastodon::API::v1 &call,
735  const string &argument,
736  const parametermap &parameters,
737  string &answer);
738 
749  uint_fast16_t del(const Mastodon::API::v1 &call,
750  const parametermap &parameters);
751 
765  uint_fast16_t del(const string &call,
766  const parametermap &parameters,
767  string &answer);
768 
769  [[deprecated("Will vanish in 1.0.0, use del() without string argument "
770  "instead.")]]
771  uint_fast16_t del(const Mastodon::API::v1 &call,
772  const string &argument);
773 
774  [[deprecated("Will vanish in 1.0.0, use del() without string argument "
775  "instead.")]]
776  uint_fast16_t del(const Mastodon::API::v1 &call,
777  const string &argument,
778  const parametermap &parameters);
779 
780 private:
781  const string _instance;
782  string _access_token;
783  string _useragent;
784  http _http;
785  bool _exceptions;
786  string _proxy;
787  string _proxy_userpw;
788 
797  const string maptostr(const parametermap &map,
798  const bool &firstparam = true);
799 
807  const curlpp::Forms maptoformdata(const parametermap &map);
808 };
809 }
810 
811 #endif
void set_proxy(const string &proxy, const string &userpw="")
Sets the proxy.
Definition: mastodon-cpp.cpp:589
void get_proxy(string &proxy, string &userpw) const
For internal use.
Definition: mastodon-cpp.cpp:595
Class for the Mastodon API.
Definition: mastodon-cpp.hpp:77
uint_fast16_t 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:163
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:141
void get_headers(string &headers) const
Get all headers in a string.
Definition: http.cpp:199
static const string urldecode(const string &str)
Decodes a percent-encoded string.
Definition: mastodon-cpp.cpp:146
v1
A list of all v1 API calls.
Definition: mastodon-cpp.hpp:198
uint_fast16_t del(const Mastodon::API::v1 &call, const parametermap &parameters)
Make a DELETE request which requires parameters.
Definition: delete.cpp:23
v2
A list of all v2 API calls.
Definition: mastodon-cpp.hpp:282
const string get_header(const string &header) const
Gets the header from the last answer.
Definition: mastodon-cpp.cpp:262
uint_fast16_t patch(const Mastodon::API::v1 &call, const parametermap &parameters, string &answer)
Make a PATCH request.
Definition: patch.cpp:24
API(const string &instance, const string &access_token)
Constructs a new API object.
Definition: mastodon-cpp.cpp:30
uint_fast16_t post(const Mastodon::API::v1 &call, string &answer)
Make a POST request which doesn&#39;t require parameters.
Definition: post.cpp:140
method
HTTP methods.
Definition: mastodon-cpp.hpp:91
void cancel_stream()
Cancels the stream. Use only with streams.
Definition: http.cpp:230
uint_fast16_t put(const Mastodon::API::v1 &call, const parametermap &parameters, string &answer)
Make a PUT request which requires a parameters.
Definition: put.cpp:23
bool exceptions() const
Returns true if exceptions are turned on, false otherwise.
Definition: mastodon-cpp.cpp:283
uint_fast16_t 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:227
uint_fast16_t get_stream(const Mastodon::API::v1 &call, const parametermap &parameters, string &answer, std::unique_ptr< Mastodon::API::http > &ptr)
Make a streaming GET request.
Definition: get_stream.cpp:24
void set_useragent(const string &useragent)
Sets the useragent. Default is mastodon-cpp/version.
Definition: mastodon-cpp.cpp:42
Definition: mastodon-cpp.hpp:52
std::map< string, std::vector< string > > parametermap
Used for passing parameters.
Definition: mastodon-cpp.hpp:189
static const string unescape_html(const string &html)
Replaces HTML entities with UTF-8 characters.
Definition: mastodon-cpp.cpp:288
const string get_instance() const
Returns the instance.
Definition: mastodon-cpp.cpp:52
http class. Do not use this directly.
Definition: mastodon-cpp.hpp:85
const string get_useragent() const
Gets the useragent.
Definition: mastodon-cpp.cpp:47
std::mutex & get_mutex()
Gets the mutex guarding the string that is written to.
Definition: http.cpp:240