mastodonpp  0.0.0
curl_wrapper.hpp
1 /* This file is part of mastodonpp.
2  * Copyright © 2020 tastytea <tastytea@tastytea.de>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as published by
6  * the Free Software Foundation, version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Affero General Public License for more details.
12  *
13  * You should have received a copy of the GNU Affero General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef MASTODONPP_CURL_WRAPPER_HPP
18 #define MASTODONPP_CURL_WRAPPER_HPP
19 
20 #include "return_types.hpp"
21 
22 #include "curl/curl.h"
23 
24 #include <string>
25 #include <string_view>
26 
27 namespace mastodonpp
28 {
29 
30 using std::string;
31 using std::string_view;
32 
38 enum class http_method
39 {
40  GET,
41  POST,
42  PATCH,
43  PUT,
44  DELETE
45 };
46 
57 {
58 public:
69  CURLWrapper();
70 
72  CURLWrapper(const CURLWrapper &other) = default;
73 
75  CURLWrapper(CURLWrapper &&other) noexcept = default;
76 
86  virtual ~CURLWrapper() noexcept;
87 
89  CURLWrapper& operator=(const CURLWrapper &other) = default;
90 
92  CURLWrapper& operator=(CURLWrapper &&other) noexcept = default;
93 
102  [[nodiscard]]
103  answer_type make_request(const http_method &method, const string_view &uri);
104 
105 private:
106  CURL *_connection;
107  char _curl_buffer_error[CURL_ERROR_SIZE];
108  string _curl_buffer_headers;
109  string _curl_buffer_body;
110 
116  static int writer(char *data, size_t size, size_t nmemb,
117  string *writerData);
118 
124  void setup_curl();
125 };
126 
127 } // namespace mastodonpp
128 
129 #endif // MASTODONPP_CURL_WRAPPER_HPP
mastodonpp::http_method
http_method
The HTTP method.
Definition: curl_wrapper.hpp:38
mastodonpp::CURLWrapper::CURLWrapper
CURLWrapper()
Initializes curl and sets up connection.
Definition: curl_wrapper.cpp:31
mastodonpp
C++ wrapper for the Mastodon API.
Definition: api.cpp:19
mastodonpp::answer_type
Return type for Requests.
Definition: return_types.hpp:40
mastodonpp::CURLWrapper::make_request
answer_type make_request(const http_method &method, const string_view &uri)
Make a request.
Definition: curl_wrapper.cpp:56
mastodonpp::CURLWrapper
Handles the details of network connections.
Definition: curl_wrapper.hpp:56
mastodonpp::CURLWrapper::~CURLWrapper
virtual ~CURLWrapper() noexcept
Cleans up curl and connection.
Definition: curl_wrapper.cpp:44
mastodonpp::CURLWrapper::operator=
CURLWrapper & operator=(const CURLWrapper &other)=default
Copy assignment operator.