Add Instance::get_nodeinfo().
This commit is contained in:
parent
acd6da37f2
commit
b9bf28c264
|
@ -18,6 +18,7 @@
|
||||||
#define MASTODONPP_INSTANCE_HPP
|
#define MASTODONPP_INSTANCE_HPP
|
||||||
|
|
||||||
#include "curl_wrapper.hpp"
|
#include "curl_wrapper.hpp"
|
||||||
|
#include "answer.hpp"
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -106,6 +107,9 @@ public:
|
||||||
* Queries `/api/v1/instance` for `max_toot_chars'. If the instance doesn't
|
* Queries `/api/v1/instance` for `max_toot_chars'. If the instance doesn't
|
||||||
* support it, the limit is assumed to be 500.
|
* support it, the limit is assumed to be 500.
|
||||||
*
|
*
|
||||||
|
* After the first call, the value is saved internally. Subsequent calls
|
||||||
|
* return the saved value.
|
||||||
|
*
|
||||||
* @since 0.1.0
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
|
@ -135,6 +139,18 @@ public:
|
||||||
return _proxy;
|
return _proxy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @brief Returns the NodeInfo of the instance.
|
||||||
|
*
|
||||||
|
* Attempts to download the [NodeInfo]
|
||||||
|
* (https://nodeinfo.diaspora.software/protocol.html) of the instance and
|
||||||
|
* returns it. Not every instance has it.
|
||||||
|
*
|
||||||
|
* @since 0.3.0
|
||||||
|
*/
|
||||||
|
[[nodiscard]]
|
||||||
|
answer_type get_nodeinfo();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const string _hostname;
|
const string _hostname;
|
||||||
const string _baseuri;
|
const string _baseuri;
|
||||||
|
|
|
@ -18,13 +18,17 @@
|
||||||
#include "instance.hpp"
|
#include "instance.hpp"
|
||||||
#include "log.hpp"
|
#include "log.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace mastodonpp
|
namespace mastodonpp
|
||||||
{
|
{
|
||||||
|
|
||||||
|
using std::sort;
|
||||||
using std::stoull;
|
using std::stoull;
|
||||||
using std::exception;
|
using std::exception;
|
||||||
|
using std::vector;
|
||||||
|
|
||||||
Instance::Instance(const string_view hostname, const string_view access_token)
|
Instance::Instance(const string_view hostname, const string_view access_token)
|
||||||
: _hostname{hostname}
|
: _hostname{hostname}
|
||||||
|
@ -77,4 +81,31 @@ uint64_t Instance::get_max_chars()
|
||||||
return _max_chars;
|
return _max_chars;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
answer_type Instance::get_nodeinfo()
|
||||||
|
{
|
||||||
|
debuglog << "Finding location of NodeInfo on " << _hostname << "…\n";
|
||||||
|
auto answer{make_request(http_method::GET,
|
||||||
|
_baseuri + "/.well-known/nodeinfo", {})};
|
||||||
|
if (!answer)
|
||||||
|
{
|
||||||
|
debuglog << "NodeInfo not found.\n";
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t pos{0};
|
||||||
|
vector<string> hrefs;
|
||||||
|
constexpr string_view searchstring{R"("href":")"};
|
||||||
|
while ((pos = answer.body.find(searchstring, pos)) != string::npos)
|
||||||
|
{
|
||||||
|
pos += searchstring.size();
|
||||||
|
auto endpos{answer.body.find('"', pos)};
|
||||||
|
hrefs.push_back(answer.body.substr(pos, endpos - pos));
|
||||||
|
debuglog << "Found href: " << hrefs.back() << '\n';
|
||||||
|
}
|
||||||
|
sort(hrefs.begin(), hrefs.end()); // We assume they are sortable strings.
|
||||||
|
debuglog << "Selecting href: " << hrefs.back() << '\n';
|
||||||
|
|
||||||
|
return make_request(http_method::GET, hrefs.back(), {});
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace mastodonpp
|
} // namespace mastodonpp
|
||||||
|
|
Loading…
Reference in New Issue
Block a user