Kgapi

personmodifyjob.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 "personmodifyjob.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
22namespace KGAPI2::People
23{
24
25class Q_DECL_HIDDEN PersonModifyJob::Private
26{
27public:
28 explicit Private(PersonModifyJob *parent);
29 void processNextPerson();
30
31 QueueHelper<PersonPtr> people;
32
33private:
34 PersonModifyJob * const q;
35};
36
37PersonModifyJob::Private::Private(PersonModifyJob *parent)
38 : q(parent)
39{
40}
41
42void PersonModifyJob::Private::processNextPerson()
43{
44 if (people.atEnd()) {
45 q->emitFinished();
46 return;
47 }
48
49 const auto person = people.current();
50 const auto modifyUrl = PeopleService::updateContactUrl(person->resourceName(), PeopleService::allUpdatablePersonFields());
51 QNetworkRequest request(modifyUrl);
52 request.setRawHeader("Host", "people.googleapis.com");
53
54 const auto personJson = QJsonDocument(person->toJSON().toObject());
55 const auto rawData = personJson.toJson();
56 q->enqueueRequest(request, rawData, QStringLiteral("application/json"));
57}
58
59PersonModifyJob::PersonModifyJob(const PersonList &people, const AccountPtr &account, QObject* parent)
60 : ModifyJob(account, parent)
61 , d(std::make_unique<Private>(this))
62{
63 d->people = people;
64}
65
66PersonModifyJob::PersonModifyJob(const PersonPtr &person, const AccountPtr &account, QObject* parent)
67 : ModifyJob(account, parent)
68 , d(std::make_unique<Private>(this))
69{
70 d->people << person;
71}
72
73PersonModifyJob::~PersonModifyJob() = default;
74
75void PersonModifyJob::start()
76{
77 d->processNextPerson();
78}
79
80void PersonModifyJob::dispatchRequest(QNetworkAccessManager *accessManager,
81 const QNetworkRequest &request,
82 const QByteArray &data,
83 const QString &contentType)
84{
85 QNetworkRequest r = request;
86 if (!r.hasRawHeader("Content-Type")) {
88 }
89
90 if (!r.hasRawHeader("If-Match")) {
91 r.setRawHeader("If-Match", "*");
92 }
93
94 accessManager->sendCustomRequest(r, "PATCH", data);
95}
96
97ObjectsList PersonModifyJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
98{
99 const auto contentTypeString = reply->header(QNetworkRequest::ContentTypeHeader).toString();
100 const auto contentType = Utils::stringToContentType(contentTypeString);
101
102 if (contentType != KGAPI2::JSON) {
103 setError(KGAPI2::InvalidResponse);
104 setErrorString(tr("Invalid response content type"));
105 emitFinished();
106 return {};
107 }
108
109 ObjectsList items;
110 const auto json = QJsonDocument::fromJson(rawData);
111 if (json.isObject()) {
112 const auto person = Person::fromJSON(json.object());
113 items << person;
114 }
115
116 d->people.currentProcessed();
117 d->processNextPerson();
118
119 return items;
120}
121
122}
123
124#include "moc_personmodifyjob.cpp"
@ InvalidResponse
LibKGAPI error - Google returned invalid response.
Definition types.h:183
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QNetworkReply * sendCustomRequest(const QNetworkRequest &request, const QByteArray &verb, QHttpMultiPart *multiPart)
QVariant header(QNetworkRequest::KnownHeaders header) const const
bool hasRawHeader(const QByteArray &headerName) const const
void setHeader(KnownHeaders header, const QVariant &value)
void setRawHeader(const QByteArray &headerName, const QByteArray &headerValue)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.