ArtifactRequest.cpp Example File
mavenartifacts/src/ArtifactRequest.cpp
#include "ArtifactRequest.hpp"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QUrl>
ArtifactRequest::ArtifactRequest(QObject *parent)
: QObject(parent)
{
}
void ArtifactRequest::requestArtifactline(const QString &artifactName)
{
QNetworkAccessManager* networkAccessManager = new QNetworkAccessManager(this);
const QString queryUri = QString::fromLatin1("http://search.maven.org/solrsearch/select?q=%1&rows=20&wt=json").arg(artifactName);
QNetworkRequest request(queryUri);
QNetworkReply* reply = networkAccessManager->get(request);
bool ok = connect(reply, SIGNAL(finished()), this, SLOT(onArtifactlineReply()));
Q_ASSERT(ok);
Q_UNUSED(ok);
}
void ArtifactRequest::onArtifactlineReply()
{
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
QString response;
bool success = false;
if (reply) {
if (reply->error() == QNetworkReply::NoError) {
const int available = reply->bytesAvailable();
if (available > 0) {
const QByteArray buffer = reply->readAll();
response = QString::fromUtf8(buffer);
success = true;
}
} else {
response = tr("Error: %1 status: %2").arg(reply->errorString(), reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString());
}
reply->deleteLater();
}
if (response.trimmed().isEmpty()) {
response = tr("Artifact request failed. Check internet connection");
}
emit complete(response, success);
}