/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                         *
 *  Copyright (C) 2015 Simon Stürz <simon.stuerz@guh.io>                   *
 *                                                                         *
 *  This file is part of nymea.                                            *
 *                                                                         *
 *  nymea is free software: you can redistribute it and/or modify          *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation, version 2 of the License.                *
 *                                                                         *
 *  nymea 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 General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with nymea. If not, see <http://www.gnu.org/licenses/>.          *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#ifndef RESTRESOURCE_H
#define RESTRESOURCE_H

#include <QObject>
#include <QPair>

#include "servers/httpreply.h"
#include "servers/httprequest.h"
#include "jsonrpc/jsontypes.h"

class QVariant;

namespace nymeaserver {

class RestResource : public QObject
{
    Q_OBJECT
public:
    explicit RestResource(QObject *parent = 0);
    virtual ~RestResource() = 0;

    virtual QString name() const = 0;

    virtual HttpReply *proccessRequest(const HttpRequest &request, const QStringList &urlTokens) = 0;

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

private:
    virtual HttpReply *proccessGetRequest(const HttpRequest &request, const QStringList &urlTokens);
    virtual HttpReply *proccessDeleteRequest(const HttpRequest &request, const QStringList &urlTokens);
    virtual HttpReply *proccessPutRequest(const HttpRequest &request, const QStringList &urlTokens);
    virtual HttpReply *proccessPostRequest(const HttpRequest &request, const QStringList &urlTokens);
    virtual HttpReply *proccessOptionsRequest(const HttpRequest &request, const QStringList &urlTokens);

    QHash<QPair<HttpRequest::RequestMethod, QString>, QString> m_descriptions;
    QHash<QString, QVariantMap> m_params;
    QHash<QString, QVariantMap> m_returns;

};

}

#endif // RESTRESOURCE_H
