NetworkJobs: JSON network job now reports OCS reply code.

The signal jsonReceived() now not only delivers the raw json string, but
also the status code that came as OCS reply.

Also, fixed a typo in the signals name (recieved => received).
This commit is contained in:
Klaas Freitag 2015-11-19 15:59:10 +01:00
parent d6aa667971
commit 421c6a92f3
7 changed files with 43 additions and 13 deletions

View file

@ -142,7 +142,8 @@ void ActivityListModel::startFetchJob(AccountState* s)
return;
}
JsonApiJob *job = new JsonApiJob(s->account(), QLatin1String("ocs/v1.php/cloud/activity"), this);
QObject::connect(job, SIGNAL(jsonRecieved(QVariantMap)), this, SLOT(slotActivitiesReceived(QVariantMap)));
QObject::connect(job, SIGNAL(jsonReceived(QVariantMap, int)),
this, SLOT(slotActivitiesReceived(QVariantMap, int)));
job->setProperty("AccountStatePtr", QVariant::fromValue<AccountState*>(s));
QList< QPair<QString,QString> > params;
@ -155,7 +156,7 @@ void ActivityListModel::startFetchJob(AccountState* s)
job->start();
}
void ActivityListModel::slotActivitiesReceived(const QVariantMap& json)
void ActivityListModel::slotActivitiesReceived(const QVariantMap& json, int statusCode)
{
auto activities = json.value("ocs").toMap().value("data").toList();
qDebug() << "*** activities" << activities;
@ -164,6 +165,7 @@ void ActivityListModel::slotActivitiesReceived(const QVariantMap& json)
AccountState* ai = qvariant_cast<AccountState*>(sender()->property("AccountStatePtr"));
_currentlyFetching.remove(ai);
list.setAccountName( ai->account()->displayName());
foreach( auto activ, activities ) {
auto json = activ.toMap();

View file

@ -110,7 +110,7 @@ public slots:
void slotRemoveAccount( AccountState *ast );
private slots:
void slotActivitiesReceived(const QVariantMap& json);
void slotActivitiesReceived(const QVariantMap& json, int statusCode);
private:
void startFetchJob(AccountState* s);

View file

@ -22,11 +22,14 @@ ShibbolethUserJob::ShibbolethUserJob(AccountPtr account, QObject* parent)
: JsonApiJob(account, QLatin1String("ocs/v1.php/cloud/user"), parent)
{
setIgnoreCredentialFailure(true);
connect(this, SIGNAL(jsonRecieved(QVariantMap)), this, SLOT(slotJsonRecieved(QVariantMap)));
connect(this, SIGNAL(jsonReceived(QVariantMap, int)), this, SLOT(slotJsonReceived(QVariantMap, int)));
}
void ShibbolethUserJob::slotJsonRecieved(const QVariantMap &json)
void ShibbolethUserJob::slotJsonReceived(const QVariantMap &json, int statusCode)
{
if( statusCode != 100 ) {
qWarning() << "JSON Api call resulted in status code != 100";
}
QString user = json.value("ocs").toMap().value("data").toMap().value("id").toString();
//qDebug() << "cloud/user: " << json << "->" << user;
emit userFetched(user);

View file

@ -33,7 +33,7 @@ signals:
void userFetched(const QString &user);
private slots:
void slotJsonRecieved(const QVariantMap &);
void slotJsonReceived(const QVariantMap &, int statusCode);
};

View file

@ -209,7 +209,7 @@ void ConnectionValidator::slotAuthSuccess()
void ConnectionValidator::checkServerCapabilities()
{
JsonApiJob *job = new JsonApiJob(_account, QLatin1String("ocs/v1.php/cloud/capabilities"), this);
QObject::connect(job, SIGNAL(jsonRecieved(QVariantMap)), this, SLOT(slotCapabilitiesRecieved(QVariantMap)));
QObject::connect(job, SIGNAL(jsonReceived(QVariantMap, int)), this, SLOT(slotCapabilitiesRecieved(QVariantMap)));
job->start();
}

View file

@ -608,23 +608,40 @@ void JsonApiJob::start()
bool JsonApiJob::finished()
{
int statusCode = 0;
if (reply()->error() != QNetworkReply::NoError) {
qWarning() << "Network error: " << path() << reply()->errorString() << reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute);
emit jsonRecieved(QVariantMap());
emit jsonReceived(QVariantMap(), statusCode);
return true;
}
bool success = false;
QString jsonStr = QString::fromUtf8(reply()->readAll());
if( jsonStr.contains( "<?xml version=\"1.0\"?>") ) {
QRegExp rex("<statuscode>(\\d+)</statuscode>");
if( jsonStr.contains(rex) ) {
// this is a error message coming back from ocs.
statusCode = rex.cap(1).toInt();
}
} else {
QRegExp rex("\"statuscode\":(\\d+),");
// example: "{"ocs":{"meta":{"status":"ok","statuscode":100,"message":null},"data":{"version":{"major":8,"minor":"... (504)
if( jsonStr.contains(rex) ) {
statusCode = rex.cap(1).toInt();
}
}
bool success = false;
QVariantMap json = QtJson::parse(jsonStr, success).toMap();
// empty or invalid response
if (!success || json.isEmpty()) {
qWarning() << "invalid JSON!" << jsonStr;
emit jsonRecieved(QVariantMap());
emit jsonReceived(QVariantMap(), statusCode);
return true;
}
emit jsonRecieved(json);
emit jsonReceived(json, statusCode);
return true;
}

View file

@ -199,7 +199,7 @@ private slots:
* To be used like this:
* \code
* _job = new JsonApiJob(account, QLatin1String("ocs/v1.php/foo/bar"), this);
* connect(job, SIGNAL(jsonRecieved(QVariantMap)), ...)
* connect(job, SIGNAL(jsonReceived(QVariantMap)), ...)
* The received QVariantMap is empty in case of error or otherwise is a map as parsed by QtJson
* \encode
*
@ -221,12 +221,20 @@ public:
* This function needs to be called before start() obviously.
*/
void addQueryParams(QList< QPair<QString,QString> > params);
public slots:
void start() Q_DECL_OVERRIDE;
protected:
bool finished() Q_DECL_OVERRIDE;
signals:
void jsonRecieved(const QVariantMap &json);
/**
* @brief jsonReceived - signal to report the json answer from ocs
* @param json - the raw json string
* @param statusCode - the OCS status code: 100 (!) for success
*/
void jsonReceived(const QVariantMap &json, int statusCode);
private:
QList< QPair<QString,QString> > _additionalParams;
};