Exemplo n.º 1
0
void QTweetRetweetByMe::reply()
{
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());

    if (reply) {
         m_response = reply->readAll();
        emit finished(m_response);

        reply->deleteLater();

        if (isJsonParsingEnabled())
            parseJson(m_response);
    }
}
Exemplo n.º 2
0
/**
 *  Called after response from twitter
 */
void QTweetNetBase::reply()
{
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());

    // prep m_response for finished() signal
    if (reply) {
    	m_response = reply->readAll();
    } else {
        m_response.clear();
    }

    // ALWAYS emit a finished signal no matter what happens
	emit finished(m_response);


	// now analyze the response
    if (reply) {

    	QString rateLimit(reply->rawHeader("X-Rate-Limit-Limit"));
    	QString rateRemaining(reply->rawHeader("X-Rate-Limit-Remaining"));
    	QString limitRest(reply->rawHeader("X-Rate-Limit-Reset"));

    	uint timeToReset = limitRest.toLongLong()- QDateTime::currentDateTime().toTime_t();

    	QString contentType(reply->rawHeader("content-type"));
    	bool contentTypeHasJson = contentType.contains("application/json", Qt::CaseInsensitive);

        if (reply->error() == QNetworkReply::NoError) {
        	if (isJsonParsingEnabled() && contentTypeHasJson) {
        		parseJson(m_response);
        	}
        } else {
            //dump error
            qDebug() << "Network error: " << reply->error();
            qDebug() << "Error string: " << reply->errorString();
            qDebug() << "Error response: " << m_response;

            //HTTP status code
            int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

            //prep error
            QTweetNetError* err = 0;

            if (!contentTypeHasJson) {
            	err = new QTweetHttpError(httpStatus, m_response);
            } else {
				//try to json parse the error response
				QJson::Parser parser;
				bool ok;
				QVariant json = parser.parse(m_response, &ok);
				QVariantMap errMsgMap = json.toMap();
				QMap<int, QString> twitterErrors;

				if (ok) {
					// here are a few sample responses we've seen:
					//{"request":"\/1\/users\/search.json?q=&per_page=20&page=1","error":"Client must provide a 'q' parameter with a value."}
					//{"error":"You must enter a query."}
					//{"errors":[{"message": "...", "code": 34}]}
					QList<QVariant> errors = errMsgMap["errors"].toList();
					QList<QVariant>::iterator it = errors.begin();

					while (it != errors.end()) {
						QVariantMap error = (*it).toMap();
						int errorCode = error["code"].toInt();
						QString errorMessage = error["message"].toString();

						twitterErrors[errorCode] = errorMessage;

						it++;
					}

					if (twitterErrors.size() > 0) {
						err = new QTweetServiceErrors(httpStatus, m_response, json, twitterErrors);
					} else {
						qDebug() << "Parsing json ok but twitter errors not found";
						err = new QTweetRawServiceError(httpStatus, m_response, json);
					}
				}
				else {
					qDebug() << "Unable to parse json in response";
					err = new QTweetHttpError(httpStatus, m_response);
				}
            }

            //finally emit the error signal
            if (err) {
            	emit error(*err);
            	err->deleteLater();
            } else {
            	Q_ASSERT(false);
            }
		}
        reply->deleteLater();
    }
}