mastodonpp  0.4.0
connection.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_CONNECTION_HPP
18 #define MASTODONPP_CONNECTION_HPP
19 
20 #include "api.hpp"
21 #include "curl_wrapper.hpp"
22 #include "instance.hpp"
23 #include "types.hpp"
24 
25 #include <string>
26 #include <string_view>
27 #include <variant>
28 #include <vector>
29 
30 namespace mastodonpp
31 {
32 
33 using std::string;
34 using std::string_view;
35 using std::variant;
36 using std::vector;
37 
43 using endpoint_variant = variant<API::endpoint_type,string_view>;
44 
52 struct event_type
53 {
62  string type;
63 
65  string data;
66 };
67 
75 class Connection : public CURLWrapper
76 {
77 public:
85  explicit Connection(Instance &instance)
86  : _instance{instance}
87  , _baseuri{instance.get_baseuri()}
88  {
89  _instance.copy_connection_properties(*this);
90  }
91 
110  [[nodiscard]]
111  answer_type get(const endpoint_variant &endpoint,
112  const parametermap &parameters);
113 
126  [[nodiscard]]
127  inline answer_type get(const endpoint_variant &endpoint)
128  {
129  return get(endpoint, {});
130  }
131 
152  [[nodiscard]]
153  answer_type post(const endpoint_variant &endpoint,
154  const parametermap &parameters);
155 
163  [[nodiscard]]
164  inline answer_type post(const endpoint_variant &endpoint)
165  {
166  return post(endpoint, {});
167  }
168 
178  [[nodiscard]]
179  answer_type patch(const endpoint_variant &endpoint,
180  const parametermap &parameters);
181 
189  [[nodiscard]]
190  inline answer_type patch(const endpoint_variant &endpoint)
191  {
192  return patch(endpoint, {});
193  }
194 
204  [[nodiscard]]
205  answer_type put(const endpoint_variant &endpoint,
206  const parametermap &parameters);
207 
215  [[nodiscard]]
216  inline answer_type put(const endpoint_variant &endpoint)
217  {
218  return put(endpoint, {});
219  }
220 
230  [[nodiscard]]
231  answer_type del(const endpoint_variant &endpoint,
232  const parametermap &parameters);
233 
241  [[nodiscard]]
242  inline answer_type del(const endpoint_variant &endpoint)
243  {
244  return del(endpoint, {});
245  }
246 
258  string get_new_stream_contents();
259 
265  vector<event_type> get_new_events();
266 
268  inline void cancel_stream()
269  {
271  }
272 private:
273  Instance &_instance;
274  const string_view _baseuri;
275 
276  [[nodiscard]]
277  string endpoint_to_uri(const endpoint_variant &endpoint) const;
278 };
279 
280 } // namespace mastodonpp
281 
282 #endif // MASTODONPP_CONNECTION_HPP
mastodonpp::Connection::get
answer_type get(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP GET call with parameters.
Definition: connection.cpp:34
mastodonpp::event_type::type
string type
The type of the event.
Definition: connection.hpp:62
mastodonpp::Connection::post
answer_type post(const endpoint_variant &endpoint)
Make a HTTP POST call.
Definition: connection.hpp:164
mastodonpp::parametermap
map< string_view, variant< string_view, vector< string_view > >> parametermap
std::map of parameters for API calls.
Definition: types.hpp:64
mastodonpp::Connection::Connection
Connection(Instance &instance)
Construct a new Connection object.
Definition: connection.hpp:85
mastodonpp::Connection::put
answer_type put(const endpoint_variant &endpoint)
Make a HTTP PUT call.
Definition: connection.hpp:216
mastodonpp::Connection::get
answer_type get(const endpoint_variant &endpoint)
Make a HTTP GET call.
Definition: connection.hpp:127
mastodonpp::event_type::data
string data
The payload.
Definition: connection.hpp:65
mastodonpp::Instance::get_baseuri
string_view get_baseuri() const noexcept
Returns the base URI.
Definition: instance.hpp:99
mastodonpp
C++ wrapper for the Mastodon API.
Definition: api.cpp:19
mastodonpp::Connection::patch
answer_type patch(const endpoint_variant &endpoint)
Make a HTTP PATCH call.
Definition: connection.hpp:190
mastodonpp::Connection::put
answer_type put(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP PUT call with parameters.
Definition: connection.cpp:55
mastodonpp::Connection::get_new_events
vector< event_type > get_new_events()
Get new stream events.
Definition: connection.cpp:79
mastodonpp::Connection::del
answer_type del(const endpoint_variant &endpoint)
Make a HTTP DELETE call.
Definition: connection.hpp:242
mastodonpp::Connection::cancel_stream
void cancel_stream()
Cancel the stream.
Definition: connection.hpp:268
mastodonpp::endpoint_variant
variant< API::endpoint_type, string_view > endpoint_variant
An endpoint. Either API::endpoint_type or std::string_view.
Definition: connection.hpp:43
mastodonpp::Connection::del
answer_type del(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP DELETE call with parameters.
Definition: connection.cpp:62
mastodonpp::event_type
A stream event.
Definition: connection.hpp:52
mastodonpp::answer_type
Return type for Requests.
Definition: types.hpp:82
mastodonpp::Connection::patch
answer_type patch(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP PATCH call with parameters.
Definition: connection.cpp:48
mastodonpp::Instance
Holds the access data of an instance.
Definition: instance.hpp:45
mastodonpp::Connection::post
answer_type post(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP POST call with parameters.
Definition: connection.cpp:41
mastodonpp::Connection
Represents a connection to an instance. Used for requests.
Definition: connection.hpp:75
mastodonpp::CURLWrapper
Handles the details of network connections.
Definition: curl_wrapper.hpp:58
mastodonpp::Instance::copy_connection_properties
void copy_connection_properties(CURLWrapper &curlwrapper)
Set the properties of the connection of the calling class up.
Definition: instance.hpp:74
mastodonpp::Connection::get_new_stream_contents
string get_new_stream_contents()
Copy new stream contents and delete the “original”.
Definition: connection.cpp:69
mastodonpp::CURLWrapper::cancel_stream
void cancel_stream()
Cancel the stream.
Definition: curl_wrapper.hpp:210