mastodonpp  0.0.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 (const string_view &hostname, const 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...
 
uint64_t get_max_chars ()
 Returns the maximum number of characters per post. More...
 
- Public Member Functions inherited from mastodonpp::CURLWrapper
 CURLWrapper ()
 Initializes curl and sets up connection. More...
 
 CURLWrapper (const CURLWrapper &other)=default
 Copy constructor. More...
 
 CURLWrapper (CURLWrapper &&other) noexcept=default
 Move constructor. More...
 
virtual ~CURLWrapper () noexcept
 Cleans up curl and connection. More...
 
CURLWrapperoperator= (const CURLWrapper &other)=default
 Copy assignment operator. More...
 
CURLWrapperoperator= (CURLWrapper &&other) noexcept=default
 Move assignment operator. More...
 
CURL * get_curl_easy_handle ()
 Returns pointer to the CURL easy handle. More...
 

Additional Inherited Members

- Protected Member Functions inherited from mastodonpp::CURLWrapper
answer_type make_request (const http_method &method, const string_view &uri)
 Make a request. More...
 

Detailed Description

Holds the access data of an instance.

Since
0.1.0

Constructor & Destructor Documentation

◆ Instance()

mastodonpp::Instance::Instance ( const string_view &  hostname,
const 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
87  {
88  return _access_token;
89  }

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

◆ get_hostname()

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

Returns the hostname.

Since
0.1.0
63  {
64  return _hostname;
65  }

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

The documentation for this class was generated from the following files:
mastodonpp::CURLWrapper::make_request
answer_type make_request(const http_method &method, const string_view &uri)
Make a request.
Definition: curl_wrapper.cpp:60