123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- #ifndef QHTTPCLIENT_HPP
- #define QHTTPCLIENT_HPP
- #include "qhttpfwd.hpp"
- #include <QTcpSocket>
- #include <QUrl>
- namespace qhttp {
- namespace client {
- typedef std::function<void (QHttpRequest*)> TRequstHandler;
- typedef std::function<void (QHttpResponse*)> TResponseHandler;
- class QHTTP_API QHttpClient : public QObject
- {
- Q_OBJECT
- Q_PROPERTY(quint32 timeOut READ timeOut WRITE setTimeOut)
- public:
- explicit QHttpClient(QObject *parent = nullptr);
- virtual ~QHttpClient();
-
- bool request(THttpMethod method, QUrl url,
- const TRequstHandler& reqHandler,
- const TResponseHandler& resHandler);
-
- inline bool request(THttpMethod method, QUrl url, const TResponseHandler& resHandler) {
- return request(method, url, nullptr, resHandler);
- }
-
- inline bool request(THttpMethod method, QUrl url) {
- return request(method, url, nullptr, nullptr);
- }
-
- bool isOpen() const;
-
- void killConnection();
-
- quint32 timeOut()const;
-
- void setTimeOut(quint32);
-
- void setConnectingTimeOut(quint32);
- template<class Handler>
- void setConnectingTimeOut(quint32 timeout, Handler&& func) {
- setConnectingTimeOut(timeout);
- QObject::connect(this, &QHttpClient::connectingTimeOut,
- std::forward<Handler&&>(func)
- );
- }
-
- TBackend backendType() const;
-
- QTcpSocket* tcpSocket() const;
-
- QLocalSocket* localSocket() const;
- signals:
-
- void httpConnected(QHttpRequest* req);
-
- void newResponse(QHttpResponse* res);
-
- void disconnected();
-
- void connectingTimeOut();
- protected:
-
- virtual void onRequestReady(QHttpRequest* req);
-
- virtual void onResponseReady(QHttpResponse* res);
- protected:
- explicit QHttpClient(QHttpClientPrivate&, QObject*);
- void timerEvent(QTimerEvent*) override;
- Q_DECLARE_PRIVATE(QHttpClient)
- Q_DISABLE_COPY(QHttpClient)
- QScopedPointer<QHttpClientPrivate> d_ptr;
- };
- }
- }
- #endif
|