blob: 5f765b386ed7b82a844ebe44bbed02ec912c152f (
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
57
58
59
60
61
62
63
|
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "abstractresource.h"
#include "restservice.h"
#if QT_CONFIG(ssl)
#include <QtNetwork/qsslsocket.h>
#endif
#include <QtNetwork/qrestaccessmanager.h>
using namespace Qt::StringLiterals;
RestService::RestService(QObject *parent) : QObject(parent)
{
m_qnam.setAutoDeleteReplies(true);
m_manager = std::make_shared<QRestAccessManager>(&m_qnam);
m_serviceApi = std::make_shared<QNetworkRequestFactory>();
}
void RestService::setUrl(const QUrl &url)
{
if (m_serviceApi->baseUrl() == url)
return;
m_serviceApi->setBaseUrl(url);
QHttpHeaders authenticationHeaders;
// reqres.in requires an API-key, see https://reqres.in/signup
if (url.host().startsWith("reqres"_L1))
authenticationHeaders.append("x-api-key", "reqres-free-v1");
m_serviceApi->setCommonHeaders(authenticationHeaders);
emit urlChanged();
}
QUrl RestService::url() const
{
return m_serviceApi->baseUrl();
}
bool RestService::sslSupported()
{
#if QT_CONFIG(ssl)
return QSslSocket::supportsSsl();
#else
return false;
#endif
}
QQmlListProperty<AbstractResource> RestService::resources()
{
return {this, &m_resources};
}
void RestService::classBegin()
{
}
void RestService::componentComplete()
{
for (const auto resource : std::as_const(m_resources)) {
resource->setAccessManager(m_manager);
resource->setServiceApi(m_serviceApi);
}
}
|