mastodonpp  0.5.7
C++ wrapper for the Mastodon and Pleroma APIs.
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 
78 class Connection : public CURLWrapper
79 {
80 public:
88  explicit Connection(const Instance &instance)
89  : _instance{instance}
90  , _baseuri{instance.get_baseuri()}
91  {
92  _instance.copy_connection_properties(*this);
93  }
94 
100  Connection(const Connection &other) = default;
101 
103  Connection(Connection &&other) noexcept = delete;
104 
106  ~Connection() noexcept override = default;
107 
109  Connection &operator=(const Connection &other) = delete;
110 
112  Connection &operator=(Connection &&other) noexcept = delete;
113 
132  [[nodiscard]] answer_type get(const endpoint_variant &endpoint,
133  const parametermap &parameters);
134 
147  [[nodiscard]] inline answer_type get(const endpoint_variant &endpoint)
148  {
149  return get(endpoint, {});
150  }
151 
172  [[nodiscard]] answer_type post(const endpoint_variant &endpoint,
173  const parametermap &parameters);
174 
182  [[nodiscard]] inline answer_type post(const endpoint_variant &endpoint)
183  {
184  return post(endpoint, {});
185  }
186 
196  [[nodiscard]] answer_type patch(const endpoint_variant &endpoint,
197  const parametermap &parameters);
198 
206  [[nodiscard]] inline answer_type patch(const endpoint_variant &endpoint)
207  {
208  return patch(endpoint, {});
209  }
210 
220  [[nodiscard]] answer_type put(const endpoint_variant &endpoint,
221  const parametermap &parameters);
222 
230  [[nodiscard]] inline answer_type put(const endpoint_variant &endpoint)
231  {
232  return put(endpoint, {});
233  }
234 
244  [[nodiscard]] answer_type del(const endpoint_variant &endpoint,
245  const parametermap &parameters);
246 
254  [[nodiscard]] inline answer_type del(const endpoint_variant &endpoint)
255  {
256  return del(endpoint, {});
257  }
258 
270  string get_new_stream_contents();
271 
277  vector<event_type> get_new_events();
278 
280  inline void cancel_stream()
281  {
283  }
284 
285 private:
286  const Instance &_instance;
287  const string_view _baseuri;
288 
289  [[nodiscard]] string
290  endpoint_to_uri(const endpoint_variant &endpoint) const;
291 };
292 
293 } // namespace mastodonpp
294 
295 #endif // MASTODONPP_CONNECTION_HPP
Handles the details of network connections.
Definition: curl_wrapper.hpp:58
void cancel_stream()
Cancel the stream.
Definition: curl_wrapper.hpp:209
Represents a connection to an instance. Used for requests.
Definition: connection.hpp:79
string get_new_stream_contents()
Copy new stream contents and delete the “original”.
Definition: connection.cpp:69
answer_type del(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP DELETE call with parameters.
Definition: connection.cpp:62
answer_type patch(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP PATCH call with parameters.
Definition: connection.cpp:48
answer_type del(const endpoint_variant &endpoint)
Make a HTTP DELETE call.
Definition: connection.hpp:254
~Connection() noexcept override=default
Destructor.
answer_type patch(const endpoint_variant &endpoint)
Make a HTTP PATCH call.
Definition: connection.hpp:206
vector< event_type > get_new_events()
Get new stream events.
Definition: connection.cpp:79
void cancel_stream()
Cancel the stream.
Definition: connection.hpp:280
Connection(const Connection &other)=default
Copy constructor. A new CURLWrapper is constructed.
answer_type put(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP PUT call with parameters.
Definition: connection.cpp:55
answer_type put(const endpoint_variant &endpoint)
Make a HTTP PUT call.
Definition: connection.hpp:230
answer_type post(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP POST call with parameters.
Definition: connection.cpp:41
answer_type get(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP GET call with parameters.
Definition: connection.cpp:34
Connection(const Instance &instance)
Construct a new Connection object.
Definition: connection.hpp:88
Connection(Connection &&other) noexcept=delete
Move constructor.
answer_type post(const endpoint_variant &endpoint)
Make a HTTP POST call.
Definition: connection.hpp:182
Holds the access data of an instance.
Definition: instance.hpp:49
void copy_connection_properties(CURLWrapper &curlwrapper) const
Set the properties of the connection of the calling class up.
Definition: instance.hpp:90
C++ wrapper for the Mastodon API.
Definition: api.hpp:25
variant< API::endpoint_type, string_view > endpoint_variant
An endpoint. Either API::endpoint_type or std::string_view.
Definition: connection.hpp:43
map< string_view, variant< string_view, vector< string_view > >> parametermap
std::map of parameters for API calls.
Definition: types.hpp:64
Return type for Requests.
Definition: types.hpp:80
A stream event.
Definition: connection.hpp:53
string type
The type of the event.
Definition: connection.hpp:62
string data
The payload.
Definition: connection.hpp:65