• RestResource
  • RestResource Class

    (nymeaserver::RestResource)

    This class provides an interface for REST API resources. More...

    Public Functions

    RestResource(QObject * parent = 0)
    virtual ~RestResource() = 0
    virtual QString name() const = 0
    virtual HttpReply * proccessRequest(const HttpRequest & request, const QStringList & urlTokens) = 0

    Static Public Members

    HttpReply * createAsyncReply()
    HttpReply * createCorsSuccessReply()
    HttpReply * createDeviceErrorReply(const HttpReply::HttpStatusCode & statusCode, const DeviceManager::DeviceError & deviceError)
    HttpReply * createErrorReply(const HttpReply::HttpStatusCode & statusCode)
    HttpReply * createLoggingErrorReply(const HttpReply::HttpStatusCode & statusCode, const Logging::LoggingError & loggingError)
    HttpReply * createRuleErrorReply(const HttpReply::HttpStatusCode & statusCode, const RuleEngine::RuleError & ruleError)
    HttpReply * createSuccessReply()
    QPair<bool, QVariant> verifyPayload(const QByteArray & payload)

    Detailed Description

    This class provides an interface for REST API resources.

    The RestResource class provides an interface for subclassing a new resource for the REST API. A resource represents an Object in the core like Devices or Plugins. A RestResource will be identified by it's name.

    Each subclass of RestResource has to implement the method RestResource::proccessRequest() where a valid HttpRequest will be processed.

    This name of the resource will define the endpoint of the an API call:

    http://localhost:3333/api/v1/<resourceName>

    See also RestServer.

    Member Function Documentation

    RestResource::RestResource(QObject * parent = 0)

    Constructs a RestResource with the given parent.

    [pure virtual] RestResource::~RestResource()

    Pure virtual destructor for this RestResource.

    [static] HttpReply * RestResource::createAsyncReply()

    Returns the pointer to a new created HttpReply initialized with HttpReply::Ok and HttpReply::TypeAsync.

    [static] HttpReply * RestResource::createCorsSuccessReply()

    Returns the pointer to a new created HttpReply which represents a response to a CORS request.

    [static] HttpReply * RestResource::createDeviceErrorReply(const HttpReply::HttpStatusCode & statusCode, const DeviceManager::DeviceError & deviceError)

    Returns the pointer to a new created error HttpReply initialized with the given statusCode, HttpReply::TypeSync and the deviceError.

    [static] HttpReply * RestResource::createErrorReply(const HttpReply::HttpStatusCode & statusCode)

    Returns the pointer to a new created error HttpReply initialized with the given statusCode and HttpReply::TypeSync.

    [static] HttpReply * RestResource::createLoggingErrorReply(const HttpReply::HttpStatusCode & statusCode, const Logging::LoggingError & loggingError)

    Returns the pointer to a new created HttpReply with the given statusCode and loggingError. This method will be used to create an error response for the LogsResource. The loggingError will be written in the payload of this reply.

    [static] HttpReply * RestResource::createRuleErrorReply(const HttpReply::HttpStatusCode & statusCode, const RuleEngine::RuleError & ruleError)

    Returns the pointer to a new created HttpReply with the given statusCode and ruleError. This method will be used to create an error response for the RulesResource. The ruleError will be written in the payload of this reply.

    [static] HttpReply * RestResource::createSuccessReply()

    Returns the pointer to a new created HttpReply initialized with HttpReply::Ok and HttpReply::TypeSync.

    [pure virtual] QString RestResource::name() const

    This method must be implemented in a subclass of RestResource. It returns the endpoint name of this RestResource i.e. if this method returns "example" the API resource will be accessable with the URL:

    http://localhost:3333/api/v1/example

    [pure virtual] HttpReply * RestResource::proccessRequest(const HttpRequest & request, const QStringList & urlTokens)

    This method will be called from the RestServer once a HttpRequest request was identified to belong to this RestResource. The given urlTokens contain the full list of URL tokens from this request.

    [static] QPair<bool, QVariant> RestResource::verifyPayload(const QByteArray & payload)

    This method can be used from every RestResource in order to verify if the payload of a HttpRequest is a valid JSON document. Returns true and the valid QVariant if there was no error while parsing JSON. Returns false and an invalid QVariant if the payload could not be parsed.