NuriaProject Framework
0.1
The NuriaProject Framework
|
The RestfulHttpNode class makes it easy to write RESTful APIs. More...
#include <restfulhttpnode.hpp>
Public Member Functions | |
RestfulHttpNode (void *object, MetaObject *metaObject, const QString &resourceName, HttpNode *parent=0) | |
RestfulHttpNode (const QString &resourceName=QString(), HttpNode *parent=0) | |
~RestfulHttpNode () | |
void | setRestfulHandler (HttpClient::HttpVerbs verbs, const QString &path, const QStringList &argumentNames, const Callback &callback, bool waitForRequestPostBody=true) |
void | setRestfulHandler (const QString &path, const QStringList &argumentNames, const Callback &callback, bool waitForRequestPostBody=true) |
![]() | |
HttpNode (const QString &resourceName, HttpNode *parent=0) | |
HttpNode (QObject *parent=0) | |
~HttpNode () | |
bool | addNode (HttpNode *node) |
SlotInfo | connectSlot (const QString &name, const Callback &callback) |
SlotInfo | connectSlot (const QString &name, QObject *receiver, const char *slot) |
bool | disconnectSlot (const QString &name) |
bool | isChild () const |
HttpNode * | parentNode () const |
const QString & | resourceName () const |
const QDir & | staticResourceDir () const |
StaticResourcesMode | staticResourceMode () const |
bool | setResourceName (const QString &name) |
void | setStaticResourceDir (QDir path) |
void | setStaticResourceMode (StaticResourcesMode mode) |
bool | hasNode (const QString &name) |
bool | hasSlot (const QString &name) |
HttpNode * | findNode (const QString &name) const |
Protected Member Functions | |
virtual void | conversionFailure (const QVariant &variant, HttpClient *client) |
virtual QVariant | convertArgumentToVariant (const QString &argumentData, int targetType) |
virtual QByteArray | generateResultData (const QVariant &result, HttpClient *client) |
bool | invokePath (const QString &path, const QStringList &parts, int index, HttpClient *client) override |
virtual QVariant | serializeVariant (const QVariant &variant) |
![]() | |
virtual bool | allowAccessToClient (const QString &path, const QStringList &parts, int index, HttpClient *client) |
virtual bool | callSlotByName (const QString &name, HttpClient *client) |
bool | sendStaticResource (const QString &name, HttpClient *client) |
bool | sendStaticResource (const QStringList &path, int indexInPath, HttpClient *client) |
Friends | |
class | RestfulHttpNodePrivate |
Additional Inherited Members | |
![]() | |
enum | StaticResourcesMode { NoStaticResources = 0, UseStaticResources = 1, UseNestedStaticResources = 2 } |
The RestfulHttpNode class makes it easy to write RESTful APIs.
This class was designed to be as flexible as possible when it comes to convenience from a remote API standpoint. You use this just like a HttpNode, with the exception that slots are allowed to return something.
If you don't want to sub-class RestfulHttpNode, you can use the constructor which takes a object pointer and a MetaObject instance.
Regardless which option you choose, you can then annotate methods you want to expose using NURIA_RESTFUL() (and optionally with NURIA_RESTFUL_VERBS()). When using this route all handlers (i.e. NURIA_RESTFUL methods) are registered automatically when the first invocation on the node happens.
If the method has a argument of type 'Nuria::HttpClient*', the HttpClient behind the request will be passed as value for this parameter.
To validate arguments prior calling, use the generic NURIA_REQUIRE() approach.
You can also completely alter this behaviour by reimplementing
It's also possible to return a custom type if Nuria::Serializer is able to serialize it to a QVariantMap.
You can change the behaviour of this by reimplementing convertVariantToData(). If you want to customize the behaviour when conversion fails, reimplement conversionFailure().
|
explicit |
Constructor. The node will operate on object of type metaObject. Ownership of object is not transferred. If you want to do this, you could e.g. connect to the destroyed() signal of this instance to deleteLater() of object, if object is a QObject. Another option would be connecting a lambda which destroys object to destroyed().
|
explicit |
Constructor.
Nuria::RestfulHttpNode::~RestfulHttpNode | ( | ) |
Destructor.
|
protectedvirtual |
Called if the conversion for variant failed. When this happens the reply is pretty much lost to the client. The default implementation loggs this as a error message and sends a 500 reply code to client.
|
protectedvirtual |
Takes argumentData and returns a QVariant of type targetType. Returns a invalid QVariant on failure. The behaviour of the default implementation is outlined in the class documentation above.
|
protectedvirtual |
Converts result to a QByteArray. A empty QByteArray is treated as error. Also responsible for setting response headers specific to the format, e.g. Content-Type for JSON.
|
overrideprotectedvirtual |
Does the invocation.
Reimplemented from Nuria::HttpNode.
|
protectedvirtual |
Takes variant and tries to convert it to a QVariant
only consisting out of POD- and some Qt types.
Allowed types:
When re-implementing this method call the default implementation if you don't handle this type:
If the result is invalid the conversion failed.
void Nuria::RestfulHttpNode::setRestfulHandler | ( | HttpClient::HttpVerbs | verbs, |
const QString & | path, | ||
const QStringList & | argumentNames, | ||
const Callback & | callback, | ||
bool | waitForRequestPostBody = true |
||
) |
Manually registers a RESTful handler which will be invoked when path is requested with a HTTP verb in verbs. In this case, arguments are read from the requested path as defined in path and are passed to callback in the order defined by argumentNames.
When dealing with POST/PUT bodies, the default is to wait for the whole body to be received and then invoke the handler. This can be overriden by passing false
for waitForRequestPostBody.
void Nuria::RestfulHttpNode::setRestfulHandler | ( | const QString & | path, |
const QStringList & | argumentNames, | ||
const Callback & | callback, | ||
bool | waitForRequestPostBody = true |
||
) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Does the same as the other setRestfulHandler(), but assumes a value of HttpClient::AllVerbs for verbs.