mastodonpp  0.5.7
C++ wrapper for the Mastodon and Pleroma APIs.
types.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 // Types that are used in more than one file.
18 
19 #ifndef MASTODONPP_TYPES_HPP
20 #define MASTODONPP_TYPES_HPP
21 
22 #include <cstdint>
23 #include <map>
24 #include <ostream>
25 #include <string>
26 #include <string_view>
27 #include <utility>
28 #include <variant>
29 #include <vector>
30 
31 namespace mastodonpp
32 {
33 
34 using std::map;
35 using std::ostream;
36 using std::pair;
37 using std::string;
38 using std::string_view;
39 using std::uint16_t;
40 using std::uint8_t;
41 using std::variant;
42 using std::vector;
43 
63 using parametermap = map<string_view,
64  variant<string_view, vector<string_view>>>;
65 
71 using parameterpair = pair<string_view,
72  variant<string_view, vector<string_view>>>;
73 
80 {
91  uint8_t curl_error_code{0};
92 
98  string error_message;
99 
105  uint16_t http_status{0};
106 
112  string headers;
113 
119  string body;
120 
127  explicit operator bool() const;
128 
134  explicit operator string_view() const;
135 
141  friend ostream &operator<<(ostream &out, const answer_type &answer);
142 
152  [[nodiscard]] string_view get_header(string_view field) const;
153 
161  [[nodiscard]] inline parametermap next() const
162  {
163  return parse_pagination(true);
164  }
165 
174  [[nodiscard]] inline parametermap prev() const
175  {
176  return parse_pagination(false);
177  }
178 
179 private:
188  [[nodiscard]] parametermap parse_pagination(bool next) const;
189 };
190 
191 } // namespace mastodonpp
192 
193 #endif // MASTODONPP_TYPES_HPP
C++ wrapper for the Mastodon API.
Definition: api.hpp:25
pair< string_view, variant< string_view, vector< string_view > >> parameterpair
A single parameter of a parametermap.
Definition: types.hpp:72
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
friend ostream & operator<<(ostream &out, const answer_type &answer)
Returns body as std::ostream.
Definition: types.cpp:40
uint16_t http_status
HTTP status code.
Definition: types.hpp:105
parametermap next() const
Returns the parameters needed for the next entries.
Definition: types.hpp:161
parametermap prev() const
Returns the parameters needed for the previous entries.
Definition: types.hpp:174
string_view get_header(string_view field) const
Returns the value of a header field.
Definition: types.cpp:46
uint8_t curl_error_code
The error code returned by libcurl.
Definition: types.hpp:91
string body
The response from the server, usually JSON.
Definition: types.hpp:119
string headers
The headers of the response from the server.
Definition: types.hpp:112
string error_message
The error message.
Definition: types.hpp:98