mastodon-cpp  0.106.0
Namespaces | Classes | Typedefs | Enumerations | Functions
Mastodon Namespace Reference

Collection of things to interface with server software that implements the Mastodon API. More...

Namespaces

 Easy
 Collection of things that make it easier to interface with server software that implements the Mastodon API.
 

Classes

class  API
 Interface to the Mastodon API. More...
 
struct  param
 A single parameter. More...
 
struct  parameters
 Vector of Mastodon::param, used for passing parameters in calls. More...
 
struct  return_base
 Basis for return types. More...
 
struct  return_call
 Return type for API calls. More...
 

Typedefs

typedef struct Mastodon::return_base return_base
 Basis for return types. More...
 
typedef Mastodon::return_call return_call
 Return type for API calls. More...
 
typedef struct Mastodon::param param
 A single parameter. More...
 
typedef Mastodon::parameters parameters
 Vector of Mastodon::param, used for passing parameters in calls. More...
 

Enumerations

enum  http_method {
  GET, PATCH, POST, PUT,
  DELETE, GET_STREAM
}
 HTTP methods. Used in API calls. More...
 

Functions

const string urlencode (const string &str)
 Percent-encodes a string. This is done automatically, unless you make a custom request. More...
 
const string urldecode (const string &str)
 Decodes a percent-encoded string. More...
 
const string unescape_html (const string &html)
 Replaces HTML entities with UTF-8 characters. More...
 
std::ostream & operator<< (std::ostream &out, const return_call &ret)
 

Detailed Description

Collection of things to interface with server software that implements the Mastodon API.

Typedef Documentation

◆ param

A single parameter.

Parameters
keyThe key as a string.
valuesThe values as a vector of strings.
Since
0.100.0

◆ parameters

Vector of Mastodon::param, used for passing parameters in calls.

The only difference to a std::vector<param> is the added member find.

Example:

{
{ "media_ids", { "1234", "4321" } },
{ "status", { "Hello world!" } }
};
Since
0.100.0

◆ return_base

Basis for return types.

Since
0.100.0

◆ return_call

Return type for API calls.

Example:

Mastodon::return_call ret = masto.get(Mastodon::API::v1::instance);
if (!ret) // Or ret.error_code != 0
{
cout << "Error " << std::to_string(ret.error_code);
cout << " (HTTP " << std::to_string(ret.http_error_code) << "): ";
cout << ret.error_message << endl
}
else
{
cout << ret << endl; // Or ret.answer
}
Since
0.100.0

Enumeration Type Documentation

◆ http_method

enum Mastodon::http_method
strong

HTTP methods. Used in API calls.

Since
0.100.0
88  {
89  GET,
90  PATCH,
91  POST,
92  PUT,
93  DELETE,
94  GET_STREAM
95  };

Function Documentation

◆ operator<<()

std::ostream& Mastodon::operator<< ( std::ostream &  out,
const return_call ret 
)
Since
0.100.0
44  {
45  out << ret.answer;
46  return out;
47  }

◆ unescape_html()

const string Mastodon::unescape_html ( const string &  html)

Replaces HTML entities with UTF-8 characters.

Supports named and numbered entities, decimal and hexadecimal.

Parameters
htmlThe html to unescape.
Returns
The unescaped string.
Since
0.105.0
334 {
335  string buffer = html;
336  string output = "";
337 
338  // Used to convert int to utf-8 char
339  std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> u8c;
340  // Matches numbered entities between 1 and 8 digits, decimal or hexadecimal
341  std::regex re_entity("&#(x)?([[:alnum:]]{1,8});");
342  std::smatch match;
343 
344  while (std::regex_search(buffer, match, re_entity))
345  {
346  char32_t codepoint = 0;
347  // 'x' in front of the number means it's hexadecimal, else decimal.
348  if (match[1].length() == 1)
349  {
350  codepoint = std::stoi(match[2].str(), nullptr, 16);
351  }
352  else
353  {
354  codepoint = std::stoi(match[2].str(), nullptr, 10);
355  }
356  output += match.prefix().str() + u8c.to_bytes(codepoint);
357  buffer = match.suffix().str();
358  }
359  output += buffer;
360 
361  // Source: https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_
362  // entity_references#Character_entity_references_in_HTML
363  const std::array<const std::pair<const string, const char32_t>, 258> names =
364  {{
365  { "exclamation", 0x0021 },
366  { "quot", 0x0022 },
367  { "percent", 0x0025 },
368  { "amp", 0x0026 },
369  { "apos", 0x0027 },
370  { "add", 0x002B },
371  { "lt", 0x003C },
372  { "equal", 0x003D },
373  { "gt", 0x003E },
374  { "nbsp", 0x00A0 },
375  { "iexcl", 0x00A1 },
376  { "cent", 0x00A2 },
377  { "pound", 0x00A3 },
378  { "curren", 0x00A4 },
379  { "yen", 0x00A5 },
380  { "brvbar", 0x00A6 },
381  { "sect", 0x00A7 },
382  { "uml", 0x00A8 },
383  { "copy", 0x00A9 },
384  { "ordf", 0x00AA },
385  { "laquo", 0x00AB },
386  { "not", 0x00AC },
387  { "shy", 0x00AD },
388  { "reg", 0x00AE },
389  { "macr", 0x00AF },
390  { "deg", 0x00B0 },
391  { "plusmn", 0x00B1 },
392  { "sup2", 0x00B2 },
393  { "sup3", 0x00B3 },
394  { "acute", 0x00B4 },
395  { "micro", 0x00B5 },
396  { "para", 0x00B6 },
397  { "middot", 0x00B7 },
398  { "cedil", 0x00B8 },
399  { "sup1", 0x00B9 },
400  { "ordm", 0x00BA },
401  { "raquo", 0x00BB },
402  { "frac14", 0x00BC },
403  { "frac12", 0x00BD },
404  { "frac34", 0x00BE },
405  { "iquest", 0x00BF },
406  { "Agrave", 0x00C0 },
407  { "Aacute", 0x00C1 },
408  { "Acirc", 0x00C2 },
409  { "Atilde", 0x00C3 },
410  { "Auml", 0x00C4 },
411  { "Aring", 0x00C5 },
412  { "AElig", 0x00C6 },
413  { "Ccedil", 0x00C7 },
414  { "Egrave", 0x00C8 },
415  { "Eacute", 0x00C9 },
416  { "Ecirc", 0x00CA },
417  { "Euml", 0x00CB },
418  { "Igrave", 0x00CC },
419  { "Iacute", 0x00CD },
420  { "Icirc", 0x00CE },
421  { "Iuml", 0x00CF },
422  { "ETH", 0x00D0 },
423  { "Ntilde", 0x00D1 },
424  { "Ograve", 0x00D2 },
425  { "Oacute", 0x00D3 },
426  { "Ocirc", 0x00D4 },
427  { "Otilde", 0x00D5 },
428  { "Ouml", 0x00D6 },
429  { "times", 0x00D7 },
430  { "Oslash", 0x00D8 },
431  { "Ugrave", 0x00D9 },
432  { "Uacute", 0x00DA },
433  { "Ucirc", 0x00DB },
434  { "Uuml", 0x00DC },
435  { "Yacute", 0x00DD },
436  { "THORN", 0x00DE },
437  { "szlig", 0x00DF },
438  { "agrave", 0x00E0 },
439  { "aacute", 0x00E1 },
440  { "acirc", 0x00E2 },
441  { "atilde", 0x00E3 },
442  { "auml", 0x00E4 },
443  { "aring", 0x00E5 },
444  { "aelig", 0x00E6 },
445  { "ccedil", 0x00E7 },
446  { "egrave", 0x00E8 },
447  { "eacute", 0x00E9 },
448  { "ecirc", 0x00EA },
449  { "euml", 0x00EB },
450  { "igrave", 0x00EC },
451  { "iacute", 0x00ED },
452  { "icirc", 0x00EE },
453  { "iuml", 0x00EF },
454  { "eth", 0x00F0 },
455  { "ntilde", 0x00F1 },
456  { "ograve", 0x00F2 },
457  { "oacute", 0x00F3 },
458  { "ocirc", 0x00F4 },
459  { "otilde", 0x00F5 },
460  { "ouml", 0x00F6 },
461  { "divide", 0x00F7 },
462  { "oslash", 0x00F8 },
463  { "ugrave", 0x00F9 },
464  { "uacute", 0x00FA },
465  { "ucirc", 0x00FB },
466  { "uuml", 0x00FC },
467  { "yacute", 0x00FD },
468  { "thorn", 0x00FE },
469  { "yuml", 0x00FF },
470  { "OElig", 0x0152 },
471  { "oelig", 0x0153 },
472  { "Scaron", 0x0160 },
473  { "scaron", 0x0161 },
474  { "Yuml", 0x0178 },
475  { "fnof", 0x0192 },
476  { "circ", 0x02C6 },
477  { "tilde", 0x02DC },
478  { "Alpha", 0x0391 },
479  { "Beta", 0x0392 },
480  { "Gamma", 0x0393 },
481  { "Delta", 0x0394 },
482  { "Epsilon", 0x0395 },
483  { "Zeta", 0x0396 },
484  { "Eta", 0x0397 },
485  { "Theta", 0x0398 },
486  { "Iota", 0x0399 },
487  { "Kappa", 0x039A },
488  { "Lambda", 0x039B },
489  { "Mu", 0x039C },
490  { "Nu", 0x039D },
491  { "Xi", 0x039E },
492  { "Omicron", 0x039F },
493  { "Pi", 0x03A0 },
494  { "Rho", 0x03A1 },
495  { "Sigma", 0x03A3 },
496  { "Tau", 0x03A4 },
497  { "Upsilon", 0x03A5 },
498  { "Phi", 0x03A6 },
499  { "Chi", 0x03A7 },
500  { "Psi", 0x03A8 },
501  { "Omega", 0x03A9 },
502  { "alpha", 0x03B1 },
503  { "beta", 0x03B2 },
504  { "gamma", 0x03B3 },
505  { "delta", 0x03B4 },
506  { "epsilon", 0x03B5 },
507  { "zeta", 0x03B6 },
508  { "eta", 0x03B7 },
509  { "theta", 0x03B8 },
510  { "iota", 0x03B9 },
511  { "kappa", 0x03BA },
512  { "lambda", 0x03BB },
513  { "mu", 0x03BC },
514  { "nu", 0x03BD },
515  { "xi", 0x03BE },
516  { "omicron", 0x03BF },
517  { "pi", 0x03C0 },
518  { "rho", 0x03C1 },
519  { "sigmaf", 0x03C2 },
520  { "sigma", 0x03C3 },
521  { "tau", 0x03C4 },
522  { "upsilon", 0x03C5 },
523  { "phi", 0x03C6 },
524  { "chi", 0x03C7 },
525  { "psi", 0x03C8 },
526  { "omega", 0x03C9 },
527  { "thetasym", 0x03D1 },
528  { "upsih", 0x03D2 },
529  { "piv", 0x03D6 },
530  { "ensp", 0x2002 },
531  { "emsp", 0x2003 },
532  { "thinsp", 0x2009 },
533  { "zwnj", 0x200C },
534  { "zwj", 0x200D },
535  { "lrm", 0x200E },
536  { "rlm", 0x200F },
537  { "ndash", 0x2013 },
538  { "mdash", 0x2014 },
539  { "horbar", 0x2015 },
540  { "lsquo", 0x2018 },
541  { "rsquo", 0x2019 },
542  { "sbquo", 0x201A },
543  { "ldquo", 0x201C },
544  { "rdquo", 0x201D },
545  { "bdquo", 0x201E },
546  { "dagger", 0x2020 },
547  { "Dagger", 0x2021 },
548  { "bull", 0x2022 },
549  { "hellip", 0x2026 },
550  { "permil", 0x2030 },
551  { "prime", 0x2032 },
552  { "Prime", 0x2033 },
553  { "lsaquo", 0x2039 },
554  { "rsaquo", 0x203A },
555  { "oline", 0x203E },
556  { "frasl", 0x2044 },
557  { "euro", 0x20AC },
558  { "image", 0x2111 },
559  { "weierp", 0x2118 },
560  { "real", 0x211C },
561  { "trade", 0x2122 },
562  { "alefsym", 0x2135 },
563  { "larr", 0x2190 },
564  { "uarr", 0x2191 },
565  { "rarr", 0x2192 },
566  { "darr", 0x2193 },
567  { "harr", 0x2194 },
568  { "crarr", 0x21B5 },
569  { "lArr", 0x21D0 },
570  { "uArr", 0x21D1 },
571  { "rArr", 0x21D2 },
572  { "dArr", 0x21D3 },
573  { "hArr", 0x21D4 },
574  { "forall", 0x2200 },
575  { "part", 0x2202 },
576  { "exist", 0x2203 },
577  { "empty", 0x2205 },
578  { "nabla", 0x2207 },
579  { "isin", 0x2208 },
580  { "notin", 0x2209 },
581  { "ni", 0x220B },
582  { "prod", 0x220F },
583  { "sum", 0x2211 },
584  { "minus", 0x2212 },
585  { "lowast", 0x2217 },
586  { "radic", 0x221A },
587  { "prop", 0x221D },
588  { "infin", 0x221E },
589  { "ang", 0x2220 },
590  { "and", 0x2227 },
591  { "or", 0x2228 },
592  { "cap", 0x2229 },
593  { "cup", 0x222A },
594  { "int", 0x222B },
595  { "there4", 0x2234 },
596  { "sim", 0x223C },
597  { "cong", 0x2245 },
598  { "asymp", 0x2248 },
599  { "ne", 0x2260 },
600  { "equiv", 0x2261 },
601  { "le", 0x2264 },
602  { "ge", 0x2265 },
603  { "sub", 0x2282 },
604  { "sup", 0x2283 },
605  { "nsub", 0x2284 },
606  { "sube", 0x2286 },
607  { "supe", 0x2287 },
608  { "oplus", 0x2295 },
609  { "otimes", 0x2297 },
610  { "perp", 0x22A5 },
611  { "sdot", 0x22C5 },
612  { "lceil", 0x2308 },
613  { "rceil", 0x2309 },
614  { "lfloor", 0x230A },
615  { "rfloor", 0x230B },
616  { "lang", 0x2329 },
617  { "rang", 0x232A },
618  { "loz", 0x25CA },
619  { "spades", 0x2660 },
620  { "clubs", 0x2663 },
621  { "hearts", 0x2665 },
622  { "diams", 0x2666 }
623  }};
624 
625  for (auto &pair : names)
626  {
627  const std::regex re('&' + pair.first + ';');
628  output = std::regex_replace(output, re, u8c.to_bytes(pair.second));
629  }
630 
631  return output;
632 }

◆ urldecode()

const string Mastodon::urldecode ( const string &  str)

Decodes a percent-encoded string.

Calls curlpp::unescape(str).

See RFC 3986 section 2.1 for more info.

Parameters
strThe string to decode.
Returns
The decoded string.
Since
0.105.0

◆ urlencode()

const string Mastodon::urlencode ( const string &  str)

Percent-encodes a string. This is done automatically, unless you make a custom request.

Calls curlpp::escape(str).

The only time you should use this, is if you use get(const string &call, string &answer).

See RFC 3986 section 2.1 for more info.

Parameters
strThe string to encode.
Returns
The percent-encoded string.
Since
0.105.0