KItinerary

httpresponse.cpp
1/*
2 SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "httpresponse.h"
7#include "logging.h"
8
9#include <QJsonArray>
10#include <QJsonDocument>
11#include <QJsonObject>
12#ifdef QT_NETWORK_LIB
13#include <QNetworkReply>
14#endif
15
16using namespace KItinerary;
17
18namespace KItinerary {
19class HttpResponsePrivate : public QSharedData {
20public:
21 QUrl url;
22 QByteArray content;
23 QDateTime requestDateTime;
24 // expand with HTTP headers, status code, etc as necessary
25};
26}
27
28HttpResponse::HttpResponse()
29 : d(new HttpResponsePrivate)
30{
31}
32
33HttpResponse::~HttpResponse() = default;
34HttpResponse::HttpResponse(const HttpResponse&) = default;
35HttpResponse& HttpResponse::operator=(const HttpResponse&) = default;
36
37HttpResponse::operator QVariant() const
38{
39 return QVariant::fromValue(*this);
40}
41
42QUrl HttpResponse::url() const
43{
44 return d->url;
45}
46
47QByteArray HttpResponse::content() const
48{
49 return d->content;
50}
51
52QDateTime HttpResponse::requestDateTime() const
53{
54 return d->requestDateTime;
55}
56
58{
60#ifdef QT_NETWORK_LIB
61 r.d->url = reply->url();
62 r.d->content = reply->readAll();
63 r.d->requestDateTime = QDateTime::currentDateTime();
64#endif
65 return r;
66}
67
69{
71 r.d->url = QUrl(harEntry.value(QLatin1StringView("request"))
72 .toObject()
73 .value(QLatin1StringView("url"))
74 .toString());
75 const auto content = harEntry.value(QLatin1StringView("response"))
76 .toObject()
77 .value(QLatin1StringView("content"))
78 .toObject();
79
80 r.d->content = content.value(QLatin1StringView("text")).toString().toUtf8();
81 if (content.value(QLatin1StringView("encoding")).toString() ==
82 QLatin1StringView("base64")) {
83 r.d->content = QByteArray::fromBase64(r.d->content);
84 }
85
86 r.d->requestDateTime = QDateTime::fromString(
87 harEntry.value(QLatin1StringView("startedDateTime")).toString(),
89 return r;
90}
91
93{
94 QJsonParseError error;
95 const auto doc = QJsonDocument::fromJson(harFile, &error);
96 if (error.error != QJsonParseError::NoError) {
97 qCWarning(Log) << error.errorString() << error.offset;
98 return {};
99 }
100
101 const auto entries = QJsonDocument::fromJson(harFile)
102 .object()
103 .value(QLatin1StringView("log"))
104 .toObject()
105 .value(QLatin1StringView("entries"))
106 .toArray();
107 QList<HttpResponse> result;
108 result.reserve(entries.size());
109 for (const auto &entry : entries) {
110 result.push_back(HttpResponse::fromHarEntry(entry.toObject()));
111 }
112 return result;
113}
114
115#include "moc_httpresponse.cpp"
Container for an HTTP response to be passed into the extractor engine.
static QList< HttpResponse > fromHarFile(const QByteArray &harFile)
Read a set of HTTP responses from an HAR file.
static HttpResponse fromHarEntry(const QJsonObject &harEntry)
Read from an HAR entry.
static HttpResponse fromNetworkReply(QNetworkReply *reply)
Create a HttpResponse object from an active QNetworkReply.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
QByteArray fromBase64(const QByteArray &base64, Base64Options options)
QDateTime currentDateTime()
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
QByteArray readAll()
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonObject object() const const
QJsonValue value(QLatin1StringView key) const const
QJsonArray toArray() const const
QJsonObject toObject() const const
void push_back(parameter_type value)
void reserve(qsizetype size)
QUrl url() const const
ISODateWithMs
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:14:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.