This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.dart
215 lines (184 loc) · 6.81 KB
/
base.dart
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import 'dart:convert';
import 'package:http/http.dart' as http;
class DetaBase {
late final String _projectKey;
late final String _projectId;
static final DetaBase _instance = DetaBase._internal();
static DetaBase get instance => _instance;
DetaBase._internal();
factory DetaBase() => _instance;
init({required String projectId, required String projectKey}) {
_projectId = projectId;
_projectKey = projectKey;
}
Map<Type, Function>? _factories;
// has to be called if generic calls shall be used
initGenericFactories(Map<Type, Function> factories) {
_factories = factories;
}
checkForCreds() {
// ignore: unnecessary_null_comparison
if (_projectId == null && _projectKey == null) {
throw ArgumentError(
"ProjectID and ProjectKey must not be null! Did you call DetaBase.init(id, key)?");
}
}
get _baseUrl => "https://database.deta.sh/v1/$_projectId";
get _defaultHeaders =>
{"X-API-Key": _projectKey, "Content-Type": "application/json"};
Future<dynamic> addItem(String path, Object body) async {
checkForCreds();
return jsonDecode((await http.post(Uri.parse(_baseUrl + path),
body: jsonEncode({"items": body}), headers: _defaultHeaders))
.body);
}
Future<dynamic> addMultipleItems(String path, List<Object> body) async {
checkForCreds();
return jsonDecode((await http.post(Uri.parse(_baseUrl + path),
body: jsonEncode({"items": body}), headers: _defaultHeaders))
.body);
}
Future<dynamic> updateItem(String path, Object body) async {
checkForCreds();
return jsonDecode((await http.patch(Uri.parse(_baseUrl + path),
body: jsonEncode(body), headers: _defaultHeaders))
.body);
}
Future<dynamic> getItem(String url) async {
checkForCreds();
return json.decode(
(await http.get(Uri.parse(_baseUrl + url), headers: _defaultHeaders))
.body);
}
Future<dynamic> queryItem(String url, List<Object> queries) async {
checkForCreds();
return json.decode((await http.post(Uri.parse(_baseUrl + url),
body: jsonEncode({"query": queries}), headers: _defaultHeaders))
.body);
}
Future<dynamic> deleteItem(String path) async {
checkForCreds();
return json.decode((await http.delete(Uri.parse(_baseUrl + path),
headers: _defaultHeaders))
.body);
}
// region generic calls
checkFactories() {
if (_factories == null) {
throw ArgumentError.notNull(
"You have to initialize factories via initGenericFactories if you want to use generic methods!");
}
}
Future<T> addGenericItem<T>(String path, Object body) async {
checkForCreds();
checkFactories();
final resp = jsonDecode((await http.post(Uri.parse(_baseUrl + path),
body: jsonEncode({"items": body}), headers: _defaultHeaders))
.body);
return _factories![T]!(resp);
}
Future<T> addMultipleGenericItems<T>(String path, List<Object> body) async {
checkForCreds();
checkFactories();
final resp = jsonDecode((await http.post(Uri.parse(_baseUrl + path),
body: jsonEncode({"items": body}), headers: _defaultHeaders))
.body);
return _factories![T]!(resp);
}
Future<T> updateGenericItem<T>(String path, Object body) async {
checkForCreds();
checkFactories();
final resp = jsonDecode((await http.patch(Uri.parse(_baseUrl + path),
body: jsonEncode(body), headers: _defaultHeaders))
.body);
return _factories![T]!(resp);
}
Future<T> getGenericItem<T>(String url) async {
checkForCreds();
checkFactories();
final resp = json.decode(
(await http.get(Uri.parse(_baseUrl + url), headers: _defaultHeaders))
.body);
return _factories![T]!(resp);
}
Future<T?> querySingleGenericItem<T>(String url, List<Object> queries) async {
checkForCreds();
checkFactories();
final resp = json.decode((await http.post(Uri.parse(_baseUrl + url),
body: jsonEncode({"query": queries, "limit": 1}),
headers: _defaultHeaders))
.body);
return resp["items"].length == 0 ? null : _factories![T]!(resp["items"[0]]);
}
Future<List<T>> queryGenericItems<T>(String url, List<Object> queries) async {
checkForCreds();
checkFactories();
final resp = json.decode((await http.post(Uri.parse(_baseUrl + url),
body: jsonEncode({"query": queries}), headers: _defaultHeaders))
.body)["items"];
return _factories![T]!(resp);
}
// endregion
// region raw calls
Future<dynamic> addRawItem(String path, Object body) async {
checkForCreds();
return jsonDecode((await http.post(Uri.parse(_baseUrl + path),
body: jsonEncode(body), headers: _defaultHeaders))
.body);
}
Future<dynamic> addMultipleRawItems(String path, Object body) async {
checkForCreds();
return jsonDecode((await http.post(Uri.parse(_baseUrl + path),
body: jsonEncode(body), headers: _defaultHeaders))
.body);
}
Future<dynamic> queryRaw(String url, Object query) async {
checkForCreds();
return json.decode((await http.post(Uri.parse(_baseUrl + url),
body: jsonEncode(query), headers: _defaultHeaders))
.body);
}
// endregion
// region safe calls
Future<dynamic> addItemSafe(String base, Object body) async {
checkForCreds();
return jsonDecode((await http.post(Uri.parse("$_baseUrl/$base/items"),
body: jsonEncode({"items": body}), headers: _defaultHeaders))
.body);
}
Future<dynamic> addMultipleItemsSafe(String base, List<Object> body) async {
checkForCreds();
return jsonDecode((await http.post(Uri.parse("$_baseUrl/$base/items"),
body: jsonEncode({"items": body}), headers: _defaultHeaders))
.body);
}
Future<dynamic> updateItemSafe(
String base, String toUpdateKey, Object body) async {
checkForCreds();
return jsonDecode((await http.patch(
Uri.parse("$_baseUrl/$base/items/$toUpdateKey"),
body: jsonEncode(body),
headers: _defaultHeaders))
.body);
}
Future<dynamic> getItemSafe(String base, String key) async {
checkForCreds();
return json.decode((await http.get(Uri.parse("$_baseUrl/$base/items/$key"),
headers: _defaultHeaders))
.body);
}
Future<dynamic> queryItemSafe(String base, List<Object> queries) async {
checkForCreds();
return json.decode((await http.post(Uri.parse("$_baseUrl/$base/query"),
body: jsonEncode({"query": queries}), headers: _defaultHeaders))
.body);
}
Future<dynamic> deleteItemSafe(String base, String toDeleteKey) async {
checkForCreds();
return json.decode((await http.delete(
Uri.parse("$_baseUrl/$base/items/$toDeleteKey"),
headers: _defaultHeaders))
.body);
}
// endregion
}