mastodonpp  0.2.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.

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

Member Function Documentation

◆ get_access_token()

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

Returns the access token.

Since
0.1.0
86  {
87  return _access_token;
88  }

◆ 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
75  {
76  return _baseuri;
77  }

◆ get_hostname()

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

Returns the hostname.

Since
0.1.0
62  {
63  return _hostname;
64  }

◆ get_max_chars()

uint64_t mastodonpp::Instance::get_max_chars ( )

Returns the maximum number of characters per post.

Queries /api/v1/instance for ‘max_toot_chars’. If the instance doesn't support it, the limit is assumed to be 500.

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

◆ 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
134  {
135  return _proxy;
136  }

◆ 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
99  {
100  _access_token = move(access_token);
101  }

◆ 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.

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

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