mastodonpp  0.5.4
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::move;
36 using std::vector;
37 
49 class Instance : public CURLWrapper
50 {
51 public:
60  explicit Instance(string_view hostname, string_view access_token);
61 
67  Instance(const Instance &other);
68 
70  Instance(Instance &&other) noexcept = delete;
71 
73  ~Instance() noexcept override = default;
74 
76  Instance& operator=(const Instance &other) = delete;
77 
79  Instance& operator=(Instance &&other) noexcept = delete;
80 
91  inline void copy_connection_properties(CURLWrapper &curlwrapper) const
92  {
93  curlwrapper.setup_connection_properties(_proxy, _access_token, _cainfo,
94  _useragent);
95  }
96 
102  [[nodiscard]]
103  inline string_view get_hostname() const noexcept
104  {
105  return _hostname;
106  }
107 
115  [[nodiscard]]
116  inline string_view get_baseuri() const noexcept
117  {
118  return _baseuri;
119  }
120 
126  [[nodiscard]]
127  inline string_view get_access_token() const noexcept
128  {
129  return _access_token;
130  }
131 
140  inline void set_access_token(const string_view access_token)
141  {
142  _access_token = access_token;
143  CURLWrapper::set_access_token(access_token);
144  }
145 
157  [[nodiscard]]
158  uint64_t get_max_chars() noexcept;
159 
165  void set_proxy(const string_view proxy) override
166  {
167  _proxy = proxy;
168  CURLWrapper::set_proxy(proxy);
169  }
170 
180  [[nodiscard]]
182 
194  vector<string> get_post_formats() noexcept;
195 
204  void set_cainfo(string_view path) override
205  {
206  _cainfo = path;
208  }
209 
218  void set_useragent(const string_view useragent) override
219  {
220  _useragent = useragent;
221  CURLWrapper::set_useragent(useragent);
222  }
223 
254  class ObtainToken : public CURLWrapper
255  {
256  public:
262  explicit ObtainToken(Instance &instance)
263  : _instance{instance}
264  , _baseuri{instance.get_baseuri()}
265  {
266  _instance.copy_connection_properties(*this);
267  }
268 
285  [[nodiscard]]
286  answer_type step_1(string_view client_name, string_view scopes,
287  string_view website);
288 
304  [[nodiscard]]
305  answer_type step_2(string_view code);
306 
307  private:
308  Instance &_instance;
309  const string _baseuri;
310  string _scopes;
311  string _client_id;
312  string _client_secret;
313  };
314 
315 private:
316  const string _hostname;
317  const string _baseuri;
318  string _access_token;
319  uint64_t _max_chars;
320  string _proxy;
321  vector<string> _post_formats;
322  string _cainfo;
323  string _useragent;
324 };
325 
326 } // namespace mastodonpp
327 
328 #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:254
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:91
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:103
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:116
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:204
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:49
mastodonpp::Instance::set_useragent
void set_useragent(const string_view useragent) override
Sets the User-Agent.
Definition: instance.hpp:218
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:262
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:140
mastodonpp::Instance::get_access_token
string_view get_access_token() const noexcept
Returns the access token.
Definition: instance.hpp:127
mastodonpp::Instance::set_proxy
void set_proxy(const string_view proxy) override
Set the proxy to use.
Definition: instance.hpp:165
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