Separate json SN parsing definition/declaration

No changes to code here (committing this separately to make the changes
in the following commit easier to follow).
This commit is contained in:
Jason Rhinelander 2020-01-20 16:43:27 -04:00
parent 5be1865318
commit 1ba1e04390

View File

@ -110,46 +110,7 @@ namespace llarp
} }
bool bool
HandleJSONResult(const nlohmann::json& result) override HandleJSONResult(const nlohmann::json& result) override;
{
PubkeyList_t keys;
if(not result.is_object())
{
LogWarn("Invalid result: not an object");
handler({}, false);
return false;
}
const auto itr = result.find("service_node_states");
if(itr == result.end())
{
LogWarn("Invalid result: no service_node_states member");
handler({}, false);
return false;
}
if(not itr.value().is_array())
{
LogWarn("Invalid result: service_node_states is not an array");
handler({}, false);
return false;
}
for(const auto item : itr.value())
{
if(not item.is_object())
continue;
if(not item.value("active", false))
continue;
if(not item.value("funded", false))
continue;
const std::string pk = item.value("pubkey_ed25519", "");
if(pk.empty())
continue;
PubKey k;
if(k.FromString(pk))
keys.emplace_back(std::move(k));
}
handler(keys, not keys.empty());
return true;
}
void void
HandleError() override HandleError() override
@ -253,6 +214,48 @@ namespace llarp
~CallerImpl() = default; ~CallerImpl() = default;
}; };
bool
GetServiceNodeListHandler::HandleJSONResult(const nlohmann::json& result)
{
PubkeyList_t keys;
if(not result.is_object())
{
LogWarn("Invalid result: not an object");
handler({}, false);
return false;
}
const auto itr = result.find("service_node_states");
if(itr == result.end())
{
LogWarn("Invalid result: no service_node_states member");
handler({}, false);
return false;
}
if(not itr.value().is_array())
{
LogWarn("Invalid result: service_node_states is not an array");
handler({}, false);
return false;
}
for(const auto item : itr.value())
{
if(not item.is_object())
continue;
if(not item.value("active", false))
continue;
if(not item.value("funded", false))
continue;
const std::string pk = item.value("pubkey_ed25519", "");
if(pk.empty())
continue;
PubKey k;
if(k.FromString(pk))
keys.emplace_back(std::move(k));
}
handler(keys, not keys.empty());
return true;
}
void void
CallerHandler::PopulateReqHeaders(abyss::http::Headers_t& hdr) CallerHandler::PopulateReqHeaders(abyss::http::Headers_t& hdr)
{ {