You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
1.9 KiB
92 lines
1.9 KiB
#include "XHttpManager.h"
|
|
#include "HttpClient.h"
|
|
#include "XHttpClientCallback.h"
|
|
#include "XGlobal.h"
|
|
|
|
XHttpManager XHttpManager::instance;
|
|
|
|
XHttpManager::XHttpManager() {
|
|
|
|
}
|
|
|
|
XHttpManager::~XHttpManager() {
|
|
|
|
}
|
|
|
|
XHttpManager& XHttpManager::getInstance() {
|
|
return instance;
|
|
}
|
|
|
|
bool XHttpManager::getByHttp(const char* id, const char* baseUrl, const char* token, const char* params) {
|
|
if (nullptr == baseUrl)
|
|
return false;
|
|
|
|
QMap<QString, QString> paramValues;
|
|
|
|
XGlobal::parseCustomFormat(params, paramValues);
|
|
|
|
HttpClient* httpClient = getIdle();
|
|
if (nullptr == httpClient)
|
|
return false;
|
|
|
|
XHttpClientCallback* callback = (XHttpClientCallback*)httpClient->userData(0);
|
|
assert(nullptr != callback);
|
|
|
|
callback->setInfo(id, "get");
|
|
|
|
httpClient->get(QUrl(baseUrl), token, paramValues);
|
|
|
|
return true;
|
|
}
|
|
|
|
bool XHttpManager::postByHttp(const char* id, const char* baseUrl, const char* token, const char* jsonData) {
|
|
if (nullptr == baseUrl)
|
|
return false;
|
|
|
|
QJsonObject json;
|
|
|
|
if (nullptr != jsonData) {
|
|
QString jsonString = QString::fromUtf8(jsonData);
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8());
|
|
if (!jsonDoc.isNull() && jsonDoc.isObject())
|
|
json = jsonDoc.object();
|
|
}
|
|
|
|
HttpClient* httpClient = getIdle();
|
|
if (nullptr == httpClient)
|
|
return false;
|
|
|
|
XHttpClientCallback* callback = (XHttpClientCallback*)httpClient->userData(0);
|
|
assert(nullptr != callback);
|
|
|
|
callback->setInfo(id, "post");
|
|
|
|
httpClient->post(QUrl(baseUrl), json, token);
|
|
|
|
return true;
|
|
}
|
|
|
|
HttpClient* XHttpManager::getIdle() {
|
|
for (auto client : clients) {
|
|
if (client->isFinished())
|
|
return client;
|
|
}
|
|
|
|
HttpClient* httpClient = new HttpClient(this);
|
|
assert(nullptr != httpClient);
|
|
|
|
XHttpClientCallback* callback = new XHttpClientCallback(httpClient);
|
|
assert(nullptr != callback);
|
|
|
|
httpClient->setUserData(0, callback);
|
|
|
|
clients.append(httpClient);
|
|
|
|
return httpClient;
|
|
}
|
|
|
|
void XHttpManager::release() {
|
|
|
|
}
|