mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-10-01 02:35:48 -04:00
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:
parent
92f90178c4
commit
5245765964
@ -31,19 +31,7 @@ registerHandler("$%apiPath%$",
|
||||
const std::shared_ptr<rb::Session> session,
|
||||
const rb::Bytes& body )
|
||||
{
|
||||
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());
|
||||
INITIALIZE_API_CALL_JSON_CONTEXT;
|
||||
|
||||
if( !checkRsServicePtrReady(
|
||||
$%instanceName%$, "$%instanceName%$", cAns, session ) )
|
||||
|
@ -24,19 +24,7 @@ registerHandler("$%apiPath%$",
|
||||
const std::shared_ptr<rb::Session> session,
|
||||
const rb::Bytes& body )
|
||||
{
|
||||
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());
|
||||
INITIALIZE_API_CALL_JSON_CONTEXT;
|
||||
|
||||
if( !checkRsServicePtrReady(
|
||||
$%instanceName%$, "$%instanceName%$", cAns, session ) )
|
||||
@ -54,15 +42,7 @@ $%functionCall%$
|
||||
$%outputParamsSerialization%$
|
||||
|
||||
// return them to the API caller
|
||||
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);
|
||||
DEFAULT_API_CALL_JSON_RETURN(rb::OK);
|
||||
} );
|
||||
});
|
||||
|
||||
|
@ -26,10 +26,38 @@
|
||||
#include "util/rsjson.h"
|
||||
#include "retroshare/rsfiles.h"
|
||||
#include "util/radix64.h"
|
||||
#include "retroshare/rsversion.h"
|
||||
|
||||
// Generated at compile time
|
||||
#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(
|
||||
void* serviceInstance, const std::string& serviceName,
|
||||
RsGenericSerializer::SerializeContext& ctx,
|
||||
@ -45,18 +73,12 @@ static bool checkRsServicePtrReady(
|
||||
RsGenericSerializer::SerializeJob j(RsGenericSerializer::TO_JSON);
|
||||
RS_SERIAL_PROCESS(jsonApiError);
|
||||
|
||||
std::stringstream ss;
|
||||
ss << ctx.mJson;
|
||||
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);
|
||||
RsJson& jAns(ctx.mJson);
|
||||
DEFAULT_API_CALL_JSON_RETURN(rb::CONFLICT);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
JsonApiServer::JsonApiServer(
|
||||
uint16_t port, const std::string& bindAddress,
|
||||
const std::function<void(int)> shutdownCallback ) :
|
||||
@ -69,6 +91,32 @@ JsonApiServer::JsonApiServer(
|
||||
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",
|
||||
[](const std::shared_ptr<rb::Session> session)
|
||||
{
|
||||
@ -77,19 +125,7 @@ JsonApiServer::JsonApiServer(
|
||||
const std::shared_ptr<rb::Session> session,
|
||||
const rb::Bytes& body )
|
||||
{
|
||||
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());
|
||||
INITIALIZE_API_CALL_JSON_CONTEXT;
|
||||
|
||||
if(!checkRsServicePtrReady(rsFiles, "rsFiles", cAns, session))
|
||||
return;
|
||||
@ -133,16 +169,7 @@ JsonApiServer::JsonApiServer(
|
||||
if(!errorMessage.empty()) RS_SERIAL_PROCESS(errorMessage);
|
||||
}
|
||||
|
||||
// return them to the API caller
|
||||
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);
|
||||
DEFAULT_API_CALL_JSON_RETURN(rb::OK);
|
||||
} );
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user