-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackendlessAPI.cpp
110 lines (97 loc) · 2.8 KB
/
BackendlessAPI.cpp
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
//
// 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"
BackendlessAPI::BackendlessAPI(AnyNetworkAccessManager* _networkAccessManager, QString _appId, QString _apiKey, QString _endpoint): QObject(),
networkAccessManager(_networkAccessManager),
userAPI(_networkAccessManager, _appId, _apiKey, _endpoint),
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::deleteItemFromTable(QString tableName, QString objectId) {
request(
networkAccessManager,
this,
endpoint + appId + "/" + apiKey + "/data/" + tableName + "/" + objectId,
{
},
BERequestMethod::deleteResource,
{},
[&](auto replyValue){
qDebug() << replyValue;
extractResult<DeletionResult>(
replyValue,
[&](auto result) {
emit deleteItemFromTableSuccess(result);
},
[&](auto beError) {
emit deleteItemFromTableError(beError);
},
[&](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());
}
);
}