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

Represents a connection to an instance. Used for requests. More...

#include <mastodonpp/connection.hpp>

Inheritance diagram for mastodonpp::Connection:
mastodonpp::CURLWrapper

Public Member Functions

 Connection (Instance &instance)
 Construct a new Connection object. More...
 
answer_type get (const endpoint_variant &endpoint, const parametermap &parameters)
 Make a HTTP GET call with parameters. More...
 
answer_type get (const endpoint_variant &endpoint)
 Make a HTTP GET call. More...
 
answer_type post (const endpoint_variant &endpoint, const parametermap &parameters)
 Make a HTTP POST call with parameters. More...
 
answer_type post (const endpoint_variant &endpoint)
 Make a HTTP POST call. More...
 
string get_new_stream_contents ()
 Copy new stream contents and delete the “original”. More...
 
vector< event_typeget_new_events ()
 Get new stream events. More...
 
void cancel_stream ()
 Cancel the stream. 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

Represents a connection to an instance. Used for requests.

Since
0.1.0

Constructor & Destructor Documentation

◆ Connection()

mastodonpp::Connection::Connection ( Instance instance)
explicit

Construct a new Connection object.

Parameters
instanceAn Instance with the access data.
Since
0.1.0
25  : _instance{instance}
26  , _baseuri{instance.get_baseuri()}
27 {
28  auto proxy{_instance.get_proxy()};
29  if (!proxy.empty())
30  {
32  }
33 
34  if (!_instance.get_access_token().empty())
35  {
37  }
38 }

Member Function Documentation

◆ cancel_stream()

void mastodonpp::Connection::cancel_stream ( )
inline

Cancel the stream.

The stream will be cancelled, usually whithin a second. The curl_error_code of the answer will be set to 42 (CURLE_ABORTED_BY_CALLBACK).

Since
0.1.0
186  {
188  }

◆ get() [1/2]

answer_type mastodonpp::Connection::get ( const endpoint_variant endpoint)
inline

Make a HTTP GET call.

Example:

auto answer{connection.get("/api/v1/instance")};
Parameters
endpointEndpoint as API::endpoint_type or std::string_view.
Since
0.1.0
123  {
124  return get(endpoint, {});
125  }

◆ get() [2/2]

answer_type mastodonpp::Connection::get ( const endpoint_variant endpoint,
const parametermap parameters 
)

Make a HTTP GET call with parameters.

Example:

auto answer{connection.get(mastodonpp::API::v1::accounts_id_followers,
{
{"id", "12"},
{"limit", "10"}
})};
Parameters
endpointEndpoint as API::endpoint_type or std::string_view.
parametersA map of parameters.
Since
0.1.0
52 {
53  return make_request(http_method::GET,
54  endpoint_to_uri(endpoint), parameters);
55 }

◆ get_new_events()

vector< event_type > mastodonpp::Connection::get_new_events ( )

Get new stream events.

Since
0.1.0
75 {
76  buffer_mutex.lock();
77  auto &buffer{get_buffer()};
78  vector<event_type> events;
79 
80  size_t pos{0};
81  while ((pos = buffer.find("event: ")) != string::npos)
82  {
83  const auto endpos{buffer.find("\n\n", pos)};
84  if (endpos == string::npos)
85  {
86  break;
87  }
88 
89  event_type event;
90  pos += 7; // Length of "event: ".
91  event.type = buffer.substr(pos, buffer.find('\n', pos) - pos);
92  pos = buffer.find("data: ") + 6;
93  event.data = buffer.substr(pos, endpos - pos);
94  events.push_back(event);
95 
96  buffer.erase(0, endpos);
97  }
98 
99  buffer_mutex.unlock();
100  return events;
101 }

◆ get_new_stream_contents()

string mastodonpp::Connection::get_new_stream_contents ( )

Copy new stream contents and delete the “original”.

Note that the last event is not necessarily complete, it could happen that you are calling this function mid-transfer. You have to check the data integrity yourself.

Using get_new_events() instead is recommended.

Since
0.1.0
65 {
66  buffer_mutex.lock();
67  auto &buffer{get_buffer()};
68  auto buffer_copy{buffer};
69  buffer.clear();
70  buffer_mutex.unlock();
71  return buffer_copy;
72 }

◆ post() [1/2]

answer_type mastodonpp::Connection::post ( const endpoint_variant endpoint)
inline

Make a HTTP POST call.

Parameters
endpointEndpoint as API::endpoint_type or std::string_view.
Since
0.1.0
160  {
161  return post(endpoint, {});
162  }

◆ post() [2/2]

answer_type mastodonpp::Connection::post ( const endpoint_variant endpoint,
const parametermap parameters 
)

Make a HTTP POST call with parameters.

Example:

auto answer{connection.post(
mastodonpp::API::v1::statuses,
{
{"status", "How is the wheather?"},
{"poll[options]", vector<string_view>{"Nice", "not nice"}},
{"poll[expires_in]", to_string(poll_seconds)}
})};
Parameters
endpointEndpoint as API::endpoint_type or std::string_view.
parametersA map of parameters.
Since
0.1.0
59 {
60  return make_request(http_method::POST,
61  endpoint_to_uri(endpoint), parameters);
62 }

The documentation for this class was generated from the following files:
mastodonpp::Connection::get
answer_type get(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP GET call with parameters.
Definition: connection.cpp:50
mastodonpp::Instance::get_proxy
string_view get_proxy() const
Returns the proxy string that was previously set.
Definition: instance.hpp:132
mastodonpp::CURLWrapper::set_access_token
void set_access_token(const string_view access_token)
Set OAuth 2.0 Bearer Access Token.
Definition: curl_wrapper.cpp:170
mastodonpp::Instance::get_access_token
string_view get_access_token() const
Returns the access token.
Definition: instance.hpp:87
mastodonpp::CURLWrapper::buffer_mutex
mutex buffer_mutex
Mutex for get_buffer a.k.a. _curl_buffer_body.
Definition: curl_wrapper.hpp:164
mastodonpp::CURLWrapper::get_buffer
string & get_buffer()
Returns a reference to the buffer libcurl writes into.
Definition: curl_wrapper.hpp:185
mastodonpp::CURLWrapper::set_proxy
void set_proxy(string_view proxy)
Set the proxy to use.
Definition: curl_wrapper.cpp:67
mastodonpp::Connection::post
answer_type post(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP POST call with parameters.
Definition: connection.cpp:57
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
mastodonpp::CURLWrapper::cancel_stream
void cancel_stream()
Cancel the stream.
Definition: curl_wrapper.hpp:199