Skip to content

Commit

Permalink
feat: Add context in ParseObject (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfakourii authored Oct 16, 2023
1 parent 088bfbc commit 21ce56f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 16 deletions.
8 changes: 7 additions & 1 deletion packages/dart/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [6.1.0](https://github.com/parse-community/Parse-SDK-Flutter/compare/dart-6.0.0...dart-6.1.0) (2023-10-17)

### Features

* Add `context` in `ParseObject` ([#970](https://github.com/parse-community/Parse-SDK-Flutter/pull/970))

## [6.0.0](https://github.com/parse-community/Parse-SDK-Flutter/compare/dart-5.1.3...dart-6.0.0) (2023-10-16)

### BREAKING CHANGES
Expand All @@ -7,7 +13,7 @@
### Features

* Add support for Dart 3.1, remove support for Dart 2.18 ([#969](https://github.com/parse-community/Parse-SDK-Flutter/pull/969))

## [5.1.3](https://github.com/parse-community/Parse-SDK-Flutter/compare/dart-5.1.2...dart-5.1.3) (2023-07-18)

### Bug Fixes
Expand Down
3 changes: 2 additions & 1 deletion packages/dart/lib/src/base/parse_constants.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
part of flutter_parse_sdk;

// Library
const String keySdkVersion = '6.0.0';
const String keySdkVersion = '6.1.0';
const String keyLibraryName = 'Flutter Parse SDK';

// End Points
Expand Down Expand Up @@ -44,6 +44,7 @@ const String keyFileClassname = 'ParseFile';
// Headers
const String keyHeaderSessionToken = 'X-Parse-Session-Token';
const String keyHeaderRevocableSession = 'X-Parse-Revocable-Session';
const String keyHeaderCloudContext = 'X-Parse-Cloud-Context';
const String keyHeaderUserAgent = 'user-agent';
const String keyHeaderApplicationId = 'X-Parse-Application-Id';
const String keyHeaderContentType = 'content-type';
Expand Down
2 changes: 1 addition & 1 deletion packages/dart/lib/src/objects/parse_file_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ abstract class ParseFileBase extends ParseObject {

/// Uploads a file to Parse Server
@override
Future<ParseResponse> save() async {
Future<ParseResponse> save({dynamic context}) async {
return upload();
}

Expand Down
5 changes: 3 additions & 2 deletions packages/dart/lib/src/objects/parse_installation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ class ParseInstallation extends ParseObject {
}

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

/// Saves the current installation
@override
Future<ParseResponse> save() async {
Future<ParseResponse> save({dynamic context}) async {
final bool isCurrent = await ParseInstallation.isCurrent(this);
if (isCurrent) {
await _updateInstallation();
Expand Down
31 changes: 23 additions & 8 deletions packages/dart/lib/src/objects/parse_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ class ParseObject extends ParseBase implements ParseCloneable {
/// Creates a new object and saves it online
///
/// Prefer using [save] over [create]
Future<ParseResponse> create({bool allowCustomObjectId = false}) async {
Future<ParseResponse> create(
{bool allowCustomObjectId = false, dynamic context}) async {
try {
final Uri url = getSanitisedUri(_client, _path);
final String body = json.encode(toJson(
Expand All @@ -96,8 +97,17 @@ class ParseObject extends ParseBase implements ParseCloneable {

_saveChanges();

final ParseNetworkResponse result =
await _client.post(url.toString(), data: body);
final Map<String, String> headers = {
keyHeaderContentType: keyHeaderContentTypeJson,
};

if (context != null) {
headers
.addAll({keyHeaderCloudContext: json.encode(parseEncode(context))});
}

final ParseNetworkResponse result = await _client.post(url.toString(),
data: body, options: ParseNetworkOptions(headers: headers));

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

final Map<String, String> headers = {
keyHeaderContentType: keyHeaderContentTypeJson
keyHeaderContentType: keyHeaderContentTypeJson,
};

if (context != null) {
headers
.addAll({keyHeaderCloudContext: json.encode(parseEncode(context))});
}

final ParseNetworkResponse result = await _client.put(url.toString(),
data: body, options: ParseNetworkOptions(headers: headers));

Expand Down Expand Up @@ -185,14 +200,14 @@ class ParseObject extends ParseBase implements ParseCloneable {
/// Its safe to call this function aging if an error occurred while saving.
///
/// Prefer using [save] over [update] and [create]
Future<ParseResponse> save() async {
Future<ParseResponse> save({dynamic context}) async {
final ParseResponse childrenResponse = await _saveChildren(this);
if (childrenResponse.success) {
ParseResponse? response;
if (objectId == null) {
response = await create();
response = await create(context: context);
} else if (_isDirty(false)) {
response = await update();
response = await update(context: context);
}

if (response != null) {
Expand Down
4 changes: 2 additions & 2 deletions packages/dart/lib/src/objects/parse_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
/// If changes are made to the current user, call save to sync them with
/// Parse Server
@override
Future<ParseResponse> save() async {
Future<ParseResponse> save({dynamic context}) async {
if (objectId == null) {
return await signUp();
} else {
Expand All @@ -406,7 +406,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
}

@override
Future<ParseResponse> update() async {
Future<ParseResponse> update({dynamic context}) async {
if (objectId == null) {
return await signUp();
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/dart/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: parse_server_sdk
description: The Dart SDK to connect to Parse Server. Build your apps faster with Parse Platform, the complete application stack.
version: 6.0.0
version: 6.1.0
homepage: https://parseplatform.org
repository: https://github.com/parse-community/Parse-SDK-Flutter
issue_tracker: https://github.com/parse-community/Parse-SDK-Flutter/issues
Expand Down

0 comments on commit 21ce56f

Please sign in to comment.