diff options
author | tastytea | 2020-01-12 15:27:11 +0100 |
---|---|---|
committer | tastytea | 2020-01-12 15:28:26 +0100 |
commit | 79c5087ca57de629badb72580a296231b242d849 (patch) | |
tree | 081f3e7c13b9ea2b4147ac4f7d533f3d244c284a /src | |
parent | 7fc19639b16f4f2c08a30f15ab44b9efaba65835 (diff) | |
download | mastodonpp-79c5087ca57de629badb72580a296231b242d849.tar mastodonpp-79c5087ca57de629badb72580a296231b242d849.tar.gz mastodonpp-79c5087ca57de629badb72580a296231b242d849.zip |
Marked some functions noexcept.
Diffstat (limited to 'src')
-rw-r--r-- | src/instance.cpp | 114 |
1 files changed, 72 insertions, 42 deletions
diff --git a/src/instance.cpp b/src/instance.cpp index 172489c..cc8ffeb 100644 --- a/src/instance.cpp +++ b/src/instance.cpp @@ -35,46 +35,48 @@ Instance::Instance(const string_view hostname, const string_view access_token) , _max_chars{0} {} -uint64_t Instance::get_max_chars() +uint64_t Instance::get_max_chars() noexcept { constexpr uint64_t default_max_chars{500}; - if (_max_chars == 0) + if (_max_chars != 0) { - try - { - debuglog << "Querying " << _hostname << " for max_toot_chars…\n"; - const auto answer{make_request(http_method::GET, - _baseuri + "/api/v1/instance", {})}; - if (!answer) - { - debuglog << "Could not get instance info.\n"; - return default_max_chars; - } + return _max_chars; + } - _max_chars = [&answer] - { - auto &body{answer.body}; - size_t pos_start{body.find("max_toot_chars")}; - if (pos_start == string::npos) - { - debuglog << "max_toot_chars not found.\n"; - return default_max_chars; - } - pos_start = body.find(':', pos_start) + 1; - const size_t pos_end{body.find(',', pos_start)}; - - const auto max_toot_chars{body.substr(pos_start, - pos_end - pos_start)}; - return static_cast<uint64_t>(stoull(max_toot_chars)); - }(); - debuglog << "Set _max_chars to: " << _max_chars << '\n'; - } - catch (const exception &e) + try + { + debuglog << "Querying " << _hostname << " for max_toot_chars…\n"; + const auto answer{make_request(http_method::GET, + _baseuri + "/api/v1/instance", {})}; + if (!answer) { - debuglog << "Unexpected exception: " << e.what() << '\n'; + debuglog << "Could not get instance info.\n"; return default_max_chars; } + + _max_chars = [&answer] + { + auto &body{answer.body}; + size_t pos_start{body.find("max_toot_chars")}; + if (pos_start == string::npos) + { + debuglog << "max_toot_chars not found.\n"; + return default_max_chars; + } + pos_start = body.find(':', pos_start) + 1; + const size_t pos_end{body.find(',', pos_start)}; + + const auto max_toot_chars{body.substr(pos_start, + pos_end - pos_start)}; + return static_cast<uint64_t>(stoull(max_toot_chars)); + }(); + debuglog << "Set _max_chars to: " << _max_chars << '\n'; + } + catch (const exception &e) + { + debuglog << "Unexpected exception: " << e.what() << '\n'; + return default_max_chars; } return _max_chars; @@ -107,7 +109,7 @@ answer_type Instance::get_nodeinfo() return make_request(http_method::GET, hrefs.back(), {}); } -vector<string> Instance::get_post_formats() +vector<string> Instance::get_post_formats() noexcept { constexpr auto default_value{"text/plain"}; @@ -116,18 +118,46 @@ vector<string> Instance::get_post_formats() return _post_formats; } - debuglog << "Querying " << _hostname << " for postFormats…\n"; - const auto answer{get_nodeinfo()}; - if (!answer) + try { - debuglog << "Couldn't get NodeInfo.\n"; - _post_formats = {default_value}; - return _post_formats; + debuglog << "Querying " << _hostname << " for postFormats…\n"; + const auto answer{get_nodeinfo()}; + if (!answer) + { + debuglog << "Couldn't get NodeInfo.\n"; + _post_formats = {default_value}; + return _post_formats; + } + + constexpr string_view searchstring{R"("postFormats":[)"}; + auto pos{answer.body.find(searchstring)}; + if (pos == string::npos) + { + debuglog << "Couldn't find metadata.postFormats.\n"; + _post_formats = {default_value}; + return _post_formats; + } + pos += searchstring.size(); + auto endpos{answer.body.find("],", pos)}; + string formats{answer.body.substr(pos, endpos - pos)}; + debuglog << "Extracted postFormats: " << formats << '\n'; + + while ((pos = formats.find('"', 1)) != string::npos) + { + _post_formats.push_back(formats.substr(1, pos - 1)); + formats.erase(0, pos + 2); // 2 is the length of: ", + debuglog << "Found postFormat: " << _post_formats.back() << '\n'; + } + } + catch (const std::exception &e) + { + debuglog << "Unexpected exception: " << e.what() << '\n'; + return {default_value}; } - constexpr string_view searchstring{R"("postFormats":[)"}; - auto pos{answer.body.find(searchstring)}; - if (pos == string::npos) + return _post_formats; +} + { debuglog << "Couldn't find metadata.postFormats.\n"; _post_formats = {default_value}; |