JSON API expose retroshare version

Added a couple of macro to reduce boilerplate in manually written API
wrappers, use them in auto-generated wrappers too
This commit is contained in:
Gioacchino Mazzurco 2018-09-12 00:35:42 +02:00
parent 92f90178c4
commit 5245765964
No known key found for this signature in database
GPG key ID: A1FBCA3872E87051
3 changed files with 62 additions and 67 deletions

View file

@ -31,19 +31,7 @@ registerHandler("$%apiPath%$",
const std::shared_ptr<rb::Session> session, const std::shared_ptr<rb::Session> session,
const rb::Bytes& body ) const rb::Bytes& body )
{ {
RsGenericSerializer::SerializeContext cReq( INITIALIZE_API_CALL_JSON_CONTEXT;
nullptr, 0,
RsGenericSerializer::SERIALIZATION_FLAG_YIELDING );
RsJson& jReq(cReq.mJson);
jReq.Parse(reinterpret_cast<const char*>(body.data()), body.size());
RsGenericSerializer::SerializeContext cAns;
RsJson& jAns(cAns.mJson);
// if caller specified caller_data put it back in the answhere
const char kcd[] = "caller_data";
if(jReq.HasMember(kcd))
jAns.AddMember(kcd, jReq[kcd], jAns.GetAllocator());
if( !checkRsServicePtrReady( if( !checkRsServicePtrReady(
$%instanceName%$, "$%instanceName%$", cAns, session ) ) $%instanceName%$, "$%instanceName%$", cAns, session ) )

View file

@ -24,19 +24,7 @@ registerHandler("$%apiPath%$",
const std::shared_ptr<rb::Session> session, const std::shared_ptr<rb::Session> session,
const rb::Bytes& body ) const rb::Bytes& body )
{ {
RsGenericSerializer::SerializeContext cReq( INITIALIZE_API_CALL_JSON_CONTEXT;
nullptr, 0,
RsGenericSerializer::SERIALIZATION_FLAG_YIELDING );
RsJson& jReq(cReq.mJson);
jReq.Parse(reinterpret_cast<const char*>(body.data()), body.size());
RsGenericSerializer::SerializeContext cAns;
RsJson& jAns(cAns.mJson);
// if caller specified caller_data put it back in the answhere
const char kcd[] = "caller_data";
if(jReq.HasMember(kcd))
jAns.AddMember(kcd, jReq[kcd], jAns.GetAllocator());
if( !checkRsServicePtrReady( if( !checkRsServicePtrReady(
$%instanceName%$, "$%instanceName%$", cAns, session ) ) $%instanceName%$, "$%instanceName%$", cAns, session ) )
@ -54,15 +42,7 @@ $%functionCall%$
$%outputParamsSerialization%$ $%outputParamsSerialization%$
// return them to the API caller // return them to the API caller
std::stringstream ss; DEFAULT_API_CALL_JSON_RETURN(rb::OK);
ss << jAns;
std::string&& ans(ss.str());
const std::multimap<std::string, std::string> headers
{
{ "Content-Type", "text/json" },
{ "Content-Length", std::to_string(ans.length()) }
};
session->close(rb::OK, ans, headers);
} ); } );
}); });

View file

@ -26,10 +26,38 @@
#include "util/rsjson.h" #include "util/rsjson.h"
#include "retroshare/rsfiles.h" #include "retroshare/rsfiles.h"
#include "util/radix64.h" #include "util/radix64.h"
#include "retroshare/rsversion.h"
// Generated at compile time // Generated at compile time
#include "jsonapi-includes.inl" #include "jsonapi-includes.inl"
#define INITIALIZE_API_CALL_JSON_CONTEXT \
RsGenericSerializer::SerializeContext cReq( \
nullptr, 0, \
RsGenericSerializer::SERIALIZATION_FLAG_YIELDING ); \
RsJson& jReq(cReq.mJson); \
jReq.Parse(reinterpret_cast<const char*>(body.data()), body.size()); \
\
RsGenericSerializer::SerializeContext cAns; \
RsJson& jAns(cAns.mJson); \
\
/* if caller specified caller_data put it back in the answhere */ \
const char kcd[] = "caller_data"; \
if(jReq.HasMember(kcd)) \
jAns.AddMember(kcd, jReq[kcd], jAns.GetAllocator())
#define DEFAULT_API_CALL_JSON_RETURN(RET_CODE) \
std::stringstream ss; \
ss << jAns; \
std::string&& ans(ss.str()); \
const std::multimap<std::string, std::string> headers \
{ \
{ "Content-Type", "text/json" }, \
{ "Content-Length", std::to_string(ans.length()) } \
}; \
session->close(RET_CODE, ans, headers)
static bool checkRsServicePtrReady( static bool checkRsServicePtrReady(
void* serviceInstance, const std::string& serviceName, void* serviceInstance, const std::string& serviceName,
RsGenericSerializer::SerializeContext& ctx, RsGenericSerializer::SerializeContext& ctx,
@ -45,18 +73,12 @@ static bool checkRsServicePtrReady(
RsGenericSerializer::SerializeJob j(RsGenericSerializer::TO_JSON); RsGenericSerializer::SerializeJob j(RsGenericSerializer::TO_JSON);
RS_SERIAL_PROCESS(jsonApiError); RS_SERIAL_PROCESS(jsonApiError);
std::stringstream ss; RsJson& jAns(ctx.mJson);
ss << ctx.mJson; DEFAULT_API_CALL_JSON_RETURN(rb::CONFLICT);
std::string&& ans(ss.str());
const std::multimap<std::string, std::string> headers
{
{ "Content-Type", "text/json" },
{ "Content-Length", std::to_string(ans.length()) }
};
session->close(rb::CONFLICT, ans, headers);
return false; return false;
} }
JsonApiServer::JsonApiServer( JsonApiServer::JsonApiServer(
uint16_t port, const std::string& bindAddress, uint16_t port, const std::string& bindAddress,
const std::function<void(int)> shutdownCallback ) : const std::function<void(int)> shutdownCallback ) :
@ -69,6 +91,32 @@ JsonApiServer::JsonApiServer(
shutdown(); shutdown();
}); });
registerHandler("/jsonApiServer/version",
[](const std::shared_ptr<rb::Session> session)
{
size_t reqSize = session->get_request()->get_header("Content-Length", 0);
session->fetch( reqSize, [](
const std::shared_ptr<rb::Session> session,
const rb::Bytes& body )
{
INITIALIZE_API_CALL_JSON_CONTEXT;
uint32_t major = RS_MAJOR_VERSION;
uint32_t minor = RS_MINOR_VERSION;
uint32_t mini = RS_MINI_VERSION;
std::string human = RS_HUMAN_READABLE_VERSION;
RsGenericSerializer::SerializeContext& ctx(cAns);
RsGenericSerializer::SerializeJob j(RsGenericSerializer::TO_JSON);
RS_SERIAL_PROCESS(major);
RS_SERIAL_PROCESS(minor);
RS_SERIAL_PROCESS(mini);
RS_SERIAL_PROCESS(human);
DEFAULT_API_CALL_JSON_RETURN(rb::OK);
} );
});
registerHandler("/rsFiles/getFileData", registerHandler("/rsFiles/getFileData",
[](const std::shared_ptr<rb::Session> session) [](const std::shared_ptr<rb::Session> session)
{ {
@ -77,19 +125,7 @@ JsonApiServer::JsonApiServer(
const std::shared_ptr<rb::Session> session, const std::shared_ptr<rb::Session> session,
const rb::Bytes& body ) const rb::Bytes& body )
{ {
RsGenericSerializer::SerializeContext cReq( INITIALIZE_API_CALL_JSON_CONTEXT;
nullptr, 0,
RsGenericSerializer::SERIALIZATION_FLAG_YIELDING );
RsJson& jReq(cReq.mJson);
jReq.Parse(reinterpret_cast<const char*>(body.data()), body.size());
RsGenericSerializer::SerializeContext cAns;
RsJson& jAns(cAns.mJson);
// if caller specified caller_data put it back in the answhere
const char kcd[] = "caller_data";
if(jReq.HasMember(kcd))
jAns.AddMember(kcd, jReq[kcd], jAns.GetAllocator());
if(!checkRsServicePtrReady(rsFiles, "rsFiles", cAns, session)) if(!checkRsServicePtrReady(rsFiles, "rsFiles", cAns, session))
return; return;
@ -133,16 +169,7 @@ JsonApiServer::JsonApiServer(
if(!errorMessage.empty()) RS_SERIAL_PROCESS(errorMessage); if(!errorMessage.empty()) RS_SERIAL_PROCESS(errorMessage);
} }
// return them to the API caller DEFAULT_API_CALL_JSON_RETURN(rb::OK);
std::stringstream ss;
ss << jAns;
std::string&& ans(ss.str());
const std::multimap<std::string, std::string> headers
{
{ "Content-Type", "text/json" },
{ "Content-Length", std::to_string(ans.length()) }
};
session->close(rb::OK, ans, headers);
} ); } );
}); });