blob: 916be0cc9916ae8d7bd86dd920a564aa3b27627c (
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
#pragma once
#include <QJsonDocument>
#include <QUrlQuery>
#include "network/httprequestmethod.h"
#include "network/webtask.h"
namespace mixxx {
namespace network {
struct JsonWebRequest final {
public:
JsonWebRequest() = delete;
JsonWebRequest(const JsonWebRequest&) = default;
JsonWebRequest(JsonWebRequest&&) = default;
JsonWebRequest& operator=(const JsonWebRequest&) = default;
JsonWebRequest& operator=(JsonWebRequest&&) = default;
HttpRequestMethod method;
QString path;
QUrlQuery query;
QJsonDocument content;
};
class JsonWebResponse final {
public:
static void registerMetaType();
JsonWebResponse() = default;
JsonWebResponse(
WebResponse&& response,
QJsonDocument&& content)
: m_response(response),
m_content(content) {
}
JsonWebResponse(const JsonWebResponse&) = default;
JsonWebResponse(JsonWebResponse&&) = default;
JsonWebResponse& operator=(const JsonWebResponse&) = default;
JsonWebResponse& operator=(JsonWebResponse&&) = default;
bool isStatusCodeSuccess() const {
return m_response.isStatusCodeSuccess();
}
HttpStatusCode statusCode() const {
return m_response.statusCode();
}
const QUrl& replyUrl() const {
return m_response.replyUrl();
}
const QJsonDocument& content() const {
return m_content;
}
friend QDebug operator<<(QDebug dbg, const JsonWebResponse& arg);
private:
WebResponse m_response;
QJsonDocument m_content;
};
class JsonWebTask : public WebTask {
Q_OBJECT
public:
JsonWebTask(
QNetworkAccessManager* networkAccessManager,
QUrl baseUrl,
JsonWebRequest&& request,
QObject* parent = nullptr);
~JsonWebTask() override = default;
signals:
void failed(
const network::JsonWebResponse& response);
protected:
// Customizable in derived classes
virtual QNetworkReply* sendNetworkRequest(
QNetworkAccessManager* networkAccessManager,
HttpRequestMethod method,
const QUrl& url,
const QJsonDocument& content);
void emitFailed(
const network::JsonWebResponse& response);
private:
/// Handle the response and ensure that the task eventually
/// gets deleted.
///
/// Could be overridden by derived classes. The default
/// implementation discards the response and deletes the task.
virtual void onFinished(
const JsonWebResponse& jsonResponse);
virtual void onFinishedCustom(
const WebResponseWithContent& customResponse);
QNetworkReply* doStartNetworkRequest(
QNetworkAccessManager* networkAccessManager,
int parentTimeoutMillis) override;
void doNetworkReplyFinished(
QNetworkReply* finishedNetworkReply,
HttpStatusCode statusCode) override;
// All member variables must only be accessed from
// the event loop thread!!
const QUrl m_baseUrl;
const JsonWebRequest m_request;
};
} // namespace network
} // namespace mixxx
Q_DECLARE_METATYPE(mixxx::network::JsonWebResponse);
|