#include "flatbuffer_schema.fbs.h"
namespace samples::fbs_handle {
class FbsSumEcho final
fbs::SampleResponse> {
public:
static constexpr std::string_view kName = "handler-fbs-sample";
: HttpHandlerFlatbufBase(config, context) {}
fbs::SampleResponse::NativeTableType HandleRequestFlatbufThrow(
const fbs::SampleRequest::NativeTableType& fbs_request,
fbs::SampleResponse::NativeTableType res;
res.sum = fbs_request.arg1 + fbs_request.arg2;
res.echo = fbs_request.data;
return res;
}
};
}
namespace samples::fbs_request {
public:
static constexpr std::string_view kName = "fbs-request";
: LoggableComponentBase(config, context),
http_client_{
context.FindComponent<
components::HttpClient>().GetHttpClient()},
task_{
utils::Async(
"requests", [this]() { KeepRequesting(); })} {}
void KeepRequesting() const;
private:
};
void FbsRequest::KeepRequesting() const {
fbs::SampleRequest::NativeTableType payload;
payload.arg1 = 20;
payload.arg2 = 22;
payload.data = "Hello word";
flatbuffers::FlatBufferBuilder fbb;
auto ret_fbb = fbs::SampleRequest::Pack(fbb, &payload);
fbb.Finish(ret_fbb);
std::string data(reinterpret_cast<const char*>(fbb.GetBufferPointer()),
fbb.GetSize());
const auto response = http_client_.CreateRequest()
.post("http://localhost:8084/fbs", std::move(data))
.timeout(std::chrono::seconds(1))
.retry(10)
.perform();
UASSERT_MSG(response->IsOk(),
"Sample should work well in tests");
const auto body = response->body_view();
const auto* response_fb =
flatbuffers::GetRoot<fbs::SampleResponse>(body.data());
flatbuffers::Verifier verifier(reinterpret_cast<const uint8_t*>(body.data()),
body.size());
UASSERT_MSG(response_fb->Verify(verifier),
"Broken flatbuf in sample");
fbs::SampleResponse::NativeTableType result;
response_fb->UnPackTo(&result);
UASSERT_MSG(result.sum == 42,
"Sample should work well in tests");
UASSERT_MSG(result.echo == payload.data,
"Sample should work well in tests");
}
}
int main(int argc, char* argv[]) {
.
Append<samples::fbs_handle::FbsSumEcho>()
.Append<clients::dns::Component>()
.Append<samples::fbs_request::FbsRequest>();
}