mastodonpp  0.5.5
instance.hpp
1 /* This file is part of mastodonpp.
2  * Copyright © 2020 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 Affero 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 Affero General Public License for more details.
12  *
13  * You should have received a copy of the GNU Affero General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef MASTODONPP_INSTANCE_HPP
18 #define MASTODONPP_INSTANCE_HPP
19 
20 #include "curl_wrapper.hpp"
21 #include "types.hpp"
22 
23 #include <cstdint>
24 #include <string>
25 #include <string_view>
26 #include <utility>
27 #include <vector>
28 
29 namespace mastodonpp
30 {
31 
32 using std::uint64_t;
33 using std::string;
34 using std::string_view;
35 using std::vector;
36 
48 class Instance : public CURLWrapper
49 {
50 public:
59  explicit Instance(string_view hostname, string_view access_token);
60 
66  Instance(const Instance &other);
67 
69  Instance(Instance &&other) noexcept = delete;
70 
72  ~Instance() noexcept override = default;
73 
75  Instance& operator=(const Instance &other) = delete;
76 
78  Instance& operator=(Instance &&other) noexcept = delete;
79 
90  inline void copy_connection_properties(CURLWrapper &curlwrapper) const
91  {
92  curlwrapper.setup_connection_properties(_proxy, _access_token, _cainfo,
93  _useragent);
94  }
95 
101  [[nodiscard]]
102  inline string_view get_hostname() const noexcept
103  {
104  return _hostname;
105  }
106 
114  [[nodiscard]]
115  inline string_view get_baseuri() const noexcept
116  {
117  return _baseuri;
118  }
119 
125  [[nodiscard]]
126  inline string_view get_access_token() const noexcept
127  {
128  return _access_token;
129  }
130 
139  inline void set_access_token(const string_view access_token)
140  {
141  _access_token = access_token;
142  CURLWrapper::set_access_token(access_token);
143  }
144 
156  [[nodiscard]]
157  uint64_t get_max_chars() noexcept;
158 
164  void set_proxy(const string_view proxy) override
165  {
166  _proxy = proxy;
167  CURLWrapper::set_proxy(proxy);
168  }
169 
179  [[nodiscard]]
181 
193  vector<string> get_post_formats() noexcept;
194 
203  void set_cainfo(string_view path) override
204  {
205  _cainfo = path;
207  }
208 
217  void set_useragent(const string_view useragent) override
218  {
219  _useragent = useragent;
220  CURLWrapper::set_useragent(useragent);
221  }
222 
253  class ObtainToken : public CURLWrapper
254  {
255  public:
261  explicit ObtainToken(Instance &instance)
262  : _instance{instance}
263  , _baseuri{instance.get_baseuri()}
264  {
265  _instance.copy_connection_properties(*this);
266  }
267 
287  [[nodiscard]]
288  answer_type step_1(string_view client_name, string_view scopes,
289  string_view website);
290 
306  [[nodiscard]]
307  answer_type step_2(string_view code);
308 
309  private:
310  Instance &_instance;
311  const string _baseuri;
312  string _scopes;
313  string _client_id;
314  string _client_secret;
315  };
316 
317 private:
318  const string _hostname;
319  const string _baseuri;
320  string _access_token;
321  uint64_t _max_chars;
322  string _proxy;
323  vector<string> _post_formats;
324  string _cainfo;
325  string _useragent;
326 };
327 
328 } // namespace mastodonpp
329 
330 #endif // MASTODONPP_INSTANCE_HPP
mastodonpp::Instance::~Instance
~Instance() noexcept override=default
Destructor.
mastodonpp::Instance::ObtainToken
Simplifies obtaining an OAuth 2.0 Bearer Access Token.
Definition: instance.hpp:253
mastodonpp::Instance::copy_connection_properties
void copy_connection_properties(CURLWrapper &curlwrapper) const
Set the properties of the connection of the calling class up.
Definition: instance.hpp:90
mastodonpp::Instance::get_nodeinfo
answer_type get_nodeinfo()
Returns the NodeInfo of the instance.
mastodonpp::Instance::operator=
Instance & operator=(const Instance &other)=delete
Copy assignment operator.
mastodonpp::Instance::get_hostname
string_view get_hostname() const noexcept
Returns the hostname.
Definition: instance.hpp:102
mastodonpp::Instance::ObtainToken::step_1
answer_type step_1(string_view client_name, string_view scopes, string_view website)
Creates an application via /api/v1/apps.
mastodonpp::Instance::get_baseuri
string_view get_baseuri() const noexcept
Returns the base URI.
Definition: instance.hpp:115
mastodonpp
C++ wrapper for the Mastodon API.
Definition: api.cpp:19
mastodonpp::Instance::ObtainToken::step_2
answer_type step_2(string_view code)
Creates a token via /oauth/token.
mastodonpp::Instance::set_cainfo
void set_cainfo(string_view path) override
Set path to Certificate Authority (CA) bundle.
Definition: instance.hpp:203
mastodonpp::Instance::Instance
Instance(string_view hostname, string_view access_token)
Construct a new Instance object.
mastodonpp::CURLWrapper::set_proxy
virtual void set_proxy(string_view proxy)
Set the proxy to use.
Definition: curl_wrapper.cpp:234
mastodonpp::Instance::get_post_formats
vector< string > get_post_formats() noexcept
Returns the allowed mime types for statuses.
mastodonpp::Instance::get_max_chars
uint64_t get_max_chars() noexcept
Returns the maximum number of characters per post.
mastodonpp::answer_type
Return type for Requests.
Definition: types.hpp:79
mastodonpp::CURLWrapper::set_cainfo
virtual void set_cainfo(string_view path)
Set path to Certificate Authority (CA) bundle.
Definition: curl_wrapper.cpp:271
mastodonpp::Instance
Holds the access data of an instance.
Definition: instance.hpp:48
mastodonpp::Instance::set_useragent
void set_useragent(const string_view useragent) override
Sets the User-Agent.
Definition: instance.hpp:217
mastodonpp::CURLWrapper::set_useragent
virtual void set_useragent(string_view useragent)
Sets the User-Agent.
Definition: curl_wrapper.cpp:281
mastodonpp::Instance::ObtainToken::ObtainToken
ObtainToken(Instance &instance)
Constructor.
Definition: instance.hpp:261
mastodonpp::CURLWrapper
Handles the details of network connections.
Definition: curl_wrapper.hpp:58
mastodonpp::Instance::set_access_token
void set_access_token(const string_view access_token)
Set OAuth 2.0 Bearer Access Token.
Definition: instance.hpp:139
mastodonpp::Instance::get_access_token
string_view get_access_token() const noexcept
Returns the access token.
Definition: instance.hpp:126
mastodonpp::Instance::set_proxy
void set_proxy(const string_view proxy) override
Set the proxy to use.
Definition: instance.hpp:164
mastodonpp::CURLWrapper::set_access_token
void set_access_token(string_view access_token)
Set OAuth 2.0 Bearer Access Token.
Definition: curl_wrapper.cpp:245
mastodonpp::CURLWrapper::setup_connection_properties
void setup_connection_properties(string_view proxy, string_view access_token, string_view cainfo, string_view useragent)
Set some properties of the connection.
Definition: curl_wrapper.cpp:208