mastodonpp  0.1.0
Public Member Functions | List of all members
mastodonpp::Instance Class Reference

Holds the access data of an instance. More...

#include <mastodonpp/instance.hpp>

Inheritance diagram for mastodonpp::Instance:
mastodonpp::CURLWrapper

Public Member Functions

 Instance (string_view hostname, string_view access_token)
 Construct a new Instance object. More...
 
string_view get_hostname () const
 Returns the hostname. More...
 
string_view get_baseuri () const
 Returns the base URI. More...
 
string_view get_access_token () const
 Returns the access token. More...
 
void set_access_token (string access_token)
 Set OAuth 2.0 Bearer Access Token. More...
 
uint64_t get_max_chars ()
 Returns the maximum number of characters per post. More...
 
void set_proxy (const string_view proxy)
 Set the proxy to use. More...
 
string_view get_proxy () const
 Returns the proxy string that was previously set. More...
 
- Public Member Functions inherited from mastodonpp::CURLWrapper
 CURLWrapper ()
 Initializes curl and sets up connection. More...
 
 CURLWrapper (const CURLWrapper &other)=delete
 Copy constructor. More...
 
 CURLWrapper (CURLWrapper &&other) noexcept=delete
 Move constructor. More...
 
virtual ~CURLWrapper () noexcept
 Cleans up curl and connection. More...
 
CURLWrapperoperator= (const CURLWrapper &other)=delete
 Copy assignment operator. More...
 
CURLWrapperoperator= (CURLWrapper &&other) noexcept=delete
 Move assignment operator. More...
 
CURL * get_curl_easy_handle ()
 Returns pointer to the CURL easy handle. More...
 
void set_proxy (string_view proxy)
 Set the proxy to use. More...
 

Additional Inherited Members

- Protected Member Functions inherited from mastodonpp::CURLWrapper
answer_type make_request (const http_method &method, string uri, const parametermap &parameters)
 Make a HTTP request. More...
 
string & get_buffer ()
 Returns a reference to the buffer libcurl writes into. More...
 
void cancel_stream ()
 Cancel the stream. More...
 
void set_access_token (const string_view access_token)
 Set OAuth 2.0 Bearer Access Token. More...
 
- Protected Attributes inherited from mastodonpp::CURLWrapper
mutex buffer_mutex
 Mutex for get_buffer a.k.a. _curl_buffer_body. More...
 

Detailed Description

Holds the access data of an instance.

Since
0.1.0

Constructor & Destructor Documentation

◆ Instance()

mastodonpp::Instance::Instance ( string_view  hostname,
string_view  access_token 
)
explicit

Construct a new Instance object.

Also queries /api/v1/instance for ‘max_toot_chars’.

Parameters
hostnameThe hostname of the instance.
access_tokenYour access token.
Since
0.1.0
27  : _hostname{hostname}
28  , _baseuri{"https://" + _hostname}
29  , _access_token{access_token}
30  , _max_chars{0}
31 {}

Member Function Documentation

◆ get_access_token()

string_view mastodonpp::Instance::get_access_token ( ) const
inline

Returns the access token.

Since
0.1.0
88  {
89  return _access_token;
90  }

◆ get_baseuri()

string_view mastodonpp::Instance::get_baseuri ( ) const
inline

Returns the base URI.

The base URI is “https://” + the hostname.

Since
0.1.0
77  {
78  return _baseuri;
79  }

◆ get_hostname()

string_view mastodonpp::Instance::get_hostname ( ) const
inline

Returns the hostname.

Since
0.1.0
64  {
65  return _hostname;
66  }

◆ get_max_chars()

uint64_t mastodonpp::Instance::get_max_chars ( )

Returns the maximum number of characters per post.

Since
0.1.0
34 {
35  constexpr uint64_t default_max_chars{500};
36 
37  if (_max_chars == 0)
38  {
39  try
40  {
41  debuglog << "Querying " << _hostname << " for max_toot_chars…\n";
42  const auto answer{make_request(http_method::GET,
43  _baseuri + "/api/v1/instance", {})};
44  if (!answer)
45  {
46  debuglog << "Could not get instance info.\n";
47  return default_max_chars;
48  }
49 
50  _max_chars = [&answer]
51  {
52  auto &body{answer.body};
53  size_t pos_start{body.find("max_toot_chars")};
54  if (pos_start == string::npos)
55  {
56  debuglog << "max_toot_chars not found.\n";
57  return default_max_chars;
58  }
59  pos_start = body.find(':', pos_start) + 1;
60  const size_t pos_end{body.find(',', pos_start)};
61 
62  const auto max_toot_chars{body.substr(pos_start,
63  pos_end - pos_start)};
64  return static_cast<uint64_t>(stoull(max_toot_chars));
65  }();
66  debuglog << "Set _max_chars to: " << _max_chars << '\n';
67  }
68  catch (const std::exception &e)
69  {
70  debuglog << "Unexpected exception: " << e.what() << '\n';
71  }
72  }
73 
74  return _max_chars;
75 }

◆ get_proxy()

string_view mastodonpp::Instance::get_proxy ( ) const
inline

Returns the proxy string that was previously set.

Does not return the proxy if it was set from an environment variable.

Since
0.1.0
133  {
134  return _proxy;
135  }

◆ set_access_token()

void mastodonpp::Instance::set_access_token ( string  access_token)
inline

Set OAuth 2.0 Bearer Access Token.

Sets also the access token for all Connections that are initialized with this Instance afterwards.

Since
0.1.0
101  {
102  _access_token = move(access_token);
103  }

◆ set_proxy()

void mastodonpp::Instance::set_proxy ( const string_view  proxy)
inline

Set the proxy to use.

See CURLOPT_PROXY(3).

Parameters
proxyExamples: "socks4a://127.0.0.1:9050", "http://[::1]:3128".
Since
0.1.0

Sets also the proxy for all Connections that are initialized with this Instance afterwards.

119  {
120  _proxy = proxy;
121  CURLWrapper::set_proxy(proxy);
122  }

The documentation for this class was generated from the following files:
mastodonpp::CURLWrapper::set_proxy
void set_proxy(string_view proxy)
Set the proxy to use.
Definition: curl_wrapper.cpp:67
mastodonpp::CURLWrapper::make_request
answer_type make_request(const http_method &method, string uri, const parametermap &parameters)
Make a HTTP request.
Definition: curl_wrapper.cpp:77