Now add parameters in a less crappy way

This commit is contained in:
Roeland Jago Douma 2015-10-15 21:58:16 +02:00
parent db1f4d4016
commit a34b663828
3 changed files with 48 additions and 65 deletions

View file

@ -32,19 +32,9 @@ void OcsJob::setVerb(const QByteArray &verb)
_verb = verb;
}
void OcsJob::setUrl(const QUrl &url)
void OcsJob::addParam(const QString &name, const QString &value)
{
_url = url;
}
void OcsJob::setGetParams(const QList<QPair<QString, QString> >& getParams)
{
_url.setQueryItems(getParams);
}
void OcsJob::setPostParams(const QList<QPair<QString, QString> >& postParams)
{
_postParams = postParams;
_params.append(qMakePair(name, value));
}
void OcsJob::addPassStatusCode(int code)
@ -58,24 +48,31 @@ void OcsJob::start()
req.setRawHeader("Ocs-APIREQUEST", "true");
req.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
// Url encode the _postParams and put them in a buffer.
QByteArray postData;
Q_FOREACH(auto tmp2, _postParams) {
if (! postData.isEmpty()) {
postData.append("&");
}
postData.append(QUrl::toPercentEncoding(tmp2.first));
postData.append("=");
postData.append(QUrl::toPercentEncoding(tmp2.second));
}
QUrl url = Account::concatUrlPath(account()->url(), path());
QBuffer *buffer = new QBuffer;
buffer->setData(postData);
auto queryItems = _url.queryItems();
if (_verb == "GET") {
url.setQueryItems(_params);
} else if (_verb == "POST" || _verb == "PUT") {
// Url encode the _postParams and put them in a buffer.
QByteArray postData;
Q_FOREACH(auto tmp, _params) {
if (!postData.isEmpty()) {
postData.append("&");
}
postData.append(QUrl::toPercentEncoding(tmp.first));
postData.append("=");
postData.append(QUrl::toPercentEncoding(tmp.second));
}
buffer->setData(postData);
}
//We want json data
auto queryItems = url.queryItems();
queryItems.append(qMakePair(QString::fromLatin1("format"), QString::fromLatin1("json")));
_url.setQueryItems(queryItems);
url.setQueryItems(queryItems);
setReply(davRequest(_verb, _url, req, buffer));
setReply(davRequest(_verb, url, req, buffer));
setupConnections(reply());
buffer->setParent(reply());
AbstractNetworkJob::start();
@ -88,14 +85,20 @@ bool OcsJob::finished()
bool success;
QVariantMap json = QtJson::parse(replyData, success).toMap();
if (!success) {
qDebug() << "Could not parse reply to" << _verb << _url << _postParams
qDebug() << "Could not parse reply to"
<< _verb
<< Account::concatUrlPath(account()->url(), path())
<< _params
<< ":" << replyData;
}
QString message;
const int statusCode = getJsonReturnCode(json, message);
if (!_passStatusCodes.contains(statusCode)) {
qDebug() << "Reply to" << _verb << _url << _postParams
qDebug() << "Reply to"
<< _verb
<< Account::concatUrlPath(account()->url(), path())
<< _params
<< "has unexpected status code:" << statusCode << replyData;
}

View file

@ -48,18 +48,13 @@ protected:
void setVerb(const QByteArray& verb);
/**
* The url of the OCS endpoint
* Add a new parameter to the request.
* Depending on the verb this is GET or POST parameter
*
* @param url
* @param name The name of the parameter
* @param value The value of the parameter
*/
void setUrl(const QUrl& url);
/**
* Set the get parameters to the url
*
* @param getParams list of pairs to add to the url
*/
void setGetParams(const QList<QPair<QString, QString> >& getParams);
void addParam(const QString& name, const QString &value);
/**
* Set the post parameters
@ -110,8 +105,7 @@ private slots:
private:
QByteArray _verb;
QUrl _url;
QList<QPair<QString, QString> > _postParams;
QList<QPair<QString, QString> > _params;
QVector<int> _passStatusCodes;
};

View file

@ -23,27 +23,23 @@ namespace OCC {
OcsShareJob::OcsShareJob(AccountPtr account, QObject* parent)
: OcsJob(account, parent)
{
setUrl(Account::concatUrlPath(account->url(), QString("ocs/v1.php/apps/files_sharing/api/v1/shares")));
setPath("ocs/v1.php/apps/files_sharing/api/v1/shares");
}
OcsShareJob::OcsShareJob(int shareId, AccountPtr account, QObject* parent)
: OcsJob(account, parent)
{
setUrl(Account::concatUrlPath(account->url(), QString("ocs/v1.php/apps/files_sharing/api/v1/shares/%1").arg(shareId)));
setPath(QString("ocs/v1.php/apps/files_sharing/api/v1/shares/%1").arg(shareId));
}
void OcsShareJob::getShares(const QString &path)
{
setVerb("GET");
QList<QPair<QString, QString> > getParams;
getParams.append(qMakePair(QString::fromLatin1("path"), path));
setGetParams(getParams);
addParam(QString::fromLatin1("path"), path);
addPassStatusCode(404);
start();
}
void OcsShareJob::deleteShare()
@ -57,15 +53,12 @@ void OcsShareJob::setExpireDate(const QDate &date)
{
setVerb("PUT");
QList<QPair<QString, QString> > postParams;
if (date.isValid()) {
postParams.append(qMakePair(QString::fromLatin1("expireDate"), date.toString("yyyy-MM-dd")));
addParam(QString::fromLatin1("expireDate"), date.toString("yyyy-MM-dd"));
} else {
postParams.append(qMakePair(QString::fromLatin1("expireDate"), QString()));
addParam(QString::fromLatin1("expireDate"), QString());
}
setPostParams(postParams);
start();
}
@ -73,10 +66,8 @@ void OcsShareJob::setPassword(const QString &password)
{
setVerb("PUT");
QList<QPair<QString, QString> > postParams;
postParams.append(qMakePair(QString::fromLatin1("password"), password));
addParam(QString::fromLatin1("password"), password);
setPostParams(postParams);
start();
}
@ -84,11 +75,9 @@ void OcsShareJob::setPublicUpload(bool publicUpload)
{
setVerb("PUT");
QList<QPair<QString, QString> > postParams;
const QString value = QString::fromLatin1(publicUpload ? "true" : "false");
postParams.append(qMakePair(QString::fromLatin1("publicUpload"), value));
addParam(QString::fromLatin1("publicUpload"), value);
setPostParams(postParams);
start();
}
@ -96,20 +85,17 @@ void OcsShareJob::createShare(const QString &path, SHARETYPE shareType, const QS
{
setVerb("POST");
QList<QPair<QString, QString> > postParams;
postParams.append(qMakePair(QString::fromLatin1("path"), path));
postParams.append(qMakePair(QString::fromLatin1("shareType"), QString::number(static_cast<int>(shareType))));
addParam(QString::fromLatin1("path"), path);
addParam(QString::fromLatin1("shareType"), QString::number(static_cast<int>(shareType)));
if (!password.isEmpty()) {
postParams.append(qMakePair(QString::fromLatin1("shareType"), password));
addParam(QString::fromLatin1("shareType"), password);
}
if (date.isValid()) {
postParams.append(qMakePair(QString::fromLatin1("expireDate"), date.toString("yyyy-MM-dd")));
addParam(QString::fromLatin1("expireDate"), date.toString("yyyy-MM-dd"));
}
setPostParams(postParams);
addPassStatusCode(403);
start();