Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions qt/rest/qflightxmlclient/Readme.md
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 13 additions & 0 deletions qt/rest/qflightxmlclient/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <QCoreApplication>
#include <qflightxmlclient.h>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QFlightXMLClient client;
const QString airlineCode("SBI");
client.airlineInfoRequest(airlineCode);

return a.exec();
}
79 changes: 79 additions & 0 deletions qt/rest/qflightxmlclient/qflightxmlclient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "qflightxmlclient.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QtNetwork>

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"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parsing an integer from the result as a string results in an empty variable.
Instead try:
item.value("airbourne").toVariant().toString()

Do you also want to include the final response value of flights_last_24_hours?


qDebug() << airlineInfo.arg(name.toString(), shortname.toString(), callsign.toString(),
location.toString(), country.toString(),
phone.toString(), url.toString(), wiki_url.toString(),
airbourne.toString());
}

32 changes: 32 additions & 0 deletions qt/rest/qflightxmlclient/qflightxmlclient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef QFLIGHTXMLCLIENT_H
#define QFLIGHTXMLCLIENT_H

#include <QObject>
#include <QNetworkAccessManager>

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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove personal credentials.
This API key has now been revoked. Please return to the FlightXML Key Generation to get a new one.

const QString ApiKey = "46a113b8bec4e98da029306e4a605c1fb9d005b1";

QNetworkAccessManager networkAccessManager;
QString airline;

void parseAirlineInfo(QJsonObject &rootObj);
};

#endif // QFLIGHTXMLCLIENT_H
22 changes: 22 additions & 0 deletions qt/rest/qflightxmlclient/qflightxmlclient.pro
Original file line number Diff line number Diff line change
@@ -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