cleaned up code

This commit is contained in:
Elisa Lupin-Jimenez 2017-12-19 13:22:31 -08:00
parent 34d8fe307d
commit 1b6c85edb0
2 changed files with 2 additions and 111 deletions

View file

@ -43,13 +43,10 @@ void GooglePolyScriptingInterface::setAPIKey(QString key) {
QString GooglePolyScriptingInterface::getAssetList(QString keyword, QString category, QString format) {
QUrl url = formatURLQuery(keyword, category, format);
if (!url.isEmpty()) {
//QJsonArray json = parseJSON(url, 0).toJsonArray();
//qCDebug(scriptengine) << "first asset name: " << json.at(0).toObject().value("displayName");
QByteArray json = parseJSON(url, 0).toJsonDocument().toJson();
return (QString) json;
} else {
qCDebug(scriptengine) << "Invalid filters were specified.";
//return QJsonArray();
return "";
}
}
@ -87,7 +84,7 @@ QString GooglePolyScriptingInterface::getTilt(QString keyword, QString category)
}
*/
// can provide asset name or full URL to model
// Can provide asset name or full URL to model
QString GooglePolyScriptingInterface::getModelInfo(QString name) {
if (name.contains("poly.googleapis") || name.contains("poly.google.com")) {
QStringList list = name.split("/");
@ -101,10 +98,6 @@ QString GooglePolyScriptingInterface::getModelInfo(QString name) {
return json;
}
void GooglePolyScriptingInterface::testPrint(QString input) {
qCDebug(scriptengine) << "Google message: " << input;
}
int GooglePolyScriptingInterface::getRandIntInRange(int length) {
QTime time = QTime::currentTime();
qsrand((uint)time.msec());
@ -152,7 +145,7 @@ QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) {
return response->readAll();
}
// since the list is a QJsonArray and a single model is a QJsonObject
// 0 = asset list, 1 = model from asset list, 2 = specific model
QVariant GooglePolyScriptingInterface::parseJSON(QUrl url, int fileType) {
//QString firstItem = QString::fromUtf8(response->readAll());
QByteArray jsonString = getHTTPRequest(url);
@ -173,109 +166,9 @@ QVariant GooglePolyScriptingInterface::parseJSON(QUrl url, int fileType) {
return json.value("formats").toArray().at(0).toObject().value("root").toObject().value("url");
}
// return whole asset list
//return arr;
return QJsonDocument(arr);
// return specific object
} else {
return jsonString;
}
}
/*
// FIXME: synchronous = not good code
QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) {
QNetworkAccessManager manager;
QNetworkReply *response = manager.get(QNetworkRequest(url));
QEventLoop event;
connect(response, SIGNAL(finished()), &event, SLOT(quit()));
event.exec();
return response->readAll();
}
/* NONE OF THESE HTTP GET METHODS WORK D:
https://stackoverflow.com/questions/28267477/getting-a-page-content-with-qt kinda used rn
this correctly returns the asset list but is apparently synchronous and not a good way to do it
https://stackoverflow.com/questions/24965972/qt-getting-source-html-code-of-a-web-page-hosted-on-the-internet
void GooglePolyScriptingInterface::getAssetList() {
authCode = "AIzaSyDamk7Vth52j7aU9JVKn3ungFS0kGJYc8A";
QUrl url(listPolyUrl + "key=" + authCode);
QNetworkAccessManager manager;
QNetworkReply *response = manager.get(QNetworkRequest(url));
QEventLoop event;
connect(response, SIGNAL(finished()), &event, SLOT(quit()));
event.exec();
QString firstItem = response->readAll();
//QJsonArray arr;
//QJsonObject jsonObject = QJsonDocument::fromJson(response->readAll()).object();
//QString firstItem = jsonObject["assets"].toString();
qCDebug(scriptengine) << "first item: " << firstItem;
//qCDebug(scriptengine) << "api key: " << authCode;
//return arr;
}
this didn't work either https://stackoverflow.com/a/24966317
QScopedPointer<QNetworkAccessManager> manager(new QNetworkAccessManager);
QNetworkReply* response = manager->get(QNetworkRequest(url));
QObject::connect(manager, &QNetworkAccessManager::finished, [manager, response] {
response->deleteLater();
manager->deleteLater();
if (response->error() != QNetworkReply::NoError) return;
QString contentType =
response->header(QNetworkRequest::ContentTypeHeader).toString();
if (!contentType.contains("charset=utf-8")) {
qWarning() << "Content charsets other than utf-8 are not implemented yet.";
return;
}
QString html = QString::fromUtf8(response->readAll());
// do something with the data
}) && manager.take();
or this :( https://stackoverflow.com/questions/39518749/make-get-request-to-web-service-get-json-response-and-update-gui-in-qt
qCDebug(scriptengine) << "gonna get assets with " << url;
QNetworkAccessManager manager;
QNetworkReply* reply = manager.get(QNetworkRequest(url));
QObject::connect(&manager, &QNetworkAccessManager::finished,
[reply]{
qCDebug(scriptengine) << "boo radley";
if (reply->error() != QNetworkReply::NoError) {
qCDebug(scriptengine) << "Poly API failed to respond";
//manager.clearAccessCache();
} else {
QJsonObject jsonObject = QJsonDocument::fromJson(reply->readAll()).object();
QString firstItem = jsonObject["assets"].toString();
qCDebug(scriptengine) << "first item: " << firstItem;
}
reply->deleteLater();
});
or this :(( http://blog.mathieu-leplatre.info/access-a-json-webservice-with-qt-c.html
//QJsonArray arr;
//QJsonObject jsonObject = QJsonDocument::fromJson(response->readAll()).object();
//QString firstItem = jsonObject["assets"].toString();
qCDebug(scriptengine) << "first item: " << firstItem;
//qCDebug(scriptengine) << "api key: " << authCode;
//return arr;
QNetworkAccessManager manager;
QNetworkReply* response = manager.get(QNetworkRequest(url));
QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onResult(QNetworkReply*)));
return "";
}
void GooglePolyScriptingInterface::onResult(QNetworkReply* reply) {
if (reply->error() != QNetworkReply::NoError) {
qCDebug(scriptengine) << "Poly API failed to respond";
return;
}
QString firstItem = reply->readAll();
qCDebug(scriptengine) << "first item: " << firstItem;
}
*/

View file

@ -22,7 +22,6 @@ public:
GooglePolyScriptingInterface();
public slots:
void testPrint(QString input);
void setAPIKey(QString key);
QString getAssetList(QString keyword, QString category, QString format);
@ -40,7 +39,6 @@ private:
QByteArray getHTTPRequest(QUrl url);
QVariant parseJSON(QUrl url, int fileType);
int getRandIntInRange(int length);
//void onResult(QNetworkReply* reply);
QString authCode;