mastodonpp  0.2.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...
 
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
264  {
266  }

◆ 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
238  {
239  return del(endpoint, {});
240  }

◆ 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
80 {
81  return make_request(http_method::DELETE,
82  endpoint_to_uri(endpoint), parameters);
83 }

◆ 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
96 {
97  buffer_mutex.lock();
98  auto &buffer{get_buffer()};
99  vector<event_type> events;
100 
101  size_t pos{0};
102  while ((pos = buffer.find("event: ")) != string::npos)
103  {
104  const auto endpos{buffer.find("\n\n", pos)};
105  if (endpos == string::npos)
106  {
107  break;
108  }
109 
110  event_type event;
111  pos += 7; // Length of "event: ".
112  event.type = buffer.substr(pos, buffer.find('\n', pos) - pos);
113  pos = buffer.find("data: ") + 6;
114  event.data = buffer.substr(pos, endpos - pos);
115  events.push_back(event);
116 
117  buffer.erase(0, endpos);
118  }
119 
120  buffer_mutex.unlock();
121  return events;
122 }

◆ 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
86 {
87  buffer_mutex.lock();
88  auto &buffer{get_buffer()};
89  auto buffer_copy{buffer};
90  buffer.clear();
91  buffer_mutex.unlock();
92  return buffer_copy;
93 }

◆ 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
186  {
187  return patch(endpoint, {});
188  }

◆ 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
66 {
67  return make_request(http_method::PATCH,
68  endpoint_to_uri(endpoint), parameters);
69 }

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

◆ 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
212  {
213  return put(endpoint, {});
214  }

◆ 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
73 {
74  return make_request(http_method::PUT,
75  endpoint_to_uri(endpoint), parameters);
76 }

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:194
mastodonpp::Instance::get_access_token
string_view get_access_token() const
Returns the access token.
Definition: instance.hpp:87
mastodonpp::Connection::put
answer_type put(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP PUT call with parameters.
Definition: connection.cpp:71
mastodonpp::CURLWrapper::buffer_mutex
mutex buffer_mutex
Mutex for get_buffer a.k.a. _curl_buffer_body.
Definition: curl_wrapper.hpp:169
mastodonpp::CURLWrapper::get_buffer
string & get_buffer()
Returns a reference to the buffer libcurl writes into.
Definition: curl_wrapper.hpp:190
mastodonpp::Connection::del
answer_type del(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP DELETE call with parameters.
Definition: connection.cpp:78
mastodonpp::CURLWrapper::set_proxy
void set_proxy(string_view proxy)
Set the proxy to use.
Definition: curl_wrapper.cpp:67
mastodonpp::Connection::patch
answer_type patch(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP PATCH call with parameters.
Definition: connection.cpp:64
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:204