Skip to content

Commit 21ce56f

Browse files
authored
feat: Add context in ParseObject (#970)
1 parent 088bfbc commit 21ce56f

File tree

7 files changed

+39
-16
lines changed

7 files changed

+39
-16
lines changed

packages/dart/CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## [6.1.0](https://github.com/parse-community/Parse-SDK-Flutter/compare/dart-6.0.0...dart-6.1.0) (2023-10-17)
2+
3+
### Features
4+
5+
* Add `context` in `ParseObject` ([#970](https://github.com/parse-community/Parse-SDK-Flutter/pull/970))
6+
17
## [6.0.0](https://github.com/parse-community/Parse-SDK-Flutter/compare/dart-5.1.3...dart-6.0.0) (2023-10-16)
28

39
### BREAKING CHANGES
@@ -7,7 +13,7 @@
713
### Features
814

915
* Add support for Dart 3.1, remove support for Dart 2.18 ([#969](https://github.com/parse-community/Parse-SDK-Flutter/pull/969))
10-
16+
1117
## [5.1.3](https://github.com/parse-community/Parse-SDK-Flutter/compare/dart-5.1.2...dart-5.1.3) (2023-07-18)
1218

1319
### Bug Fixes

packages/dart/lib/src/base/parse_constants.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
part of flutter_parse_sdk;
22

33
// Library
4-
const String keySdkVersion = '6.0.0';
4+
const String keySdkVersion = '6.1.0';
55
const String keyLibraryName = 'Flutter Parse SDK';
66

77
// End Points
@@ -44,6 +44,7 @@ const String keyFileClassname = 'ParseFile';
4444
// Headers
4545
const String keyHeaderSessionToken = 'X-Parse-Session-Token';
4646
const String keyHeaderRevocableSession = 'X-Parse-Revocable-Session';
47+
const String keyHeaderCloudContext = 'X-Parse-Cloud-Context';
4748
const String keyHeaderUserAgent = 'user-agent';
4849
const String keyHeaderApplicationId = 'X-Parse-Application-Id';
4950
const String keyHeaderContentType = 'content-type';

packages/dart/lib/src/objects/parse_file_base.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ abstract class ParseFileBase extends ParseObject {
4040

4141
/// Uploads a file to Parse Server
4242
@override
43-
Future<ParseResponse> save() async {
43+
Future<ParseResponse> save({dynamic context}) async {
4444
return upload();
4545
}
4646

packages/dart/lib/src/objects/parse_installation.dart

+3-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ class ParseInstallation extends ParseObject {
113113
}
114114

115115
@override
116-
Future<ParseResponse> create({bool allowCustomObjectId = false}) async {
116+
Future<ParseResponse> create(
117+
{bool allowCustomObjectId = false, dynamic context}) async {
117118
final bool isCurrent = await ParseInstallation.isCurrent(this);
118119
if (isCurrent) {
119120
await _updateInstallation();
@@ -130,7 +131,7 @@ class ParseInstallation extends ParseObject {
130131

131132
/// Saves the current installation
132133
@override
133-
Future<ParseResponse> save() async {
134+
Future<ParseResponse> save({dynamic context}) async {
134135
final bool isCurrent = await ParseInstallation.isCurrent(this);
135136
if (isCurrent) {
136137
await _updateInstallation();

packages/dart/lib/src/objects/parse_object.dart

+23-8
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ class ParseObject extends ParseBase implements ParseCloneable {
8686
/// Creates a new object and saves it online
8787
///
8888
/// Prefer using [save] over [create]
89-
Future<ParseResponse> create({bool allowCustomObjectId = false}) async {
89+
Future<ParseResponse> create(
90+
{bool allowCustomObjectId = false, dynamic context}) async {
9091
try {
9192
final Uri url = getSanitisedUri(_client, _path);
9293
final String body = json.encode(toJson(
@@ -96,8 +97,17 @@ class ParseObject extends ParseBase implements ParseCloneable {
9697

9798
_saveChanges();
9899

99-
final ParseNetworkResponse result =
100-
await _client.post(url.toString(), data: body);
100+
final Map<String, String> headers = {
101+
keyHeaderContentType: keyHeaderContentTypeJson,
102+
};
103+
104+
if (context != null) {
105+
headers
106+
.addAll({keyHeaderCloudContext: json.encode(parseEncode(context))});
107+
}
108+
109+
final ParseNetworkResponse result = await _client.post(url.toString(),
110+
data: body, options: ParseNetworkOptions(headers: headers));
101111

102112
final response = handleResponse<ParseObject>(
103113
this, result, ParseApiRQ.create, _debug, parseClassName);
@@ -120,7 +130,7 @@ class ParseObject extends ParseBase implements ParseCloneable {
120130
/// The object should hold an [objectId] in order to update it
121131
///
122132
/// Prefer using [save] over [update]
123-
Future<ParseResponse> update() async {
133+
Future<ParseResponse> update({dynamic context}) async {
124134
assert(
125135
objectId != null && (objectId?.isNotEmpty ?? false),
126136
"Can't update a parse object while the objectId property is null or empty",
@@ -133,9 +143,14 @@ class ParseObject extends ParseBase implements ParseCloneable {
133143
_saveChanges();
134144

135145
final Map<String, String> headers = {
136-
keyHeaderContentType: keyHeaderContentTypeJson
146+
keyHeaderContentType: keyHeaderContentTypeJson,
137147
};
138148

149+
if (context != null) {
150+
headers
151+
.addAll({keyHeaderCloudContext: json.encode(parseEncode(context))});
152+
}
153+
139154
final ParseNetworkResponse result = await _client.put(url.toString(),
140155
data: body, options: ParseNetworkOptions(headers: headers));
141156

@@ -185,14 +200,14 @@ class ParseObject extends ParseBase implements ParseCloneable {
185200
/// Its safe to call this function aging if an error occurred while saving.
186201
///
187202
/// Prefer using [save] over [update] and [create]
188-
Future<ParseResponse> save() async {
203+
Future<ParseResponse> save({dynamic context}) async {
189204
final ParseResponse childrenResponse = await _saveChildren(this);
190205
if (childrenResponse.success) {
191206
ParseResponse? response;
192207
if (objectId == null) {
193-
response = await create();
208+
response = await create(context: context);
194209
} else if (_isDirty(false)) {
195-
response = await update();
210+
response = await update(context: context);
196211
}
197212

198213
if (response != null) {

packages/dart/lib/src/objects/parse_user.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
393393
/// If changes are made to the current user, call save to sync them with
394394
/// Parse Server
395395
@override
396-
Future<ParseResponse> save() async {
396+
Future<ParseResponse> save({dynamic context}) async {
397397
if (objectId == null) {
398398
return await signUp();
399399
} else {
@@ -406,7 +406,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
406406
}
407407

408408
@override
409-
Future<ParseResponse> update() async {
409+
Future<ParseResponse> update({dynamic context}) async {
410410
if (objectId == null) {
411411
return await signUp();
412412
} else {

packages/dart/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: parse_server_sdk
22
description: The Dart SDK to connect to Parse Server. Build your apps faster with Parse Platform, the complete application stack.
3-
version: 6.0.0
3+
version: 6.1.0
44
homepage: https://parseplatform.org
55
repository: https://github.com/parse-community/Parse-SDK-Flutter
66
issue_tracker: https://github.com/parse-community/Parse-SDK-Flutter/issues

0 commit comments

Comments
 (0)