mastodonpp  0.0.0
Public Member Functions | List of all members
mastodonpp::CURLWrapper Class Reference

Handles the details of network connections. More...

#include <mastodonpp/curl_wrapper.hpp>

Inheritance diagram for mastodonpp::CURLWrapper:
mastodonpp::Connection mastodonpp::Instance

Public Member Functions

 CURLWrapper ()
 Initializes curl and sets up connection. More...
 
 CURLWrapper (const CURLWrapper &other)=default
 Copy constructor. More...
 
 CURLWrapper (CURLWrapper &&other) noexcept=default
 Move constructor. More...
 
virtual ~CURLWrapper () noexcept
 Cleans up curl and connection. More...
 
CURLWrapperoperator= (const CURLWrapper &other)=default
 Copy assignment operator. More...
 
CURLWrapperoperator= (CURLWrapper &&other) noexcept=default
 Move assignment operator. More...
 
answer_type make_request (const http_method &method, const string_view &uri)
 Make a request. More...
 

Detailed Description

Handles the details of network connections.

You don't need to use this.

Since
0.1.0

Constructor & Destructor Documentation

◆ CURLWrapper() [1/3]

mastodonpp::CURLWrapper::CURLWrapper ( )

Initializes curl and sets up connection.

Calls curl_global_init, which is not thread-safe. For more information consult curl_global_init(3).

Since
0.1.0
30  : _curl_buffer_error{}
31 {
32  curl_global_init(CURL_GLOBAL_ALL); // NOLINT(hicpp-signed-bitwise)
33  _connection = curl_easy_init();
34  setup_curl();
35 }

◆ CURLWrapper() [2/3]

mastodonpp::CURLWrapper::CURLWrapper ( const CURLWrapper other)
default

Copy constructor.

◆ CURLWrapper() [3/3]

mastodonpp::CURLWrapper::CURLWrapper ( CURLWrapper &&  other)
defaultnoexcept

Move constructor.

◆ ~CURLWrapper()

mastodonpp::CURLWrapper::~CURLWrapper ( )
virtualnoexcept

Cleans up curl and connection.

Calls curl_global_cleanup, which is not thread-safe. For more information consult curl_global_cleanup(3).

Since
0.1.0
37 {
38  curl_easy_cleanup(_connection);
39  curl_global_cleanup();
40 }

Member Function Documentation

◆ make_request()

answer_type mastodonpp::CURLWrapper::make_request ( const http_method method,
const string_view &  uri 
)

Make a request.

Parameters
methodThe HTTP method.
uriThe full URI.
Since
0.1.0
44 {
45  debuglog << "Making request to: " << uri << '\n';
46 
47  CURLcode code;
48  switch (method)
49  {
50  case http_method::GET:
51  {
52  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
53  code = curl_easy_setopt(_connection, CURLOPT_HTTPGET, 1L);
54  break;
55  }
56  case http_method::POST:
57  {
58  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
59  code = curl_easy_setopt(_connection, CURLOPT_POST, 1L);
60  break;
61  }
62  }
63  if (code != CURLE_OK)
64  {
65  throw CURLException{code, "Failed to set HTTP method",
66  _curl_buffer_error};
67  }
68 
69  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
70  code = curl_easy_setopt(_connection, CURLOPT_URL, uri.data());
71  if (code != CURLE_OK)
72  {
73  throw CURLException{code, "Failed to set URI", _curl_buffer_error};
74  }
75 
76  answer_type answer;
77  code = curl_easy_perform(_connection);
78  if (code == CURLE_OK)
79  {
80  long http_status; // NOLINT(google-runtime-int)
81  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
82  curl_easy_getinfo(_connection, CURLINFO_RESPONSE_CODE, &http_status);
83  answer.http_status = static_cast<uint16_t>(http_status);
84  debuglog << "HTTP status code: " << http_status << '\n';
85 
86  answer.headers = _curl_buffer_headers;
87  answer.body = _curl_buffer_body;
88  }
89  else
90  {
91  answer.curl_error_code = static_cast<uint8_t>(code);
92  answer.error_message = _curl_buffer_error;
93  debuglog << "libcurl error: " << code << '\n';
94  debuglog << _curl_buffer_error << '\n';
95  }
96 
97  return answer;
98 }

◆ operator=() [1/2]

CURLWrapper& mastodonpp::CURLWrapper::operator= ( const CURLWrapper other)
default

Copy assignment operator.

◆ operator=() [2/2]

CURLWrapper& mastodonpp::CURLWrapper::operator= ( CURLWrapper &&  other)
defaultnoexcept

Move assignment operator.


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