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 "answer.hpp"
21 
22 #include "curl/curl.h"
23 
24 #include <map>
25 #include <string>
26 #include <string_view>
27 #include <variant>
28 #include <vector>
29 
30 namespace mastodonpp
31 {
32 
33 using std::map;
34 using std::string;
35 using std::string_view;
36 using std::variant;
37 using std::vector;
38 
44 enum class http_method
45 {
46  GET,
47  POST,
48  PATCH,
49  PUT,
50  DELETE
51 };
52 
67 using parametermap = map<string, variant<string, vector<string>>>;
68 
79 {
80 public:
91  CURLWrapper();
92 
94  CURLWrapper(const CURLWrapper &other) = default;
95 
97  CURLWrapper(CURLWrapper &&other) noexcept = default;
98 
108  virtual ~CURLWrapper() noexcept;
109 
111  CURLWrapper& operator=(const CURLWrapper &other) = default;
112 
114  CURLWrapper& operator=(CURLWrapper &&other) noexcept = default;
115 
125  inline CURL *get_curl_easy_handle()
126  {
127  return _connection;
128  }
129 
130 protected:
139  [[nodiscard]]
140  answer_type make_request(const http_method &method, const string_view &uri);
141 
142 private:
143  CURL *_connection;
144  char _curl_buffer_error[CURL_ERROR_SIZE];
145  string _curl_buffer_headers;
146  string _curl_buffer_body;
147 
153  static int writer(char *data, size_t size, size_t nmemb,
154  string *writerData);
155 
161  void setup_curl();
162 };
163 
164 } // namespace mastodonpp
165 
166 #endif // MASTODONPP_CURL_WRAPPER_HPP
mastodonpp::http_method
http_method
The HTTP method.
Definition: curl_wrapper.hpp:44
mastodonpp::CURLWrapper::CURLWrapper
CURLWrapper()
Initializes curl and sets up connection.
Definition: curl_wrapper.cpp:35
mastodonpp::CURLWrapper::get_curl_easy_handle
CURL * get_curl_easy_handle()
Returns pointer to the CURL easy handle.
Definition: curl_wrapper.hpp:125
mastodonpp
C++ wrapper for the Mastodon API.
Definition: answer.cpp:19
mastodonpp::answer_type
Return type for Requests.
Definition: answer.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:60
mastodonpp::parametermap
map< string, variant< string, vector< string > >> parametermap
std::map of parameters for API calls.
Definition: curl_wrapper.hpp:67
mastodonpp::CURLWrapper
Handles the details of network connections.
Definition: curl_wrapper.hpp:78
mastodonpp::CURLWrapper::~CURLWrapper
virtual ~CURLWrapper() noexcept
Cleans up curl and connection.
Definition: curl_wrapper.cpp:48
mastodonpp::CURLWrapper::operator=
CURLWrapper & operator=(const CURLWrapper &other)=default
Copy assignment operator.