mastodonpp  0.5.7
C++ wrapper for the Mastodon and Pleroma APIs.
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:
Inheritance graph
[legend]
Collaboration diagram for mastodonpp::Connection:
Collaboration graph
[legend]

Public Member Functions

 Connection (const Instance &instance)
 Construct a new Connection object. More...
 
 Connection (const Connection &other)=default
 Copy constructor. A new CURLWrapper is constructed. More...
 
 Connection (Connection &&other) noexcept=delete
 Move constructor. More...
 
 ~Connection () noexcept override=default
 Destructor. More...
 
Connectionoperator= (const Connection &other)=delete
 Copy assignment operator. More...
 
Connectionoperator= (Connection &&other) noexcept=delete
 Move assignment operator. 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...
 
answer_type patch (const endpoint_variant &endpoint, const parametermap &parameters)
 Make a HTTP PATCH call with parameters. More...
 
answer_type patch (const endpoint_variant &endpoint)
 Make a HTTP PATCH call. More...
 
answer_type put (const endpoint_variant &endpoint, const parametermap &parameters)
 Make a HTTP PUT call with parameters. More...
 
answer_type put (const endpoint_variant &endpoint)
 Make a HTTP PUT call. More...
 
answer_type del (const endpoint_variant &endpoint, const parametermap &parameters)
 Make a HTTP DELETE call with parameters. More...
 
answer_type del (const endpoint_variant &endpoint)
 Make a HTTP DELETE 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 &)
 Copy constructor. Does the same as the 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...
 
string escape_url (const string_view url) const
 URL encodes the given string. More...
 
string unescape_url (const string_view url) const
 URL decodes the given string. More...
 
void setup_connection_properties (string_view proxy, string_view access_token, string_view cainfo, string_view useragent)
 Set some properties of the connection. 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...
 
virtual void set_proxy (string_view proxy)
 Set the proxy to use. More...
 
void set_access_token (string_view access_token)
 Set OAuth 2.0 Bearer Access Token. More...
 
virtual void set_cainfo (string_view path)
 Set path to Certificate Authority (CA) bundle. More...
 
virtual void set_useragent (string_view useragent)
 Sets the User-Agent. 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.

Do not make 2 requests with the same Connection at the same time. You can create as many Connections as you want from one Instance.

Since
0.1.0

Constructor & Destructor Documentation

◆ Connection() [1/3]

mastodonpp::Connection::Connection ( const Instance instance)
inlineexplicit

Construct a new Connection object.

Parameters
instanceAn Instance with the access data.
Since
0.1.0
89  : _instance{instance}
90  , _baseuri{instance.get_baseuri()}
91  {
92  _instance.copy_connection_properties(*this);
93  }
void copy_connection_properties(CURLWrapper &curlwrapper) const
Set the properties of the connection of the calling class up.
Definition: instance.hpp:90

◆ Connection() [2/3]

mastodonpp::Connection::Connection ( const Connection other)
default

Copy constructor. A new CURLWrapper is constructed.

Since
0.5.2

◆ Connection() [3/3]

mastodonpp::Connection::Connection ( Connection &&  other)
deletenoexcept

Move constructor.

◆ ~Connection()

mastodonpp::Connection::~Connection ( )
overridedefaultnoexcept

Destructor.

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
281  {
283  }
void cancel_stream()
Cancel the stream.
Definition: curl_wrapper.hpp:209

◆ del() [1/2]

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

Make a HTTP DELETE call.

Parameters
endpointEndpoint as API::endpoint_type or std::string_view.
Since
0.2.0
255  {
256  return del(endpoint, {});
257  }
answer_type del(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP DELETE call with parameters.
Definition: connection.cpp:62

◆ del() [2/2]

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

Make a HTTP DELETE call with parameters.

Parameters
endpointEndpoint as API::endpoint_type or std::string_view.
parametersA map of parameters.
Since
0.2.0
64 {
65  return make_request(http_method::DELETE, endpoint_to_uri(endpoint),
66  parameters);
67 }
answer_type make_request(const http_method &method, string uri, const parametermap &parameters)
Make a HTTP request.
Definition: curl_wrapper.cpp:80

◆ 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
148  {
149  return get(endpoint, {});
150  }
answer_type get(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP GET call with parameters.
Definition: connection.cpp:34

◆ 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
36 {
37  return make_request(http_method::GET, endpoint_to_uri(endpoint),
38  parameters);
39 }

◆ get_new_events()

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

Get new stream events.

Since
0.1.0
80 {
81  _buffer_mutex.lock();
82  auto &buffer{get_buffer()};
83  vector<event_type> events;
84 
85  size_t pos{0};
86  constexpr string_view search_event{"event: "};
87  while ((pos = buffer.find(search_event)) != string::npos)
88  {
89  const auto endpos{buffer.find("\n\n", pos)};
90  if (endpos == string::npos)
91  {
92  break;
93  }
94 
95  event_type event;
96  pos += search_event.size();
97  event.type = buffer.substr(pos, buffer.find('\n', pos) - pos);
98  constexpr string_view search_data{"data: "};
99  pos = buffer.find(search_data) + search_data.size();
100  event.data = buffer.substr(pos, endpos - pos);
101  events.push_back(event);
102 
103  buffer.erase(0, endpos);
104  }
105 
106  _buffer_mutex.unlock();
107  return events;
108 }
string & get_buffer()
Returns a reference to the buffer libcurl writes into.
Definition: curl_wrapper.hpp:195
mutex _buffer_mutex
Mutex for get_buffer a.k.a. _curl_buffer_body.
Definition: curl_wrapper.hpp:175

◆ 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
70 {
71  _buffer_mutex.lock();
72  auto &buffer{get_buffer()};
73  string buffer_copy{buffer};
74  buffer.clear();
75  _buffer_mutex.unlock();
76  return buffer_copy;
77 }

◆ operator=() [1/2]

Connection& mastodonpp::Connection::operator= ( Connection &&  other)
deletenoexcept

Move assignment operator.

◆ operator=() [2/2]

Connection& mastodonpp::Connection::operator= ( const Connection other)
delete

Copy assignment operator.

◆ patch() [1/2]

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

Make a HTTP PATCH call.

Parameters
endpointEndpoint as API::endpoint_type or std::string_view.
Since
0.2.0
207  {
208  return patch(endpoint, {});
209  }
answer_type patch(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP PATCH call with parameters.
Definition: connection.cpp:48

◆ patch() [2/2]

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

Make a HTTP PATCH call with parameters.

Parameters
endpointEndpoint as API::endpoint_type or std::string_view.
parametersA map of parameters.
Since
0.2.0
50 {
51  return make_request(http_method::PATCH, endpoint_to_uri(endpoint),
52  parameters);
53 }

◆ 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
183  {
184  return post(endpoint, {});
185  }
answer_type post(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP POST call with parameters.
Definition: connection.cpp:41

◆ 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
43 {
44  return make_request(http_method::POST, endpoint_to_uri(endpoint),
45  parameters);
46 }

◆ put() [1/2]

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

Make a HTTP PUT call.

Parameters
endpointEndpoint as API::endpoint_type or std::string_view.
Since
0.2.0
231  {
232  return put(endpoint, {});
233  }
answer_type put(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP PUT call with parameters.
Definition: connection.cpp:55

◆ put() [2/2]

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

Make a HTTP PUT call with parameters.

Parameters
endpointEndpoint as API::endpoint_type or std::string_view.
parametersA map of parameters.
Since
0.2.0
57 {
58  return make_request(http_method::PUT, endpoint_to_uri(endpoint),
59  parameters);
60 }

The documentation for this class was generated from the following files: