-
Notifications
You must be signed in to change notification settings - Fork 103
/
object_reference.dart
163 lines (142 loc) · 5.23 KB
/
object_reference.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
part of algolia;
class AlgoliaObjectReference {
AlgoliaObjectReference._(this.algolia, String? index, String? objectId)
: _index = index,
_objectId = objectId;
final Algolia algolia;
final String? _index;
final String? _objectId;
String? get index => _index;
String? get objectID => _objectId;
String? get encodedObjectID =>
_objectId != null ? Uri.encodeFull(_objectId!) : null;
String? get encodedIndex => _index != null ? Uri.encodeFull(_index!) : null;
/// Get the object referred to by this [AlgoliaObjectReference].
///
/// If the object does not yet exist, it will be created.
Future<AlgoliaObjectSnapshot> getObject() async {
assert(_index != null, 'You can\'t get an object without an index.');
assert(_objectId != null, 'You can\'t get an object without an objectID.');
var response = await algolia._apiCall(
ApiRequestType.get,
'indexes/$encodedIndex/$encodedObjectID',
);
Map<String, dynamic> body = json.decode(response.body);
if (!(response.statusCode >= 200 && response.statusCode < 300)) {
throw AlgoliaError._(body, response.statusCode);
}
return AlgoliaObjectSnapshot._(algolia, _index!, body);
}
/// Writes to the object referred to by this [AlgoliaObjectReference].
///
/// If the object does not yet exist, it will be created.
Future<AlgoliaTask> setData(Map<String, dynamic> data) async {
assert(_index != null && _index != '*' && _index != '',
'IndexName is required, but it has `*` multiple flag or `null`.');
var url = 'indexes/$encodedIndex';
if (_objectId != null) {
url += '/$encodedObjectID';
}
if (data['objectID'] != null && _objectId == null) {
url += "/${Uri.encodeFull(data['objectID'])}";
} else if (data['objectID'] != null) {
data.remove('objectID');
}
var response = await algolia._apiCall(
ApiRequestType.put,
url,
data: data,
);
Map<String, dynamic> body = json.decode(response.body);
if (!(response.statusCode >= 200 && response.statusCode < 300)) {
throw AlgoliaError._(body, response.statusCode);
}
return AlgoliaTask._(algolia, _index, body);
}
///
/// **UpdateData**
///
/// Updates fields in the object referred to by this [AlgoliaObjectReference].
///
/// Values in [data] may be of any supported Algolia type.
///
/// If no object exists yet, the update will fail.
Future<AlgoliaTask> updateData(Map<String, dynamic> data) async {
assert(_index != null && _index != '*' && _index != '',
'IndexName is required, but it has `*` multiple flag or `null`.');
var url = 'indexes/$encodedIndex';
if (_objectId != null) {
url = '$url/$encodedObjectID';
}
data['objectID'] = _objectId;
var response = await algolia._apiCall(
ApiRequestType.put,
url,
data: data,
);
Map<String, dynamic> body = json.decode(response.body);
if (!(response.statusCode >= 200 && response.statusCode < 300)) {
throw AlgoliaError._(body, response.statusCode);
}
return AlgoliaTask._(algolia, _index, body);
}
///
/// **PartialUpdateObject**
///
/// Updates fields in the object referred to by this [AlgoliaObjectReference].
///
/// Values in [data] may be of any supported Algolia type.
///
/// If no object exists yet, the update will fail.
///
/// ---
///
/// **Usage**
/// - `createIfNotExists`: When true, a partial update on a nonexistent object will create the
/// object, assuming an empty object as the basis. When false, a partial
/// update on a nonexistent object will be ignored.
///
Future<AlgoliaTask> partialUpdateObject(Map<String, dynamic> data,
{bool createIfNotExists = true}) async {
assert(_objectId != null || createIfNotExists,
'You can\'t partialUpdateObject when createIfNotExists=false and data without an objectID.');
assert(_index != null && _index != '*' && _index != '',
'IndexName is required, but it has `*` multiple flag or `null`.');
var url = 'indexes/$encodedIndex';
if (_objectId != null) {
url = '$url/$encodedObjectID/partial';
}
data['objectID'] = _objectId;
data['createIfNotExists'] = createIfNotExists;
var response = await algolia._apiCall(
ApiRequestType.post,
url,
data: data,
);
Map<String, dynamic> body = json.decode(response.body);
if (!(response.statusCode >= 200 && response.statusCode < 300)) {
throw AlgoliaError._(body, response.statusCode);
}
return AlgoliaTask._(algolia, _index, body);
}
/// Delete the object referred to by this [AlgoliaObjectReference].
///
/// If no object exists yet, the update will fail.
Future<AlgoliaTask> deleteObject() async {
assert(
_objectId != null, 'You can\'t delete an object without an objectID.');
var url = 'indexes/$encodedIndex';
if (_objectId != null) {
url = '$url/$encodedObjectID';
}
var response = await algolia._apiCall(
ApiRequestType.delete,
url,
);
Map<String, dynamic> body = json.decode(response.body);
if (!(response.statusCode >= 200 && response.statusCode < 300)) {
throw AlgoliaError._(body, response.statusCode);
}
return AlgoliaTask._(algolia, _index, body);
}
}