mastodonpp  0.4.0
Classes | Typedefs | Enumerations | Functions
mastodonpp Namespace Reference

C++ wrapper for the Mastodon API. More...

Classes

struct  answer_type
 Return type for Requests. More...
 
class  API
 Holds API endpoints. More...
 
class  Connection
 Represents a connection to an instance. Used for requests. More...
 
class  CURLException
 Exception for libcurl errors. More...
 
class  CURLWrapper
 Handles the details of network connections. More...
 
struct  event_type
 A stream event. More...
 
class  Instance
 Holds the access data of an instance. More...
 

Typedefs

using endpoint_variant = variant< API::endpoint_type, string_view >
 An endpoint. Either API::endpoint_type or std::string_view. More...
 
using parametermap = map< string_view, variant< string_view, vector< string_view > >>
 std::map of parameters for API calls. More...
 
using parameterpair = pair< string_view, variant< string_view, vector< string_view > >>
 A single parameter of a parametermap. More...
 

Enumerations

enum  http_method {
  GET, POST, PATCH, PUT,
  DELETE
}
 The HTTP method. More...
 

Functions

string unescape_html (string html)
 Replaces HTML entities with UTF-8 characters. More...
 
std::ostream & operator<< (std::ostream &out, const answer_type &answer)
 

Detailed Description

C++ wrapper for the Mastodon API.

Since
0.1.0

Typedef Documentation

◆ endpoint_variant

using mastodonpp::endpoint_variant = typedef variant<API::endpoint_type,string_view>

An endpoint. Either API::endpoint_type or std::string_view.

Since
0.1.0

◆ parametermap

using mastodonpp::parametermap = typedef map<string_view, variant<string_view, vector<string_view> >>

std::map of parameters for API calls.

Note that arrays always have to be specified as vectors, even if they have only 1 element. To send a file, use “@file:” followed by the file name as value.

Example:

parametermap parameters
{
{"poll[expires_in]", "86400"},
{"poll[options]", vector<string_view>{"Yes", "No", "Maybe"}},
{"status", "How is the weather?"}
};
Since
0.1.0

◆ parameterpair

using mastodonpp::parameterpair = typedef pair<string_view, variant<string_view, vector<string_view> >>

A single parameter of a parametermap.

Since
0.1.0

Enumeration Type Documentation

◆ http_method

The HTTP method.

Since
0.1.0
41 {
42  GET,
43  POST,
44  PATCH,
45  PUT,
46  DELETE
47 };

Function Documentation

◆ operator<<()

std::ostream& mastodonpp::operator<< ( std::ostream &  out,
const answer_type answer 
)
Since
0.1.0
40 {
41  out << answer.body;
42  return out;
43 }

◆ unescape_html()

string mastodonpp::unescape_html ( string  html)

Replaces HTML entities with UTF-8 characters.

Supports named and numbered entities, decimal and hexadecimal.

Example:

// Will output: 2€ = 2€ = 2€
std::cout << mastodonpp::unescape_html("2&euro; = 2&#8364; = 2&#x20ac;");
Parameters
htmlThe HTML to unescape.
Since
0.4.0
41 {
42  string buffer{move(html)};
43  string output;
44 
45  // Used to convert int to utf-8 char.
46  wstring_convert<codecvt_utf8<char32_t>, char32_t> u8c;
47  // Matches numbered entities between 1 and 8 digits, decimal or hexadecimal.
48  const regex re_entity{"&#(x)?([[:alnum:]]{1,8});"};
49  smatch match;
50 
51  while (regex_search(buffer, match, re_entity))
52  {
53  const char32_t codepoint{[&match]
54  {
55  // 'x' in front of the number means it's hexadecimal, else decimal.
56  if (match[1].length() == 1)
57  {
58  return static_cast<char32_t>(stol(match[2].str(), nullptr, 16));
59  }
60  return static_cast<char32_t>(stol(match[2].str(), nullptr, 10));
61  }()};
62  output += match.prefix().str() + u8c.to_bytes(codepoint);
63  buffer = match.suffix().str();
64  }
65  output += buffer;
66 
67  // Source: https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_
68  // entity_references#Character_entity_references_in_HTML
69  constexpr array<const pair<const string_view, const char32_t>, 258> names
70  {{
71  { "exclamation", 0x0021 },
72  { "quot", 0x0022 },
73  { "percent", 0x0025 },
74  { "amp", 0x0026 },
75  { "apos", 0x0027 },
76  { "add", 0x002B },
77  { "lt", 0x003C },
78  { "equal", 0x003D },
79  { "gt", 0x003E },
80  { "nbsp", 0x00A0 },
81  { "iexcl", 0x00A1 },
82  { "cent", 0x00A2 },
83  { "pound", 0x00A3 },
84  { "curren", 0x00A4 },
85  { "yen", 0x00A5 },
86  { "brvbar", 0x00A6 },
87  { "sect", 0x00A7 },
88  { "uml", 0x00A8 },
89  { "copy", 0x00A9 },
90  { "ordf", 0x00AA },
91  { "laquo", 0x00AB },
92  { "not", 0x00AC },
93  { "shy", 0x00AD },
94  { "reg", 0x00AE },
95  { "macr", 0x00AF },
96  { "deg", 0x00B0 },
97  { "plusmn", 0x00B1 },
98  { "sup2", 0x00B2 },
99  { "sup3", 0x00B3 },
100  { "acute", 0x00B4 },
101  { "micro", 0x00B5 },
102  { "para", 0x00B6 },
103  { "middot", 0x00B7 },
104  { "cedil", 0x00B8 },
105  { "sup1", 0x00B9 },
106  { "ordm", 0x00BA },
107  { "raquo", 0x00BB },
108  { "frac14", 0x00BC },
109  { "frac12", 0x00BD },
110  { "frac34", 0x00BE },
111  { "iquest", 0x00BF },
112  { "Agrave", 0x00C0 },
113  { "Aacute", 0x00C1 },
114  { "Acirc", 0x00C2 },
115  { "Atilde", 0x00C3 },
116  { "Auml", 0x00C4 },
117  { "Aring", 0x00C5 },
118  { "AElig", 0x00C6 },
119  { "Ccedil", 0x00C7 },
120  { "Egrave", 0x00C8 },
121  { "Eacute", 0x00C9 },
122  { "Ecirc", 0x00CA },
123  { "Euml", 0x00CB },
124  { "Igrave", 0x00CC },
125  { "Iacute", 0x00CD },
126  { "Icirc", 0x00CE },
127  { "Iuml", 0x00CF },
128  { "ETH", 0x00D0 },
129  { "Ntilde", 0x00D1 },
130  { "Ograve", 0x00D2 },
131  { "Oacute", 0x00D3 },
132  { "Ocirc", 0x00D4 },
133  { "Otilde", 0x00D5 },
134  { "Ouml", 0x00D6 },
135  { "times", 0x00D7 },
136  { "Oslash", 0x00D8 },
137  { "Ugrave", 0x00D9 },
138  { "Uacute", 0x00DA },
139  { "Ucirc", 0x00DB },
140  { "Uuml", 0x00DC },
141  { "Yacute", 0x00DD },
142  { "THORN", 0x00DE },
143  { "szlig", 0x00DF },
144  { "agrave", 0x00E0 },
145  { "aacute", 0x00E1 },
146  { "acirc", 0x00E2 },
147  { "atilde", 0x00E3 },
148  { "auml", 0x00E4 },
149  { "aring", 0x00E5 },
150  { "aelig", 0x00E6 },
151  { "ccedil", 0x00E7 },
152  { "egrave", 0x00E8 },
153  { "eacute", 0x00E9 },
154  { "ecirc", 0x00EA },
155  { "euml", 0x00EB },
156  { "igrave", 0x00EC },
157  { "iacute", 0x00ED },
158  { "icirc", 0x00EE },
159  { "iuml", 0x00EF },
160  { "eth", 0x00F0 },
161  { "ntilde", 0x00F1 },
162  { "ograve", 0x00F2 },
163  { "oacute", 0x00F3 },
164  { "ocirc", 0x00F4 },
165  { "otilde", 0x00F5 },
166  { "ouml", 0x00F6 },
167  { "divide", 0x00F7 },
168  { "oslash", 0x00F8 },
169  { "ugrave", 0x00F9 },
170  { "uacute", 0x00FA },
171  { "ucirc", 0x00FB },
172  { "uuml", 0x00FC },
173  { "yacute", 0x00FD },
174  { "thorn", 0x00FE },
175  { "yuml", 0x00FF },
176  { "OElig", 0x0152 },
177  { "oelig", 0x0153 },
178  { "Scaron", 0x0160 },
179  { "scaron", 0x0161 },
180  { "Yuml", 0x0178 },
181  { "fnof", 0x0192 },
182  { "circ", 0x02C6 },
183  { "tilde", 0x02DC },
184  { "Alpha", 0x0391 },
185  { "Beta", 0x0392 },
186  { "Gamma", 0x0393 },
187  { "Delta", 0x0394 },
188  { "Epsilon", 0x0395 },
189  { "Zeta", 0x0396 },
190  { "Eta", 0x0397 },
191  { "Theta", 0x0398 },
192  { "Iota", 0x0399 },
193  { "Kappa", 0x039A },
194  { "Lambda", 0x039B },
195  { "Mu", 0x039C },
196  { "Nu", 0x039D },
197  { "Xi", 0x039E },
198  { "Omicron", 0x039F },
199  { "Pi", 0x03A0 },
200  { "Rho", 0x03A1 },
201  { "Sigma", 0x03A3 },
202  { "Tau", 0x03A4 },
203  { "Upsilon", 0x03A5 },
204  { "Phi", 0x03A6 },
205  { "Chi", 0x03A7 },
206  { "Psi", 0x03A8 },
207  { "Omega", 0x03A9 },
208  { "alpha", 0x03B1 },
209  { "beta", 0x03B2 },
210  { "gamma", 0x03B3 },
211  { "delta", 0x03B4 },
212  { "epsilon", 0x03B5 },
213  { "zeta", 0x03B6 },
214  { "eta", 0x03B7 },
215  { "theta", 0x03B8 },
216  { "iota", 0x03B9 },
217  { "kappa", 0x03BA },
218  { "lambda", 0x03BB },
219  { "mu", 0x03BC },
220  { "nu", 0x03BD },
221  { "xi", 0x03BE },
222  { "omicron", 0x03BF },
223  { "pi", 0x03C0 },
224  { "rho", 0x03C1 },
225  { "sigmaf", 0x03C2 },
226  { "sigma", 0x03C3 },
227  { "tau", 0x03C4 },
228  { "upsilon", 0x03C5 },
229  { "phi", 0x03C6 },
230  { "chi", 0x03C7 },
231  { "psi", 0x03C8 },
232  { "omega", 0x03C9 },
233  { "thetasym", 0x03D1 },
234  { "upsih", 0x03D2 },
235  { "piv", 0x03D6 },
236  { "ensp", 0x2002 },
237  { "emsp", 0x2003 },
238  { "thinsp", 0x2009 },
239  { "zwnj", 0x200C },
240  { "zwj", 0x200D },
241  { "lrm", 0x200E },
242  { "rlm", 0x200F },
243  { "ndash", 0x2013 },
244  { "mdash", 0x2014 },
245  { "horbar", 0x2015 },
246  { "lsquo", 0x2018 },
247  { "rsquo", 0x2019 },
248  { "sbquo", 0x201A },
249  { "ldquo", 0x201C },
250  { "rdquo", 0x201D },
251  { "bdquo", 0x201E },
252  { "dagger", 0x2020 },
253  { "Dagger", 0x2021 },
254  { "bull", 0x2022 },
255  { "hellip", 0x2026 },
256  { "permil", 0x2030 },
257  { "prime", 0x2032 },
258  { "Prime", 0x2033 },
259  { "lsaquo", 0x2039 },
260  { "rsaquo", 0x203A },
261  { "oline", 0x203E },
262  { "frasl", 0x2044 },
263  { "euro", 0x20AC },
264  { "image", 0x2111 },
265  { "weierp", 0x2118 },
266  { "real", 0x211C },
267  { "trade", 0x2122 },
268  { "alefsym", 0x2135 },
269  { "larr", 0x2190 },
270  { "uarr", 0x2191 },
271  { "rarr", 0x2192 },
272  { "darr", 0x2193 },
273  { "harr", 0x2194 },
274  { "crarr", 0x21B5 },
275  { "lArr", 0x21D0 },
276  { "uArr", 0x21D1 },
277  { "rArr", 0x21D2 },
278  { "dArr", 0x21D3 },
279  { "hArr", 0x21D4 },
280  { "forall", 0x2200 },
281  { "part", 0x2202 },
282  { "exist", 0x2203 },
283  { "empty", 0x2205 },
284  { "nabla", 0x2207 },
285  { "isin", 0x2208 },
286  { "notin", 0x2209 },
287  { "ni", 0x220B },
288  { "prod", 0x220F },
289  { "sum", 0x2211 },
290  { "minus", 0x2212 },
291  { "lowast", 0x2217 },
292  { "radic", 0x221A },
293  { "prop", 0x221D },
294  { "infin", 0x221E },
295  { "ang", 0x2220 },
296  { "and", 0x2227 },
297  { "or", 0x2228 },
298  { "cap", 0x2229 },
299  { "cup", 0x222A },
300  { "int", 0x222B },
301  { "there4", 0x2234 },
302  { "sim", 0x223C },
303  { "cong", 0x2245 },
304  { "asymp", 0x2248 },
305  { "ne", 0x2260 },
306  { "equiv", 0x2261 },
307  { "le", 0x2264 },
308  { "ge", 0x2265 },
309  { "sub", 0x2282 },
310  { "sup", 0x2283 },
311  { "nsub", 0x2284 },
312  { "sube", 0x2286 },
313  { "supe", 0x2287 },
314  { "oplus", 0x2295 },
315  { "otimes", 0x2297 },
316  { "perp", 0x22A5 },
317  { "sdot", 0x22C5 },
318  { "lceil", 0x2308 },
319  { "rceil", 0x2309 },
320  { "lfloor", 0x230A },
321  { "rfloor", 0x230B },
322  { "lang", 0x2329 },
323  { "rang", 0x232A },
324  { "loz", 0x25CA },
325  { "spades", 0x2660 },
326  { "clubs", 0x2663 },
327  { "hearts", 0x2665 },
328  { "diams", 0x2666 }
329  }};
330 
331  for (const auto &pair : names)
332  {
333  const regex re((string("&") += pair.first) += ';');
334  output = regex_replace(output, re, u8c.to_bytes(pair.second));
335  }
336 
337  return output;
338 }
mastodonpp::parametermap
map< string_view, variant< string_view, vector< string_view > >> parametermap
std::map of parameters for API calls.
Definition: types.hpp:66
mastodonpp::unescape_html
string unescape_html(string html)
Replaces HTML entities with UTF-8 characters.
Definition: helpers.cpp:40