From 679e5eb1956408b5926ac900040e462a552d8fcd Mon Sep 17 00:00:00 2001 From: Mikhail Visloguzov Date: Fri, 2 Feb 2018 15:59:59 +0300 Subject: [PATCH] Adding Qt REST example. --- qt/rest/qflightxmlclient/Readme.md | 14 ++++ qt/rest/qflightxmlclient/main.cpp | 13 +++ qt/rest/qflightxmlclient/qflightxmlclient.cpp | 79 +++++++++++++++++++ qt/rest/qflightxmlclient/qflightxmlclient.h | 32 ++++++++ qt/rest/qflightxmlclient/qflightxmlclient.pro | 22 ++++++ 5 files changed, 160 insertions(+) create mode 100644 qt/rest/qflightxmlclient/Readme.md create mode 100644 qt/rest/qflightxmlclient/main.cpp create mode 100644 qt/rest/qflightxmlclient/qflightxmlclient.cpp create mode 100644 qt/rest/qflightxmlclient/qflightxmlclient.h create mode 100644 qt/rest/qflightxmlclient/qflightxmlclient.pro diff --git a/qt/rest/qflightxmlclient/Readme.md b/qt/rest/qflightxmlclient/Readme.md new file mode 100644 index 0000000..45804b8 --- /dev/null +++ b/qt/rest/qflightxmlclient/Readme.md @@ -0,0 +1,14 @@ +Requirements +------------ + +* Qt >= 5.8 + + +What to change +------------- + +Substitute your actual username and API key in the QFlightXMLClient class. You can also change the airlineCode in main.cpp (line 9). + +Running the example +------------------- +Open the project in Qt Creator and run the example using the IDE. \ No newline at end of file diff --git a/qt/rest/qflightxmlclient/main.cpp b/qt/rest/qflightxmlclient/main.cpp new file mode 100644 index 0000000..b3619c5 --- /dev/null +++ b/qt/rest/qflightxmlclient/main.cpp @@ -0,0 +1,13 @@ +#include +#include + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + QFlightXMLClient client; + const QString airlineCode("SBI"); + client.airlineInfoRequest(airlineCode); + + return a.exec(); +} diff --git a/qt/rest/qflightxmlclient/qflightxmlclient.cpp b/qt/rest/qflightxmlclient/qflightxmlclient.cpp new file mode 100644 index 0000000..ca7cd5f --- /dev/null +++ b/qt/rest/qflightxmlclient/qflightxmlclient.cpp @@ -0,0 +1,79 @@ +#include "qflightxmlclient.h" +#include +#include +#include + +QFlightXMLClient::QFlightXMLClient(QObject *parent) : QObject(parent) +{ + connect(&networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, \ + SLOT(serviceRequestFinished(QNetworkReply*))); + + connect(&networkAccessManager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), + SLOT(provideAuthenication(QNetworkReply*,QAuthenticator*))); +} + +void QFlightXMLClient::airlineInfoRequest(const QString& airlineCode) +{ + airline = airlineCode; + QUrl url = fxmlUrl.append("/AirlineInfo"); + QUrlQuery query; + query.addQueryItem("airline_code", airlineCode); + + url.setQuery(query.query()); + + QNetworkRequest request(url); + + networkAccessManager.get(request); +} + +void QFlightXMLClient::serviceRequestFinished(QNetworkReply *reply) +{ + if (reply->error() > 0) + { + qWarning() << reply->errorString(); + return; + } + + QJsonDocument document = QJsonDocument::fromJson(reply->readAll()); + QJsonObject rootObj = document.object(); + + if(!rootObj.empty()) + { + parseAirlineInfo(rootObj); + } + else + { + qWarning() << "json is empty"; + } +} + +void QFlightXMLClient::provideAuthenication(QNetworkReply *, QAuthenticator *ator) +{ + ator->setUser(Username); + ator->setPassword(ApiKey); +} + +void QFlightXMLClient::parseAirlineInfo(QJsonObject &rootObj) +{ + QJsonValue value = rootObj.value(QString("AirlineInfoResult")); + QString airlineInfo("Airline name: %1; shortname: %2; callsign: %3; location: %4; " + "country: %5; phone: %6; url: %7; wiki_url: %8; " + "airbourne: %9"); + QJsonObject item = value.toObject(); + + QJsonValue name = item.value(QString("name")); + QJsonValue shortname = item.value(QString("shortname")); + QJsonValue callsign = item.value(QString("callsign")); + QJsonValue location = item.value(QString("location")); + QJsonValue country = item.value(QString("country")); + QJsonValue phone = item.value(QString("phone")); + QJsonValue url = item.value(QString("url")); + QJsonValue wiki_url = item.value(QString("wiki_url")); + QJsonValue airbourne = item.value(QString("airbourne")); + + qDebug() << airlineInfo.arg(name.toString(), shortname.toString(), callsign.toString(), + location.toString(), country.toString(), + phone.toString(), url.toString(), wiki_url.toString(), + airbourne.toString()); +} + diff --git a/qt/rest/qflightxmlclient/qflightxmlclient.h b/qt/rest/qflightxmlclient/qflightxmlclient.h new file mode 100644 index 0000000..a09f007 --- /dev/null +++ b/qt/rest/qflightxmlclient/qflightxmlclient.h @@ -0,0 +1,32 @@ +#ifndef QFLIGHTXMLCLIENT_H +#define QFLIGHTXMLCLIENT_H + +#include +#include + +class QNetworkReply; +class QAuthenticator; + +class QFlightXMLClient : public QObject +{ + Q_OBJECT +public: + explicit QFlightXMLClient(QObject *parent = nullptr); + void airlineInfoRequest(const QString& airlineCode); + +private slots: + void serviceRequestFinished(QNetworkReply*); + void provideAuthenication(QNetworkReply *reply, QAuthenticator *ator); + +private: + QString fxmlUrl = "http://flightxml.flightaware.com/json/FlightXML3"; + const QString Username = "slydog1"; + const QString ApiKey = "46a113b8bec4e98da029306e4a605c1fb9d005b1"; + + QNetworkAccessManager networkAccessManager; + QString airline; + + void parseAirlineInfo(QJsonObject &rootObj); +}; + +#endif // QFLIGHTXMLCLIENT_H diff --git a/qt/rest/qflightxmlclient/qflightxmlclient.pro b/qt/rest/qflightxmlclient/qflightxmlclient.pro new file mode 100644 index 0000000..6b13ba3 --- /dev/null +++ b/qt/rest/qflightxmlclient/qflightxmlclient.pro @@ -0,0 +1,22 @@ +QT -= gui +QT += network + +CONFIG += c++11 #console +CONFIG -= app_bundle + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which as been marked deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += main.cpp \ + qflightxmlclient.cpp + +HEADERS += \ + qflightxmlclient.h