mastodonpp  0.5.0
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(const string_view hostname,
61  const string_view access_token)
62  : _hostname{hostname}
63  , _baseuri{"https://" + _hostname}
64  , _access_token{access_token}
65  , _max_chars{0}
66  {}
67 
78  inline void copy_connection_properties(CURLWrapper &curlwrapper)
79  {
80  curlwrapper.setup_connection_properties(_proxy, _access_token, _cainfo,
81  _useragent);
82  }
83 
89  [[nodiscard]]
90  inline string_view get_hostname() const noexcept
91  {
92  return _hostname;
93  }
94 
102  [[nodiscard]]
103  inline string_view get_baseuri() const noexcept
104  {
105  return _baseuri;
106  }
107 
113  [[nodiscard]]
114  inline string_view get_access_token() const noexcept
115  {
116  return _access_token;
117  }
118 
127  inline void set_access_token(string access_token)
128  {
129  _access_token = move(access_token);
130  }
131 
143  [[nodiscard]]
144  uint64_t get_max_chars() noexcept;
145 
151  void set_proxy(const string_view proxy) override
152  {
153  _proxy = proxy;
154  CURLWrapper::set_proxy(proxy);
155  }
156 
166  [[nodiscard]]
168 
180  vector<string> get_post_formats() noexcept;
181 
190  void set_cainfo(string_view path) override
191  {
192  _cainfo = path;
194  }
195 
204  void set_useragent(const string_view useragent) override
205  {
206  _useragent = useragent;
207  CURLWrapper::set_useragent(useragent);
208  }
209 
240  class ObtainToken : public CURLWrapper
241  {
242  public:
248  explicit ObtainToken(Instance &instance)
249  : _instance{instance}
250  , _baseuri{instance.get_baseuri()}
251  {
252  _instance.copy_connection_properties(*this);
253  }
254 
271  [[nodiscard]]
272  answer_type step_1(string_view client_name, string_view scopes,
273  string_view website);
274 
290  [[nodiscard]]
291  answer_type step_2(string_view code);
292 
293  private:
294  Instance &_instance;
295  const string _baseuri;
296  string _scopes;
297  string _client_id;
298  string _client_secret;
299  };
300 
301 private:
302  const string _hostname;
303  const string _baseuri;
304  string _access_token;
305  uint64_t _max_chars;
306  string _proxy;
307  vector<string> _post_formats;
308  string _cainfo;
309  string _useragent;
310 };
311 
312 } // namespace mastodonpp
313 
314 #endif // MASTODONPP_INSTANCE_HPP
mastodonpp::Instance::ObtainToken
Simplifies obtaining an OAuth 2.0 Bearer Access Token.
Definition: instance.hpp:240
mastodonpp::Instance::get_nodeinfo
answer_type get_nodeinfo()
Returns the NodeInfo of the instance.
mastodonpp::Instance::get_hostname
string_view get_hostname() const noexcept
Returns the hostname.
Definition: instance.hpp:90
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:103
mastodonpp
C++ wrapper for the Mastodon API.
Definition: api.cpp:19
mastodonpp::Instance::Instance
Instance(const string_view hostname, const string_view access_token)
Construct a new Instance object.
Definition: instance.hpp:60
mastodonpp::Instance::set_access_token
void set_access_token(string access_token)
Set OAuth 2.0 Bearer Access Token.
Definition: instance.hpp:127
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:190
mastodonpp::CURLWrapper::set_proxy
virtual void set_proxy(string_view proxy)
Set the proxy to use.
Definition: curl_wrapper.cpp:216
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:253
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:204
mastodonpp::CURLWrapper::set_useragent
virtual void set_useragent(string_view useragent)
Sets the User-Agent.
Definition: curl_wrapper.cpp:263
mastodonpp::Instance::ObtainToken::ObtainToken
ObtainToken(Instance &instance)
Constructor.
Definition: instance.hpp:248
mastodonpp::CURLWrapper
Handles the details of network connections.
Definition: curl_wrapper.hpp:58
mastodonpp::Instance::copy_connection_properties
void copy_connection_properties(CURLWrapper &curlwrapper)
Set the properties of the connection of the calling class up.
Definition: instance.hpp:78
mastodonpp::Instance::get_access_token
string_view get_access_token() const noexcept
Returns the access token.
Definition: instance.hpp:114
mastodonpp::Instance::set_proxy
void set_proxy(const string_view proxy) override
Set the proxy to use.
Definition: instance.hpp:151
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:190