Kgapi

personcreatejob.cpp
1/*
2 * This file is part of LibKGAPI library
3 *
4 * SPDX-FileCopyrightText: 2023 Claudio Cambra <claudio.cambra@kde.org>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9#include "personcreatejob.h"
10#include "peopleservice.h"
11#include "person.h"
12#include "private/queuehelper_p.h"
13#include "utils.h"
14
15#include <QNetworkRequest>
16#include <QNetworkReply>
17#include <QUrlQuery>
18#include <QJsonDocument>
19#include <QJsonObject>
20#include <QJsonArray>
21
22#include <KContacts/Picture>
23
24namespace KGAPI2::People
25{
26
27class Q_DECL_HIDDEN PersonCreateJob::Private
28{
29public:
30 explicit Private(PersonCreateJob *parent);
31 void processNextPerson();
32
33 QueueHelper<PersonPtr> people;
34
35private:
36 PersonCreateJob * const q;
37};
38
39PersonCreateJob::Private::Private(PersonCreateJob *parent)
40 : q(parent)
41{
42}
43
44void PersonCreateJob::Private::processNextPerson()
45{
46 if (people.atEnd()) {
47 q->emitFinished();
48 return;
49 }
50
51 const auto person = people.current();
52 static const auto createUrl = PeopleService::createContactUrl();
53 QNetworkRequest request(createUrl);
54 request.setRawHeader("Host", "people.googleapis.com");
55
56 const auto personJson = QJsonDocument(person->toJSON().toObject());
57 const auto rawData = personJson.toJson();
58 q->enqueueRequest(request, rawData, QStringLiteral("application/json"));
59}
60
61PersonCreateJob::PersonCreateJob(const PersonList &people, const AccountPtr &account, QObject* parent)
62 : CreateJob(account, parent)
63 , d(std::make_unique<Private>(this))
64{
65 d->people = people;
66}
67
68PersonCreateJob::PersonCreateJob(const PersonPtr &person, const AccountPtr &account, QObject* parent)
69 : CreateJob(account, parent)
70 , d(std::make_unique<Private>(this))
71{
72 d->people << person;
73}
74
75PersonCreateJob::~PersonCreateJob() = default;
76
77void PersonCreateJob::start()
78{
79 d->processNextPerson();
80}
81
82ObjectsList PersonCreateJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
83{
84 const auto contentTypeString = reply->header(QNetworkRequest::ContentTypeHeader).toString();
85 const auto contentType = Utils::stringToContentType(contentTypeString);
86
87 if (contentType != KGAPI2::JSON) {
89 setErrorString(tr("Invalid response content type"));
90 emitFinished();
91 return {};
92 }
93
94 ObjectsList items;
95 const auto json = QJsonDocument::fromJson(rawData);
96 if (json.isObject()) {
97 const auto person = Person::fromJSON(json.object());
98 items << person;
99 }
100
101 d->people.currentProcessed();
102 d->processNextPerson();
103
104 return items;
105}
106
107}
108
109#include "moc_personcreatejob.cpp"
@ InvalidResponse
LibKGAPI error - Google returned invalid response.
Definition types.h:183
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QVariant header(QNetworkRequest::KnownHeaders header) const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:08:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.