Kgapi

accountinfofetchjob.cpp
1/*
2 * This file is part of LibKGAPI library
3 *
4 * SPDX-FileCopyrightText: 2013 Daniel Vrátil <dvratil@redhat.com>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9#include "accountinfofetchjob.h"
10#include "account.h"
11#include "accountinfo.h"
12#include "debug.h"
13#include "utils.h"
14
15#include <QNetworkReply>
16#include <QNetworkRequest>
17
18using namespace KGAPI2;
19
20class Q_DECL_HIDDEN AccountInfoFetchJob::Private
21{
22};
23
25 : FetchJob(account, parent)
26 , d(new Private)
27{
28}
29
34
36{
37 const QUrl url = QUrl(QStringLiteral("https://www.googleapis.com/oauth2/v1/userinfo"));
38 QNetworkRequest request(url);
39
40 QStringList headers;
41 const auto rawHeaderList = request.rawHeaderList();
42 headers.reserve(rawHeaderList.size());
43 for (const QByteArray &str : std::as_const(rawHeaderList)) {
44 headers << QLatin1StringView(str) + QLatin1StringView(": ") + QLatin1StringView(request.rawHeader(str));
45 }
46
47 enqueueRequest(request);
48}
49
51{
53
54 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
55 ContentType ct = Utils::stringToContentType(contentType);
56 if (ct == KGAPI2::JSON) {
57 items << AccountInfo::fromJSON(rawData);
58 } else {
60 setErrorString(tr("Invalid response content type"));
62 }
63
64 return items;
65}
66
67#include "moc_accountinfofetchjob.cpp"
A job to fetch AccountInfo.
void start() override
KGAPI2::Job::start implementation.
AccountInfoFetchJob(const AccountPtr &account, QObject *parent=nullptr)
Constructs a new AccountInfoFetchJob.
~AccountInfoFetchJob() override
Destructor.
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::FetchJob::handleReplyWithItems implementation.
static AccountInfoPtr fromJSON(const QByteArray &jsonData)
Parses raw JSON data into AccountInfo object.
Abstract superclass for all jobs that fetch resources from Google.
Definition fetchjob.h:25
virtual ObjectsList items() const
Returns all items fetched by this job.
Definition fetchjob.cpp:41
void setErrorString(const QString &errorString)
Set job error description to errorString.
Definition job.cpp:401
virtual void emitFinished()
Emits Job::finished() signal.
Definition job.cpp:493
void setError(KGAPI2::Error error)
Set job error to error.
Definition job.cpp:386
virtual void enqueueRequest(const QNetworkRequest &request, const QByteArray &data=QByteArray(), const QString &contentType=QString())
Enqueues request in dispatcher queue.
Definition job.cpp:513
A job to fetch a single map tile described by a StaticMapUrl.
Definition blog.h:16
@ InvalidResponse
LibKGAPI error - Google returned invalid response.
Definition types.h:183
ContentType
Definition types.h:210
void reserve(qsizetype size)
QVariant header(QNetworkRequest::KnownHeaders header) const const
QByteArray rawHeader(const QByteArray &headerName) const const
QList< QByteArray > rawHeaderList() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:11:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.