mastodonpp  0.4.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 
45 class Instance : public CURLWrapper
46 {
47 public:
56  explicit Instance(const string_view hostname,
57  const string_view access_token)
58  : _hostname{hostname}
59  , _baseuri{"https://" + _hostname}
60  , _access_token{access_token}
61  , _max_chars{0}
62  {}
63 
74  inline void copy_connection_properties(CURLWrapper &curlwrapper)
75  {
76  curlwrapper.setup_connection_properties(_proxy, _access_token, _cainfo,
77  _useragent);
78  }
79 
85  [[nodiscard]]
86  inline string_view get_hostname() const noexcept
87  {
88  return _hostname;
89  }
90 
98  [[nodiscard]]
99  inline string_view get_baseuri() const noexcept
100  {
101  return _baseuri;
102  }
103 
109  [[nodiscard]]
110  inline string_view get_access_token() const noexcept
111  {
112  return _access_token;
113  }
114 
123  inline void set_access_token(string access_token)
124  {
125  _access_token = move(access_token);
126  }
127 
139  [[nodiscard]]
140  uint64_t get_max_chars() noexcept;
141 
147  void set_proxy(const string_view proxy)
148  {
149  _proxy = proxy;
150  CURLWrapper::set_proxy(proxy);
151  }
152 
162  [[nodiscard]]
164 
176  vector<string> get_post_formats() noexcept;
177 
186  void set_cainfo(string_view path)
187  {
188  _cainfo = path;
190  }
191 
200  void set_useragent(const string_view useragent)
201  {
202  _useragent = useragent;
203  CURLWrapper::set_useragent(useragent);
204  }
205 
234  class ObtainToken : public CURLWrapper
235  {
236  public:
242  explicit ObtainToken(Instance &instance)
243  : _instance{instance}
244  , _baseuri{instance.get_baseuri()}
245  {
246  _instance.copy_connection_properties(*this);
247  }
248 
265  [[nodiscard]]
266  answer_type step_1(string_view client_name, string_view scopes,
267  string_view website);
268 
283  [[nodiscard]]
284  answer_type step_2(string_view code);
285 
286  private:
287  Instance &_instance;
288  const string _baseuri;
289  string _scopes;
290  string _client_id;
291  string _client_secret;
292  };
293 
294 private:
295  const string _hostname;
296  const string _baseuri;
297  string _access_token;
298  uint64_t _max_chars;
299  string _proxy;
300  vector<string> _post_formats;
301  string _cainfo;
302  string _useragent;
303 };
304 
305 } // namespace mastodonpp
306 
307 #endif // MASTODONPP_INSTANCE_HPP
mastodonpp::Instance::ObtainToken
Simplifies obtaining an OAuth 2.0 Bearer Access Token.
Definition: instance.hpp:234
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:86
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:99
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:56
mastodonpp::Instance::set_access_token
void set_access_token(string access_token)
Set OAuth 2.0 Bearer Access Token.
Definition: instance.hpp:123
mastodonpp::Instance::set_useragent
void set_useragent(const string_view useragent)
Sets the User-Agent.
Definition: instance.hpp:200
mastodonpp::Instance::ObtainToken::step_2
answer_type step_2(string_view code)
Creates a token via /oauth/token.
mastodonpp::CURLWrapper::set_proxy
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:81
mastodonpp::Instance::set_proxy
void set_proxy(const string_view proxy)
Set the proxy to use.
Definition: instance.hpp:147
mastodonpp::CURLWrapper::set_cainfo
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:45
mastodonpp::CURLWrapper::set_useragent
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:242
mastodonpp::Instance::set_cainfo
void set_cainfo(string_view path)
Set path to Certificate Authority (CA) bundle.
Definition: instance.hpp:186
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:74
mastodonpp::Instance::get_access_token
string_view get_access_token() const noexcept
Returns the access token.
Definition: instance.hpp:110
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