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...
 
void set_proxy (string_view proxy)
 Set the proxy to use. More...
 
string get_new_stream_contents ()
 Copy new stream contents and delete the “original”. 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 {}

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
98  {
99  return get(endpoint, {});
100  }

◆ 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
31 {
32  const string uri{[&]
33  {
34  if (holds_alternative<API::endpoint_type>(endpoint))
35  {
36  return string(_baseuri)
37  += API{std::get<API::endpoint_type>(endpoint)}.to_string_view();
38  }
39  return string(std::get<string_view>(endpoint));
40  }()};
41 
42  return make_request(http_method::GET, uri, parameters);
43 }

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

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 }

◆ set_proxy()

void mastodonpp::Connection::set_proxy ( string_view  proxy)

Set the proxy to use.

See CURLOPT_PROXY(3).

Parameters
proxyExamples: "socks4a://127.0.0.1:9050", "http://[::1]:3128".
Since
0.1.0

Sets also the proxy for the Instance you used to initialize this Connection.

46 {
48  _instance.set_proxy(proxy);
49 }

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:29
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:67
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:82