This repository was archived by the owner on May 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackendlessAPI.cpp
More file actions
156 lines (138 loc) · 3.79 KB
/
Copy pathBackendlessAPI.cpp
File metadata and controls
156 lines (138 loc) · 3.79 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// BackendlessAPI.cpp
// BackendlessQt
//
// Created by Roman Podymov on 07/08/2024.
// Copyright © 2024 BackendlessQt. All rights reserved.
//
#include <QNetworkAccessManager>
#include <QUrlQuery>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include "BackendlessAPI.hpp"
class DeletionResultCoder: public Coder {
Codable* decode(QJsonObject obj) override {
return new DeletionResult(obj);
}
void write(QTextStream&, QSharedPointer<Codable>, QSharedPointer<Codable>) override {
}
Codable* read(QTextStream&) override {
return nullptr;
}
};
BackendlessAPI::BackendlessAPI(
AnyNetworkAccessManager* _networkAccessManager,
QSharedPointer<BackendlessSignInUserCoder> _coder,
QString _appId,
QString _apiKey,
QString _endpoint
): QObject(),
userAPI(
_networkAccessManager,
_coder,
_appId,
_apiKey,
_endpoint
),
networkAccessManager(_networkAccessManager),
appId(_appId),
apiKey(_apiKey),
endpoint(_endpoint) {
}
void BackendlessAPI::addItemToTable(QString tableName, PostParams params) {
request(
networkAccessManager,
this,
endpoint + appId + "/" + apiKey + "/data/" + tableName,
params,
BERequestMethod::post,
{},
[&](auto replyValue){
qDebug() << replyValue;
emit itemAdded();
}
);
}
void BackendlessAPI::editTableItem(QString tableName, QString whereClause, PostParams params) {
auto requestURL = endpoint + appId + "/" + apiKey + "/data/" + tableName;
if (!whereClause.isEmpty()) {
requestURL += "?where=" + whereClause;
}
request(
networkAccessManager,
this,
requestURL,
params,
BERequestMethod::put,
{},
[&](auto replyValue){
qDebug() << replyValue;
emit itemEdited();
}
);
}
void BackendlessAPI::deleteItemFromTable(QString tableName, QString objectId) {
request(
networkAccessManager,
this,
endpoint + appId + "/" + apiKey + "/data/" + tableName + "/" + objectId,
{
},
BERequestMethod::deleteResource,
{},
[&](auto replyValue){
qDebug() << replyValue;
extractResult<DeletionResult>(
replyValue,
new DeletionResultCoder(),
[&](auto result) {
emit deleteItemFromTableSuccess(result);
},
[&](auto beError) {
emit deleteItemFromTableError(beError);
},
[&]([[maybe_unused]] auto jsonError) {
}
);
}
);
}
void BackendlessAPI::loadTableItems(QString tableName, int pageSize, int offset, QString whereClause) {
auto requestURL = endpoint + appId + "/" + apiKey + "/data/" + tableName + "?pageSize=" + QString::number(pageSize) + "&offset=" + QString::number(offset);
if (!whereClause.isEmpty()) {
requestURL += "&where=" + whereClause;
}
request(
networkAccessManager,
this,
requestURL,
{
},
BERequestMethod::get,
{},
[&](auto replyValue){
qDebug() << replyValue;
#ifdef BACKENDLESS_VARIANT_RESPONSE
#else
emit loadTableItemsSuccess(replyValue);
#endif
}
);
}
void BackendlessAPI::getItemsCount(QString tableName) {
request(
networkAccessManager,
this,
endpoint + appId + "/" + apiKey + "/data/" + tableName + "/count",
{
},
BERequestMethod::get,
{},
[&](auto replyValue){
qDebug() << replyValue;
emit getItemsCountSuccess(replyValue.toInt());
}
);
}