mastodon-cpp  0.110.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...
 
enum  error {
  OK = 0, INVALID_ARGUMENT = 1, URL_CHANGED = 10, CONNECTION_TIMEOUT = 11,
  CONNECTION_REFUSED = 12, DNS = 13, ENCRYPTION = 14, UNKNOWN = 127
}
 

Functions

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

◆ urldecode()

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

Decodes a percent-encoded string.

    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 where necessary. 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