Commit 86a5d92d authored by Volker Krause's avatar Volker Krause

Add coordinate-based location queries for the Navitia backend

parent 3fa156c2
......@@ -26,6 +26,8 @@
#include <KPublicTransport/JourneyReply>
#include <KPublicTransport/JourneyRequest>
#include <KPublicTransport/Location>
#include <KPublicTransport/LocationReply>
#include <KPublicTransport/LocationRequest>
#include <QNetworkAccessManager>
#include <QNetworkReply>
......@@ -139,3 +141,59 @@ bool NavitiaBackend::queryDeparture(DepartureReply *reply, QNetworkAccessManager
return true;
}
bool NavitiaBackend::queryLocation(LocationReply *reply, QNetworkAccessManager *nam) const
{
const auto req = reply->request();
QUrl url;
url.setScheme(QStringLiteral("https"));
url.setHost(m_endpoint);
QUrlQuery query;
query.addQueryItem(QStringLiteral("disable_geojson"), QStringLiteral("true"));
query.addQueryItem(QStringLiteral("depth"), QStringLiteral("0"));
query.addQueryItem(QStringLiteral("type[]"), QStringLiteral("stop_area"));
// TODO count
if (req.hasCoordinate()) {
url.setPath(
QStringLiteral("/v1/coord/") + QString::number(req.latitude()) + QLatin1Char(';') + QString::number(req.longitude()) +
QStringLiteral("/places_nearby")
);
// TODO distance
} else {
// TODO
return false;
}
url.setQuery(query);
QNetworkRequest netReq(url);
netReq.setRawHeader("Authorization", m_auth.toUtf8());
qCDebug(Log) << "GET:" << url;
auto netReply = nam->get(netReq);
QObject::connect(netReply, &QNetworkReply::finished, [netReply, reply] {
qDebug() << netReply->request().url() << netReply->errorString();
// TODO cache handling
switch (netReply->error()) {
case QNetworkReply::NoError:
if (reply->request().hasCoordinate()) {
addResult(reply, NavitiaParser::parsePlacesNearby(netReply->readAll()));
} else {
// TODO
}
break;
case QNetworkReply::ContentNotFoundError:
addError(reply, Reply::NotFoundError, NavitiaParser::parseErrorMessage(netReply->readAll()));
break;
default:
addError(reply, Reply::NetworkError, netReply->errorString());
qCDebug(Log) << netReply->error() << netReply->errorString();
break;
}
netReply->deleteLater();
});
return true;
}
......@@ -38,6 +38,7 @@ public:
bool isSecure() const override;
bool queryJourney(JourneyReply *reply, QNetworkAccessManager *nam) const override;
bool queryDeparture(DepartureReply *reply, QNetworkAccessManager *nam) const override;
bool queryLocation(LocationReply *reply, QNetworkAccessManager *nam) const override;
private:
QString m_endpoint;
......
......@@ -230,6 +230,22 @@ std::vector<Departure> NavitiaParser::parseDepartures(const QByteArray &data)
return res;
}
std::vector<Location> NavitiaParser::parsePlacesNearby(const QByteArray &data)
{
qDebug().noquote() << data;
const auto topObj = QJsonDocument::fromJson(data).object();
const auto placesNearby = topObj.value(QLatin1String("places_nearby")).toArray();
std::vector<Location> res;
res.reserve(placesNearby.size());
for (const auto &v : placesNearby) {
res.push_back(parseWrappedLocation(v.toObject()));
}
return res;
}
QString NavitiaParser::parseErrorMessage(const QByteArray &data)
{
const auto topObj = QJsonDocument::fromJson(data).object();
......
......@@ -27,12 +27,14 @@ namespace KPublicTransport {
class Departure;
class Journey;
class Location;
/** Navitia REST response parser. */
namespace NavitiaParser
{
std::vector<Journey> parseJourneys(const QByteArray &data);
std::vector<Departure> parseDepartures(const QByteArray &data);
std::vector<Location> parsePlacesNearby(const QByteArray &data);
QString parseErrorMessage(const QByteArray &data);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment