mastodonpp  0.5.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...
 
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 &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...
 
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()

mastodonpp::Connection::Connection ( 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  }

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
272  {
274  }

◆ 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
246  {
247  return del(endpoint, {});
248  }

◆ 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,
66  endpoint_to_uri(endpoint), parameters);
67 }

◆ 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
131  {
132  return get(endpoint, {});
133  }

◆ 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,
38  endpoint_to_uri(endpoint), 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 }

◆ 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  const string buffer_copy{buffer};
74  buffer.clear();
75  _buffer_mutex.unlock();
76  return buffer_copy;
77 }

◆ 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
194  {
195  return patch(endpoint, {});
196  }

◆ 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,
52  endpoint_to_uri(endpoint), 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
168  {
169  return post(endpoint, {});
170  }

◆ 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,
45  endpoint_to_uri(endpoint), 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
220  {
221  return put(endpoint, {});
222  }

◆ 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,
59  endpoint_to_uri(endpoint), parameters);
60 }

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:34
mastodonpp::CURLWrapper::_buffer_mutex
mutex _buffer_mutex
Mutex for get_buffer a.k.a. _curl_buffer_body.
Definition: curl_wrapper.hpp:175
mastodonpp::Connection::put
answer_type put(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP PUT call with parameters.
Definition: connection.cpp:55
mastodonpp::CURLWrapper::get_buffer
string & get_buffer()
Returns a reference to the buffer libcurl writes into.
Definition: curl_wrapper.hpp:196
mastodonpp::Connection::del
answer_type del(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP DELETE call with parameters.
Definition: connection.cpp:62
mastodonpp::Connection::patch
answer_type patch(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP PATCH call with parameters.
Definition: connection.cpp:48
mastodonpp::Connection::post
answer_type post(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP POST call with parameters.
Definition: connection.cpp:41
mastodonpp::Instance::copy_connection_properties
void copy_connection_properties(CURLWrapper &curlwrapper)
Set the properties of the connection of the calling class up.
Definition: instance.hpp:78
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:67
mastodonpp::CURLWrapper::cancel_stream
void cancel_stream()
Cancel the stream.
Definition: curl_wrapper.hpp:210