mastodonpp  0.2.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 "answer.hpp"
21 #include "api.hpp"
22 #include "curl_wrapper.hpp"
23 #include "instance.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 
105  [[nodiscard]]
106  answer_type get(const endpoint_variant &endpoint,
107  const parametermap &parameters);
108 
121  [[nodiscard]]
122  inline answer_type get(const endpoint_variant &endpoint)
123  {
124  return get(endpoint, {});
125  }
126 
147  [[nodiscard]]
148  answer_type post(const endpoint_variant &endpoint,
149  const parametermap &parameters);
150 
158  [[nodiscard]]
159  inline answer_type post(const endpoint_variant &endpoint)
160  {
161  return post(endpoint, {});
162  }
163 
173  [[nodiscard]]
174  answer_type patch(const endpoint_variant &endpoint,
175  const parametermap &parameters);
176 
184  [[nodiscard]]
185  inline answer_type patch(const endpoint_variant &endpoint)
186  {
187  return patch(endpoint, {});
188  }
189 
199  [[nodiscard]]
200  answer_type put(const endpoint_variant &endpoint,
201  const parametermap &parameters);
202 
210  [[nodiscard]]
211  inline answer_type put(const endpoint_variant &endpoint)
212  {
213  return put(endpoint, {});
214  }
215 
225  [[nodiscard]]
226  answer_type del(const endpoint_variant &endpoint,
227  const parametermap &parameters);
228 
236  [[nodiscard]]
237  inline answer_type del(const endpoint_variant &endpoint)
238  {
239  return del(endpoint, {});
240  }
241 
253  string get_new_stream_contents();
254 
260  vector<event_type> get_new_events();
261 
263  inline void cancel_stream()
264  {
266  }
267 private:
268  Instance &_instance;
269  const string_view _baseuri;
270 
271  [[nodiscard]]
272  string endpoint_to_uri(const endpoint_variant &endpoint) const;
273 };
274 
275 } // namespace mastodonpp
276 
277 #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:50
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:159
mastodonpp::parametermap
map< string_view, variant< string_view, vector< string_view > >> parametermap
std::map of parameters for API calls.
Definition: curl_wrapper.hpp:77
mastodonpp::Connection::Connection
Connection(Instance &instance)
Construct a new Connection object.
Definition: connection.cpp:24
mastodonpp::Connection::put
answer_type put(const endpoint_variant &endpoint)
Make a HTTP PUT call.
Definition: connection.hpp:211
mastodonpp::Connection::get
answer_type get(const endpoint_variant &endpoint)
Make a HTTP GET call.
Definition: connection.hpp:122
mastodonpp::event_type::data
string data
The payload.
Definition: connection.hpp:65
mastodonpp
C++ wrapper for the Mastodon API.
Definition: answer.cpp:22
mastodonpp::Connection::patch
answer_type patch(const endpoint_variant &endpoint)
Make a HTTP PATCH call.
Definition: connection.hpp:185
mastodonpp::Connection::put
answer_type put(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP PUT call with parameters.
Definition: connection.cpp:71
mastodonpp::Connection::get_new_events
vector< event_type > get_new_events()
Get new stream events.
Definition: connection.cpp:95
mastodonpp::Connection::del
answer_type del(const endpoint_variant &endpoint)
Make a HTTP DELETE call.
Definition: connection.hpp:237
mastodonpp::Connection::cancel_stream
void cancel_stream()
Cancel the stream.
Definition: connection.hpp:263
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:78
mastodonpp::event_type
A stream event.
Definition: connection.hpp:52
mastodonpp::answer_type
Return type for Requests.
Definition: answer.hpp:40
mastodonpp::Connection::patch
answer_type patch(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP PATCH call with parameters.
Definition: connection.cpp:64
mastodonpp::Instance
Holds the access data of an instance.
Definition: instance.hpp:42
mastodonpp::Connection::post
answer_type post(const endpoint_variant &endpoint, const parametermap &parameters)
Make a HTTP POST call with parameters.
Definition: connection.cpp:57
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:96
mastodonpp::Connection::get_new_stream_contents
string get_new_stream_contents()
Copy new stream contents and delete the “original”.
Definition: connection.cpp:85
mastodonpp::CURLWrapper::cancel_stream
void cancel_stream()
Cancel the stream.
Definition: curl_wrapper.hpp:204