mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-02 06:06:10 -04:00
Implement automatic JSON API generation
qmake file add jsonapi-generator target to compile JSON API generator qmake files add rs_jsonapi CONFIG option to enable/disable JSON API at compile time RsTypeSerializer pass down same serialization flags when creating new context for nested objects serial job RsGxsChannels expose a few methods through JSON API as example Derive a few GXS types (RsGxsChannelGroup, RsGxsChannelPost, RsGxsFile, RsMsgMetaData) from RsSerializables so they can be used for the JSON API Create RsGenericSerializer::SERIALIZATION_FLAG_YIELDING so JSON objects that miss some fields can be still deserialized, this improve API usability SerializeContext offer friendly constructor with default paramethers Add restbed 4.6 library as git submodule as most systems doesn't have it yet Add a bit of documentation about JSON API into jsonapi-generator/README.adoc Add JsonApiServer class to expose the JSON API via HTTP protocol
This commit is contained in:
parent
2f159efb10
commit
7ad337c8d2
22 changed files with 1205 additions and 83 deletions
45
libretroshare/src/jsonapi/jsonapi.cpp
Normal file
45
libretroshare/src/jsonapi/jsonapi.cpp
Normal file
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* RetroShare JSON API
|
||||
* Copyright (C) 2018 Gioacchino Mazzurco <gio@eigenlab.org>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "jsonapi.h"
|
||||
|
||||
// Generated at compile time
|
||||
#include "jsonapi-wrappers.h"
|
||||
|
||||
JsonApiServer::JsonApiServer(uint16_t port) : mPort(port)
|
||||
{
|
||||
// Generated at compile time
|
||||
#include "jsonapi-register.inl"
|
||||
}
|
||||
|
||||
void JsonApiServer::run()
|
||||
{
|
||||
std::shared_ptr<rb::Settings> settings(new rb::Settings);
|
||||
settings->set_port(mPort);
|
||||
settings->set_default_header("Connection", "close");
|
||||
service.start(settings);
|
||||
}
|
||||
|
||||
void JsonApiServer::registerHandler(const std::string& path, const std::function<void (const std::shared_ptr<restbed::Session>)>& handler)
|
||||
{
|
||||
std::shared_ptr<restbed::Resource> resource(new rb::Resource);
|
||||
resource->set_path(path);
|
||||
resource->set_method_handler("GET", handler);
|
||||
resource->set_method_handler("POST", handler);
|
||||
service.publish(resource);
|
||||
}
|
62
libretroshare/src/jsonapi/jsonapi.h
Normal file
62
libretroshare/src/jsonapi/jsonapi.h
Normal file
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* RetroShare JSON API
|
||||
* Copyright (C) 2018 Gioacchino Mazzurco <gio@eigenlab.org>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <restbed>
|
||||
#include <rapid_json/document.h>
|
||||
|
||||
#include "retroshare/rsgxschannels.h"
|
||||
#include "serialiser/rstypeserializer.h"
|
||||
#include "util/rsthreads.h"
|
||||
|
||||
namespace rb = restbed;
|
||||
|
||||
void apiVersionHandler(const std::shared_ptr<rb::Session> session);
|
||||
void createChannelHandler(const std::shared_ptr<rb::Session> session);
|
||||
|
||||
/**
|
||||
* Simple usage
|
||||
* \code{.cpp}
|
||||
* JsonApiServer jas(9092);
|
||||
* jas.start("JsonApiServer");
|
||||
* \endcode
|
||||
*/
|
||||
struct JsonApiServer : RsSingleJobThread
|
||||
{
|
||||
JsonApiServer(uint16_t port);
|
||||
|
||||
/// @see RsSingleJobThread
|
||||
virtual void run();
|
||||
|
||||
/**
|
||||
* @param[in] handler function which will be called to handle the requested
|
||||
* path, the function must be declared like:
|
||||
* \code{.cpp}
|
||||
* void functionName(const shared_ptr<restbed::Session> session)
|
||||
* \endcode
|
||||
*/
|
||||
void registerHandler(
|
||||
const std::string& path,
|
||||
const std::function<void(const std::shared_ptr<rb::Session>)>& handler );
|
||||
|
||||
private:
|
||||
uint16_t mPort;
|
||||
rb::Service service;
|
||||
};
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue