9#include "personfetchjob.h"
10#include "peopleservice.h"
17#include <QNetworkRequest>
18#include <QNetworkReply>
20#include <QJsonDocument>
24namespace KGAPI2::People
27class Q_DECL_HIDDEN PersonFetchJob::Private
30 explicit Private(PersonFetchJob *parent);
33 ObjectsList processReceivedData(
const QByteArray &rawData);
43 PersonFetchJob *
const q;
46PersonFetchJob::Private::Private(PersonFetchJob *parent)
54 request.setRawHeader(
"Host",
"people.googleapis.com");
58void PersonFetchJob::Private::startFetch()
61 if (personResourceName.isEmpty()) {
62 url = PeopleService::fetchAllContactsUrl(syncToken);
64 url = PeopleService::fetchContactUrl(personResourceName);
68 q->enqueueRequest(request);
71ObjectsList PersonFetchJob::Private::processReceivedData(
const QByteArray &rawData)
76 if (personResourceName.isEmpty()) {
77 items = PeopleService::parseConnectionsJSONFeed(feedData, rawData, syncToken);
80 if(jsonDocumentFromData.isObject()) {
81 items << People::Person::fromJSON(jsonDocumentFromData.object());
83 qCDebug(KGAPIDebug) <<
"JSON document does not have object";
87 if (feedData.nextPageUrl.isValid()) {
88 q->emitProgress(feedData.startIndex, feedData.totalResults);
91 q->enqueueRequest(request);
93 receivedSyncToken = feedData.syncToken;
100PersonFetchJob::PersonFetchJob(
const AccountPtr& account,
QObject* parent)
101 : FetchJob(account, parent)
102 , d(std::make_unique<Private>(this))
106PersonFetchJob::PersonFetchJob(
const QString &resourceName,
const AccountPtr &account,
QObject* parent)
107 : FetchJob(account, parent)
108 , d(std::make_unique<Private>(this))
110 d->personResourceName = resourceName;
113PersonFetchJob::~PersonFetchJob() =
default;
115QString PersonFetchJob::syncToken()
const
120void PersonFetchJob::setSyncToken(
const QString &syncToken)
122 if (syncToken == d->syncToken) {
126 d->syncToken = syncToken;
127 Q_EMIT syncTokenChanged();
130QString PersonFetchJob::receivedSyncToken()
const
132 return d->receivedSyncToken;
135void PersonFetchJob::start()
140bool PersonFetchJob::handleError(
int statusCode,
const QByteArray &rawData)
144 if (error[u
"error"][u
"status"].
toString() == u
"INVALID_ARGUMENT") {
145 qCDebug(KGAPIDebug) <<
"Sync token is invalid, redoing request with no syncToken";
146 d->syncToken.clear();
150 for (
const auto detail : error[u
"error"][u
"details"].toArray()) {
151 if (detail[u
"reason"].
toString() == u
"EXPIRED_SYNC_TOKEN") {
152 qCDebug(KGAPIDebug) <<
"Sync token expired, redoing request with no syncToken";
153 d->syncToken.clear();
160 return FetchJob::handleError(statusCode, rawData);
166 const auto ct = Utils::stringToContentType(contentType);
168 if (ct == KGAPI2::JSON) {
169 return d->processReceivedData(rawData);
172 return ObjectsList();
177#include "moc_personfetchjob.cpp"
char * toString(const EngineQuery &query)
@ BadRequest
Invalid (malformed) request.
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QString toString() const const