/* * Copyright (c) 2023 Unionman Technology Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef JQLIBRARY_INCLUDE_JQNET_H #define JQLIBRARY_INCLUDE_JQNET_H #ifndef QT_NETWORK_LIB #error("Please add network in pro file") #endif // C++ lib import #include // Qt lib import #include #include #include #include #include #include namespace JQNet { QNetworkAddressEntry getNetworkAddressEntry(); QPair getNetworkAddressEntryWithNetworkInterface(const bool& ridVm = true); QString getHostName(); #ifdef JQFOUNDATION_LIB bool pingIp(const QHostAddress& hostAddress); #endif class HTTP { Q_DISABLE_COPY(HTTP) public: HTTP() = default; ~HTTP() = default; public: inline QNetworkAccessManager& manage() { return manage_; } bool get(const QNetworkRequest& request, QByteArray& target, const int& timeout = 30 * 1000); void get(const QNetworkRequest& request, const std::function& onFinished, const std::function& onError, const int& timeout = 30 * 1000); bool deleteResource(const QNetworkRequest& request, QByteArray& target, const int& timeout = 30 * 1000); void deleteResource(const QNetworkRequest& request, const std::function& onFinished, const std::function& onError, const int& timeout = 30 * 1000); bool post( const QNetworkRequest& request, const QByteArray& body, QByteArray& target, const int& timeout = 30 * 1000); bool post(const QNetworkRequest& request, const QSharedPointer& multiPart, QByteArray& target, const int& timeout = 30 * 1000); void post(const QNetworkRequest& request, const QByteArray& body, const std::function& onFinished, const std::function& onError, const int& timeout = 30 * 1000); bool put( const QNetworkRequest& request, const QByteArray& body, QByteArray& target, const int& timeout = 30 * 1000); bool put(const QNetworkRequest& request, const QSharedPointer& multiPart, QByteArray& target, const int& timeout = 30 * 1000); void put(const QNetworkRequest& request, const QByteArray& body, const std::function& onFinished, const std::function& onError, const int& timeout = 30 * 1000); #if !(defined Q_OS_LINUX) && (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)) bool patch( const QNetworkRequest& request, const QByteArray& body, QByteArray& target, const int& timeout = 30 * 1000); void patch(const QNetworkRequest& request, const QByteArray& body, const std::function& onFinished, const std::function& onError, const int& timeout = 30 * 1000); #endif static QPair get(const QString& url, const int& timeout = 30 * 1000); static QPair get(const QNetworkRequest& request, const int& timeout = 30 * 1000); static QPair deleteResource(const QString& url, const int& timeout = 30 * 1000); static QPair deleteResource(const QNetworkRequest& request, const int& timeout = 30 * 1000); static QPair post(const QString& url, const QByteArray& body, const int& timeout = 30 * 1000); static QPair post( const QNetworkRequest& request, const QByteArray& body, const int& timeout = 30 * 1000); static QPair post(const QNetworkRequest& request, const QSharedPointer& multiPart, const int& timeout = 30 * 1000); static QPair put(const QString& url, const QByteArray& body, const int& timeout = 30 * 1000); static QPair put( const QNetworkRequest& request, const QByteArray& body, const int& timeout = 30 * 1000); static QPair put(const QNetworkRequest& request, const QSharedPointer& multiPart, const int& timeout = 30 * 1000); #if !(defined Q_OS_LINUX) && (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)) static QPair patch( const QString& url, const QByteArray& body, const int& timeout = 30 * 1000); static QPair patch( const QNetworkRequest& request, const QByteArray& body, const int& timeout = 30 * 1000); #endif private: void handle(QNetworkReply* reply, const int& timeout, const std::function& onFinished, const std::function& onError, const std::function& onTimeout); private: QNetworkAccessManager manage_; }; } #endif // JQLIBRARY_INCLUDE_JQNET_H_