mastodonpp  0.5.5
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]]
133  answer_type get(const endpoint_variant &endpoint,
134  const parametermap &parameters);
135 
148  [[nodiscard]]
149  inline answer_type get(const endpoint_variant &endpoint)
150  {
151  return get(endpoint, {});
152  }
153 
174  [[nodiscard]]
175  answer_type post(const endpoint_variant &endpoint,
176  const parametermap &parameters);
177 
185  [[nodiscard]]
186  inline answer_type post(const endpoint_variant &endpoint)
187  {
188  return post(endpoint, {});
189  }
190 
200  [[nodiscard]]
201  answer_type patch(const endpoint_variant &endpoint,
202  const parametermap &parameters);
203 
211  [[nodiscard]]
212  inline answer_type patch(const endpoint_variant &endpoint)
213  {
214  return patch(endpoint, {});
215  }
216 
226  [[nodiscard]]
227  answer_type put(const endpoint_variant &endpoint,
228  const parametermap &parameters);
229 
237  [[nodiscard]]
238  inline answer_type put(const endpoint_variant &endpoint)
239  {
240  return put(endpoint, {});
241  }
242 
252  [[nodiscard]]
253  answer_type del(const endpoint_variant &endpoint,
254  const parametermap &parameters);
255 
263  [[nodiscard]]
264  inline answer_type del(const endpoint_variant &endpoint)
265  {
266  return del(endpoint, {});
267  }
268 
280  string get_new_stream_contents();
281 
287  vector<event_type> get_new_events();
288 
290  inline void cancel_stream()
291  {
293  }
294 private:
295  const Instance &_instance;
296  const string_view _baseuri;
297 
298  [[nodiscard]]
299  string endpoint_to_uri(const endpoint_variant &endpoint) const;
300 };
301 
302 } // namespace mastodonpp
303 
304 #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::Instance::copy_connection_properties
void copy_connection_properties(CURLWrapper &curlwrapper) const
Set the properties of the connection of the calling class up.
Definition: instance.hpp:90
mastodonpp::Connection::Connection
Connection(const Instance &instance)
Construct a new Connection object.
Definition: connection.hpp:88
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:186
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::put
answer_type put(const endpoint_variant &endpoint)
Make a HTTP PUT call.
Definition: connection.hpp:238
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:115
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:212
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::~Connection
~Connection() noexcept override=default
Destructor.
mastodonpp::Connection::del
answer_type del(const endpoint_variant &endpoint)
Make a HTTP DELETE call.
Definition: connection.hpp:264
mastodonpp::Connection::cancel_stream
void cancel_stream()
Cancel the stream.
Definition: connection.hpp:290
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:79
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:48
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:78
mastodonpp::CURLWrapper
Handles the details of network connections.
Definition: curl_wrapper.hpp:58
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:212