Adds function to add raw header to the request.

Function added to be able to add If-None-Match to save the ETag.

Signed-off-by: Camila San <hello@camila.codes>
This commit is contained in:
Camila San 2018-04-09 10:54:30 +02:00
parent 3b0b0fea33
commit d7286872f6
No known key found for this signature in database
GPG key ID: 7A4A6121E88E2AD4
4 changed files with 39 additions and 11 deletions

View file

@ -29,6 +29,7 @@ OcsJob::OcsJob(AccountPtr account)
{
_passStatusCodes.append(OCS_SUCCESS_STATUS_CODE);
_passStatusCodes.append(OCS_SUCCESS_STATUS_CODE_V2);
_passStatusCodes.append(OCS_NOT_MODIFIED_STATUS_CODE_V2);
setIgnoreCredentialFailure(true);
}
@ -52,6 +53,11 @@ void OcsJob::appendPath(const QString &id)
setPath(path() + QLatin1Char('/') + id);
}
void OcsJob::addRawHeader(const QByteArray &headerName, const QByteArray &value)
{
_request.setRawHeader(headerName, value);
}
static QUrlQuery percentEncodeQueryItems(
const QList<QPair<QString, QString>> &items)
{
@ -68,9 +74,8 @@ static QUrlQuery percentEncodeQueryItems(
void OcsJob::start()
{
QNetworkRequest req;
req.setRawHeader("Ocs-APIREQUEST", "true");
req.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
addRawHeader("Ocs-APIREQUEST", "true");
addRawHeader("Content-Type", "application/x-www-form-urlencoded");
QBuffer *buffer = new QBuffer;
@ -92,7 +97,7 @@ void OcsJob::start()
}
queryItems.addQueryItem(QLatin1String("format"), QLatin1String("json"));
QUrl url = Utility::concatUrlPath(account()->url(), path(), queryItems);
sendRequest(_verb, url, req, buffer);
sendRequest(_verb, url, _request, buffer);
AbstractNetworkJob::start();
}
@ -121,7 +126,11 @@ bool OcsJob::finished()
<< "has unexpected status code:" << statusCode << replyData;
emit ocsError(statusCode, message);
} else {
emit jobFinished(json);
// save new ETag value
if(reply()->rawHeaderList().contains("ETag"))
emit etagResponseHeaderReceived(reply()->rawHeader("ETag"), statusCode);
emit jobFinished(json, statusCode);
}
return true;
}

View file

@ -26,6 +26,8 @@
#define OCS_SUCCESS_STATUS_CODE 100
// Apparantly the v2.php URLs can return that
#define OCS_SUCCESS_STATUS_CODE_V2 200
// not modified when using ETag
#define OCS_NOT_MODIFIED_STATUS_CODE_V2 304
class QJsonDocument;
@ -99,6 +101,14 @@ public:
*/
static int getJsonReturnCode(const QJsonDocument &json, QString &message);
/**
* @brief Adds header to the request e.g. "If-None-Match"
* @param headerName a string with the header name
* @param value a string with the value
*/
void addRawHeader(const QByteArray &headerName, const QByteArray &value);
protected slots:
/**
@ -113,7 +123,7 @@ signals:
*
* @param reply the reply
*/
void jobFinished(QJsonDocument reply);
void jobFinished(QJsonDocument reply, int statusCode);
/**
* The status code was not one of the expected (passing)
@ -124,6 +134,14 @@ signals:
*/
void ocsError(int statusCode, const QString &message);
/**
* @brief etagResponseHeaderReceived - signal to report the ETag response header value
* from ocs api v2
* @param value - the ETag response header value
* @param statusCode - the OCS status code: 100 (!) for success
*/
void etagResponseHeaderReceived(const QByteArray &value, int statusCode);
private slots:
virtual bool finished() Q_DECL_OVERRIDE;
@ -131,6 +149,7 @@ private:
QByteArray _verb;
QList<QPair<QString, QString>> _params;
QVector<int> _passStatusCodes;
QNetworkRequest _request;
};
}

View file

@ -30,9 +30,8 @@ void OcsNavigationAppsJob::getNavigationApps()
start();
}
void OcsNavigationAppsJob::jobDone(const QJsonDocument &reply)
void OcsNavigationAppsJob::jobDone(const QJsonDocument &reply, int statusCode)
{
emit appsJobFinished(reply);
emit appsJobFinished(reply, statusCode);
}
}

View file

@ -43,11 +43,12 @@ signals:
* Result of the OCS request
*
* @param reply The reply
* @param statusCode the status code of the response
*/
void appsJobFinished(const QJsonDocument &reply);
void appsJobFinished(const QJsonDocument &reply, int statusCode);
private slots:
void jobDone(const QJsonDocument &reply);
void jobDone(const QJsonDocument &reply, int statusCode);
};
}