Kgapi

personphotoupdatejob.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 "personphotoupdatejob.h"
10#include "peopleservice.h"
11#include "person.h"
12#include "utils.h"
13
14#include <QNetworkRequest>
15#include <QNetworkReply>
16#include <QUrlQuery>
17#include <QJsonDocument>
18#include <QJsonObject>
19#include <QJsonArray>
20
21namespace KGAPI2::People
22{
23
24class Q_DECL_HIDDEN PersonPhotoUpdateJob::Private
25{
26public:
27 explicit Private(PersonPhotoUpdateJob *parent);
28 void processPersonPhoto();
29
30 QString personResourceName;
31 QByteArray photoRawData;
32
33private:
34 PersonPhotoUpdateJob * const q;
35};
36
37PersonPhotoUpdateJob::Private::Private(PersonPhotoUpdateJob *parent)
38 : q(parent)
39{
40}
41
42void PersonPhotoUpdateJob::Private::processPersonPhoto()
43{
44 const auto modifyUrl = PeopleService::updateContactPhotoUrl(personResourceName);
45 QNetworkRequest request(modifyUrl);
46 request.setRawHeader("Host", "people.googleapis.com");
47
48 const QJsonObject rootObject {
49 {QStringLiteral("photoBytes"), QString::fromUtf8(photoRawData.toBase64())},
50 {QStringLiteral("personFields"), PeopleService::allPersonFields()}
51 };
52 const QJsonDocument jsonDoc(rootObject);
53 const auto rawData = jsonDoc.toJson();
54
55 q->enqueueRequest(request, rawData, QStringLiteral("application/json"));
56}
57
58PersonPhotoUpdateJob::PersonPhotoUpdateJob(const QString &personResourceName, const QByteArray &photoRawData, const AccountPtr &account, QObject* parent)
59 : ModifyJob(account, parent)
60 , d(std::make_unique<Private>(this))
61{
62 d->personResourceName = personResourceName;
63 d->photoRawData = photoRawData;
64}
65
66PersonPhotoUpdateJob::~PersonPhotoUpdateJob() = default;
67
68void PersonPhotoUpdateJob::start()
69{
70 d->processPersonPhoto();
71}
72
73void PersonPhotoUpdateJob::dispatchRequest(QNetworkAccessManager *accessManager,
74 const QNetworkRequest &request,
75 const QByteArray &data,
76 const QString &contentType)
77{
78 QNetworkRequest r = request;
79 if (!r.hasRawHeader("Content-Type")) {
81 }
82
83 if (!r.hasRawHeader("If-Match")) {
84 r.setRawHeader("If-Match", "*");
85 }
86
87 accessManager->sendCustomRequest(r, "PATCH", data);
88}
89
90ObjectsList PersonPhotoUpdateJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
91{
92 const auto contentTypeString = reply->header(QNetworkRequest::ContentTypeHeader).toString();
93 const auto contentType = Utils::stringToContentType(contentTypeString);
94
95 if (contentType != KGAPI2::JSON) {
97 setErrorString(tr("Invalid response content type"));
98 emitFinished();
99 return {};
100 }
101
102 ObjectsList items;
103 const auto json = QJsonDocument::fromJson(rawData);
104 if (json.isObject()) {
105 const auto jsonObject = json.object();
106 const auto personJson = jsonObject.value(QStringLiteral("person")).toObject();
107 const auto person = Person::fromJSON(personJson);
108 items << person;
109 }
110
111 emitFinished();
112 return items;
113}
114
115}
116
117#include "moc_personphotoupdatejob.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 fromUtf8(QByteArrayView str)
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.