mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-22 14:55:36 +08:00
Ref T730, obtain duration from "swift modified" QNetworkReply
This commit is contained in:
committed by
Mat Sutcliffe
parent
ff6b7f8deb
commit
ea8e27cc9b
@@ -314,14 +314,14 @@ namespace BlackMisc
|
||||
return req;
|
||||
}
|
||||
|
||||
qint64 CNetworkUtils::lastModifiedMsSinceEpoch(QNetworkReply *nwReply)
|
||||
qint64 CNetworkUtils::lastModifiedMsSinceEpoch(const QNetworkReply *nwReply)
|
||||
{
|
||||
Q_ASSERT(nwReply);
|
||||
const QDateTime lm = CNetworkUtils::lastModifiedDateTime(nwReply);
|
||||
return lm.isValid() ? lm.toMSecsSinceEpoch() : -1;
|
||||
}
|
||||
|
||||
QDateTime CNetworkUtils::lastModifiedDateTime(QNetworkReply *nwReply)
|
||||
QDateTime CNetworkUtils::lastModifiedDateTime(const QNetworkReply *nwReply)
|
||||
{
|
||||
Q_ASSERT(nwReply);
|
||||
const QVariant lastModifiedQv = nwReply->header(QNetworkRequest::LastModifiedHeader);
|
||||
@@ -332,12 +332,25 @@ namespace BlackMisc
|
||||
return QDateTime();
|
||||
}
|
||||
|
||||
qint64 CNetworkUtils::lastModifiedSinceNow(QNetworkReply *nwReply)
|
||||
qint64 CNetworkUtils::lastModifiedSinceNow(const QNetworkReply *nwReply)
|
||||
{
|
||||
const qint64 sinceEpoch = CNetworkUtils::lastModifiedMsSinceEpoch(nwReply);
|
||||
return sinceEpoch > 0 ? std::max(0LL, QDateTime::currentMSecsSinceEpoch() - sinceEpoch) : QDateTime::currentMSecsSinceEpoch();
|
||||
}
|
||||
|
||||
qint64 CNetworkUtils::requestDuration(const QNetworkReply *nwReply)
|
||||
{
|
||||
if (!nwReply) { return -1; }
|
||||
const QVariant started = nwReply->property("started");
|
||||
if (started.isValid() && started.canConvert<qint64>())
|
||||
{
|
||||
const qint64 now = QDateTime::currentMSecsSinceEpoch();
|
||||
const qint64 start = started.value<qint64>();
|
||||
return (now - start);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int CNetworkUtils::getHttpStatusCode(QNetworkReply *nwReply)
|
||||
{
|
||||
if (!nwReply) { return -1; }
|
||||
@@ -524,11 +537,36 @@ namespace BlackMisc
|
||||
QString CNetworkUtils::networkStatesToString(QNetworkConfiguration::StateFlags states)
|
||||
{
|
||||
QStringList statesSl;
|
||||
if (states.testFlag(QNetworkConfiguration::Active)) { statesSl << networkStateToString(QNetworkConfiguration::Active); }
|
||||
if (states.testFlag(QNetworkConfiguration::Active)) { statesSl << networkStateToString(QNetworkConfiguration::Active); }
|
||||
if (states.testFlag(QNetworkConfiguration::Discovered)) { statesSl << networkStateToString(QNetworkConfiguration::Discovered); }
|
||||
if (states.testFlag(QNetworkConfiguration::Defined)) { statesSl << networkStateToString(QNetworkConfiguration::Defined); }
|
||||
if (states.testFlag(QNetworkConfiguration::Undefined)) { statesSl << networkStateToString(QNetworkConfiguration::Undefined); }
|
||||
if (states.testFlag(QNetworkConfiguration::Defined)) { statesSl << networkStateToString(QNetworkConfiguration::Defined); }
|
||||
if (states.testFlag(QNetworkConfiguration::Undefined)) { statesSl << networkStateToString(QNetworkConfiguration::Undefined); }
|
||||
return statesSl.join(", ");
|
||||
}
|
||||
|
||||
const QString &CNetworkUtils::networkOperationToString(QNetworkAccessManager::Operation operation)
|
||||
{
|
||||
static const QString h("HEAD");
|
||||
static const QString g("GET");
|
||||
static const QString put("PUT");
|
||||
static const QString d("POST");
|
||||
static const QString post("DELETE");
|
||||
static const QString c("custom");
|
||||
static const QString u("unknown");
|
||||
|
||||
switch (operation)
|
||||
{
|
||||
case QNetworkAccessManager::HeadOperation: return h;
|
||||
case QNetworkAccessManager::GetOperation: return g;
|
||||
case QNetworkAccessManager::PutOperation: return put;
|
||||
case QNetworkAccessManager::PostOperation: return post;
|
||||
case QNetworkAccessManager::DeleteOperation: return d;
|
||||
case QNetworkAccessManager::CustomOperation: return c;
|
||||
case QNetworkAccessManager::UnknownOperation:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return u;
|
||||
}
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
@@ -125,13 +125,16 @@ namespace BlackMisc
|
||||
static QNetworkRequest getSwiftNetworkRequest(const QNetworkRequest &request, const QString &userAgentDetails = {});
|
||||
|
||||
//! Last modified from reply
|
||||
static qint64 lastModifiedMsSinceEpoch(QNetworkReply *nwReply);
|
||||
static qint64 lastModifiedMsSinceEpoch(const QNetworkReply *nwReply);
|
||||
|
||||
//! Last modified from reply
|
||||
static QDateTime lastModifiedDateTime(QNetworkReply *nwReply);
|
||||
static QDateTime lastModifiedDateTime(const QNetworkReply *nwReply);
|
||||
|
||||
//! Last modified from reply compared with now (in ms)
|
||||
static qint64 lastModifiedSinceNow(QNetworkReply *nwReply);
|
||||
static qint64 lastModifiedSinceNow(const QNetworkReply *nwReply);
|
||||
|
||||
//! Request duration (only works if requested by swift functions)
|
||||
static qint64 requestDuration(const QNetworkReply *nwReply);
|
||||
|
||||
//! Get the http status code
|
||||
static int getHttpStatusCode(QNetworkReply *nwReply);
|
||||
@@ -178,6 +181,9 @@ namespace BlackMisc
|
||||
//! States to string
|
||||
static QString networkStatesToString(QNetworkConfiguration::StateFlags states);
|
||||
|
||||
//! Operation to string
|
||||
static const QString &networkOperationToString(QNetworkAccessManager::Operation operation);
|
||||
|
||||
private:
|
||||
//! Hidden constructor
|
||||
CNetworkUtils() {}
|
||||
|
||||
Reference in New Issue
Block a user