File indexing completed on 2025-01-05 05:01:15
0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0002 // SPDX-FileCopyrightText: 2023 Harald Sitter <sitter@kde.org> 0003 0004 #pragma once 0005 0006 #include <memory> 0007 0008 #include <QNetworkAccessManager> 0009 #include <QNetworkReply> 0010 #include <QObject> 0011 0012 // Represents a NetworkReply. Implemented for QNetwork, and testing mocks. 0013 class SentryReply : public QObject 0014 { 0015 Q_OBJECT 0016 public: 0017 virtual QByteArray readAll() = 0; 0018 virtual QNetworkReply::NetworkError error() = 0; 0019 virtual QString errorString() = 0; 0020 0021 Q_SIGNALS: 0022 void finished(); 0023 0024 protected: 0025 using QObject::QObject; 0026 }; 0027 0028 // Represents a NetworkAccessManager. Implemented for QNetwork, and testing mocks. 0029 class SentryConnection : public QObject 0030 { 0031 Q_OBJECT 0032 public: 0033 using QObject::QObject; 0034 virtual SentryReply *get(const QNetworkRequest &request) = 0; 0035 virtual SentryReply *post(const QNetworkRequest &request, const QByteArray &data) = 0; 0036 }; 0037 0038 // A QNetworkReply based reply implementation. 0039 class SentryNetworkReply final : public SentryReply 0040 { 0041 public: 0042 explicit SentryNetworkReply(QNetworkReply *reply, QObject *parent = nullptr); 0043 ~SentryNetworkReply() final; 0044 Q_DISABLE_COPY_MOVE(SentryNetworkReply) 0045 0046 QByteArray readAll() final; 0047 QNetworkReply::NetworkError error() final; 0048 QString errorString() final; 0049 0050 private: 0051 QNetworkReply *m_reply; 0052 }; 0053 0054 // A QNetworkAccessManager based connection implementation. 0055 class SentryNetworkConnection final : public SentryConnection 0056 { 0057 public: 0058 explicit SentryNetworkConnection(QObject *parent = nullptr); 0059 0060 SentryReply *get(const QNetworkRequest &request) final; 0061 SentryReply *post(const QNetworkRequest &request, const QByteArray &data) final; 0062 0063 private: 0064 QNetworkAccessManager m_manager; 0065 };