Changeset View
Changeset View
Standalone View
Standalone View
src/bugzillaintegration/libbugzilla/connection.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2019 Harald Sitter <sitter@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | later version accepted by the membership of KDE e.V. (or its | ||||
9 | successor approved by the membership of KDE e.V.), which shall | ||||
10 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | | ||||
12 | This library is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | Lesser General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU Lesser General Public | ||||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "connection.h" | ||||
22 | | ||||
23 | #include <QDebug> | ||||
24 | #include <QUrlQuery> | ||||
25 | | ||||
26 | #include <KIOCore/KIO/TransferJob> | ||||
27 | | ||||
28 | #include "bugzilla_debug.h" | ||||
29 | | ||||
30 | namespace Bugzilla { | ||||
31 | | ||||
32 | // Static container for global default connection. | ||||
33 | // We need a container here because the connection may be anything derived from | ||||
34 | // Connection and its effective type may change (e.g. in autotests). | ||||
35 | class GlobalConnection | ||||
36 | { | ||||
37 | public: | ||||
38 | ~GlobalConnection() | ||||
39 | { | ||||
40 | delete m_connection; | ||||
41 | } | ||||
42 | | ||||
43 | Connection *m_connection = new HTTPConnection; | ||||
44 | }; | ||||
45 | | ||||
46 | Q_GLOBAL_STATIC(GlobalConnection, s_connection) | ||||
47 | | ||||
48 | Connection &connection() | ||||
49 | { | ||||
50 | return *(s_connection->m_connection); | ||||
51 | } | ||||
52 | | ||||
53 | void setConnection(Connection *newConnection) | ||||
54 | { | ||||
55 | delete s_connection->m_connection; | ||||
56 | s_connection->m_connection = newConnection; | ||||
57 | } | ||||
58 | | ||||
59 | HTTPConnection::HTTPConnection(const QUrl &root, QObject *parent) | ||||
60 | : Connection(parent) | ||||
61 | , m_root(root) | ||||
62 | { | ||||
63 | } | ||||
64 | | ||||
65 | HTTPConnection::~HTTPConnection() | ||||
66 | { | ||||
67 | } | ||||
68 | | ||||
69 | void HTTPConnection::setToken(const QString &authToken) | ||||
70 | { | ||||
71 | m_token = authToken; | ||||
72 | } | ||||
73 | | ||||
74 | APIJob *HTTPConnection::get(const QString &path, const QUrlQuery &query) const | ||||
75 | { | ||||
76 | qCDebug(BUGZILLA_LOG) << path << query.toString(); | ||||
77 | auto job = new TransferAPIJob(KIO::get(url(path, query), KIO::Reload, KIO::HideProgressInfo)); | ||||
78 | return job; | ||||
79 | } | ||||
80 | | ||||
81 | APIJob *HTTPConnection::post(const QString &path, const QByteArray &data, const QUrlQuery &query) const | ||||
82 | { | ||||
83 | qCDebug(BUGZILLA_LOG) << path << query.toString(); | ||||
84 | auto job = new TransferAPIJob(KIO::http_post(url(path, query), data, KIO::HideProgressInfo)); | ||||
85 | return job; | ||||
86 | } | ||||
87 | | ||||
88 | APIJob *HTTPConnection::put(const QString &path, const QByteArray &data, const QUrlQuery &query) const | ||||
89 | { | ||||
90 | qCDebug(BUGZILLA_LOG) << path << query.toString(); | ||||
91 | auto job = new TransferAPIJob(KIO::put(url(path, query), KIO::HideProgressInfo)); | ||||
92 | job->setPutData(data); | ||||
93 | return job; | ||||
94 | } | ||||
95 | | ||||
96 | QUrl HTTPConnection::url(const QString &appendix, QUrlQuery query) const | ||||
97 | { | ||||
98 | QUrl url(m_root); | ||||
99 | url.setPath(m_root.path() + appendix); | ||||
100 | | ||||
101 | if (!m_token.isEmpty()) { | ||||
102 | query.addQueryItem(QStringLiteral("token"), m_token); | ||||
103 | } | ||||
104 | | ||||
105 | url.setQuery(query); | ||||
106 | return url; | ||||
107 | } | ||||
108 | | ||||
109 | } // namespace Bugzilla | ||||
110 | |