#ifndef HTTPREQUEST_H
#define HTTPREQUEST_H

#include <QObject>
#include <QMap>
#include <QJsonObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QHttpMultiPart>

class HttpRequest : public QObject
{
    Q_OBJECT

public:
    explicit HttpRequest(QObject *parent = nullptr);

    inline void setUserToken(const QString & aid, const QString & token)
    {
        _aid = aid;
        _token = token;
    }

    inline QString getUser()
    {
        return _aid;
    }
    inline QString getUserToken()
    {
        return _token;
    }


    inline QNetworkAccessManager * manger() {return &_manager;}

    QNetworkRequest request(const QString & url);
signals:
    void result(int code, const QJsonObject & body);

public slots:
    void get(const QString & url);
    void post(const QString & url, const QMap<QString,QString>  & body);
    void post(const QString & url, const QJsonObject & body);

private slots:
    void finishRequest(QNetworkReply*);

private:
    void initRequest(QNetworkRequest & req, bool post = false);
    void initMultiPart(QHttpMultiPart & part,const QMap<QString,QString> body);
//    void initMultiPart(QHttpMultiPart & part,const QMap<QString,QVariant> body);


private:
    QString _aid;
    QString _token;
    QNetworkAccessManager _manager;

};

#endif // HTTPREQUEST_H
