This repository has been archived on 2018-05-28. You can view files and clone it, but cannot push or open issues or pull requests.
BBB-Simple-ACS/client/request_delegate.cpp

42 lines
1.3 KiB
C++

#include "request_delegate.hpp"
#include "http_client.hpp"
#include "factory.hpp"
#include <json.hpp>
namespace acs
{
void request_delegate::response_handler(const std::string& response_body) const
{
if (response_body == "") {
callback_({ {}, {} });
return;
}
boost_helper::json response(response_body);
if (!response.has("err")) {
std::cerr << "Bad response. " << response_body << std::endl;
callback_({ {}, {} });
return;
}
if (response["err"].val() != "0") {
std::cerr << "Server responded with error " <<
response["err"].val() << response["data"].val() << std::endl;
callback_({ {}, {} });
return;
}
callback_({ response["data"]["token"].val(), response["data"]["stunum"].val() });
}
void request_delegate::execute(const callback_t& callback)
{
callback_ = callback;
// Unfortunately we can't DI client here.
// There's a bug in lower versions of Boost.Beast,
// resulting flat_buffer instance not being cleared properly.
delete client_;
client_ = new http_client;
client_->async_get(host, query, boost::bind(
&request_delegate::response_handler, this, _1));
}
}