mastodonpp  0.0.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...
 
string get_new_stream_contents ()
 Copy new stream contents and delete the “original”. More...
 
vector< event_typeget_new_events ()
 Get new stream events. 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...
 
void cancel_stream ()
 Cancel the stream. 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...
 
- 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 }

Member Function Documentation

◆ 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
37 {
38  const string uri{[&]
39  {
40  if (holds_alternative<API::endpoint_type>(endpoint))
41  {
42  return string(_baseuri)
43  += API{std::get<API::endpoint_type>(endpoint)}.to_string_view();
44  }
45  return string(_baseuri) += std::get<string_view>(endpoint);
46  }()};
47 
48  return make_request(http_method::GET, uri, parameters);
49 }

◆ get_new_events()

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

Get new stream events.

Since
0.1.0
62 {
63  buffer_mutex.lock();
64  auto &buffer{get_buffer()};
65  vector<event_type> events;
66 
67  size_t pos{0};
68  while ((pos = buffer.find("event: ")) != string::npos)
69  {
70  const auto endpos{buffer.find("\n\n", pos)};
71  if (endpos == string::npos)
72  {
73  break;
74  }
75 
76  event_type event;
77  pos += 7; // Length of "event: ".
78  event.type = buffer.substr(pos, buffer.find('\n', pos) - pos);
79  pos = buffer.find("data: ") + 6;
80  event.data = buffer.substr(pos, endpos - pos);
81  events.push_back(event);
82 
83  buffer.erase(0, endpos);
84  }
85 
86  buffer_mutex.unlock();
87  return events;
88 }

◆ 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
52 {
53  buffer_mutex.lock();
54  auto &buffer{get_buffer()};
55  auto buffer_copy{buffer};
56  buffer.clear();
57  buffer_mutex.unlock();
58  return buffer_copy;
59 }

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:35
mastodonpp::Instance::get_proxy
string_view get_proxy() const
Returns the proxy string that was previously set.
Definition: instance.hpp:116
mastodonpp::CURLWrapper::buffer_mutex
mutex buffer_mutex
Mutex for get_buffer a.k.a. _curl_buffer_body.
Definition: curl_wrapper.hpp:165
mastodonpp::CURLWrapper::get_buffer
string & get_buffer()
Returns a reference to the buffer libcurl writes into.
Definition: curl_wrapper.hpp:186
mastodonpp::CURLWrapper::set_proxy
void set_proxy(string_view proxy)
Set the proxy to use.
Definition: curl_wrapper.cpp:68
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:83