summaryrefslogtreecommitdiffstats
path: root/examples/demos/addressbook/restaccessmanager.h
blob: 19d417ede4928d77dfd8af719adac7208ce919ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#ifndef RESTACCESSMANAGER_H
#define RESTACCESSMANAGER_H

#include "contactentry.h"

#include <QMap>
#include <QMutex>
#include <QNetworkAccessManager>
#include <QObject>
#include <QString>

#include <optional>

class RestAccessManager : public QObject
{
    Q_OBJECT
    struct AuthHeader
    {
        QString key;
        QString value;
    };

public:
    using ContactsMap = QMap<qint64, ContactEntry>;

    explicit RestAccessManager(const QString &host, quint16 port);
    ~RestAccessManager();

    void setAuthorizationHeader(const QString &key, const QString &value);
    ContactsMap getContacts() const;

    void addContact(const ContactEntry &entry);
    void updateContact(const ContactEntry &entry);
    void deleteContact(qint64 id);

signals:
    void contactsChanged();

private slots:
    void readContacts(QNetworkReply *reply);

private:
    void updateContacts();

    const QString host;
    const quint16 port;
    std::optional<AuthHeader> authHeader;
    QNetworkAccessManager manager;
    mutable QMutex contactsMtx;
    ContactsMap contacts;
};

#endif // RESTACCESSMANAGER_H