forked from parse-community/Parse-SDK-Flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_utils.dart
More file actions
86 lines (74 loc) · 2.45 KB
/
Copy pathparse_utils.dart
File metadata and controls
86 lines (74 loc) · 2.45 KB
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
part of flutter_parse_sdk;
/// Checks whether debug is enabled
///
/// Debug can be set in 2 places, one global param in the Parse.initialize, and
/// then can be overwritten class by class
bool isDebugEnabled({bool objectLevelDebug}) {
return objectLevelDebug ??= ParseCoreData().debug;
}
/// Converts the object to the correct value for JSON,
///
/// Strings are wrapped with "" but integers and others are not
dynamic convertValueToCorrectType(dynamic value) {
/*if (value is String && !value.contains('__type')) {
return '\"$value\"';
}*/
if (value is DateTime || value is ParseObject) {
return parseEncode(value);
} else {
return value;
}
}
/// Sanitises a url
Uri getSanitisedUri(ParseHTTPClient client, String pathToAppend,
{Map<String, dynamic> queryParams, String query}) {
final Uri tempUri = Uri.parse(client.data.serverUrl);
final Uri url = Uri(
scheme: tempUri.scheme,
host: tempUri.host,
port: tempUri.port,
path: '${tempUri.path}$pathToAppend',
queryParameters: queryParams,
query: query);
return url;
}
/// Sanitises a url
Uri getCustomUri(ParseHTTPClient client, String path,
{Map<String, dynamic> queryParams, String query}) {
final Uri tempUri = Uri.parse(client.data.serverUrl);
final Uri url = Uri(
scheme: tempUri.scheme,
host: tempUri.host,
port: tempUri.port,
path: path,
queryParameters: queryParams,
query: query);
return url;
}
/// Removes unncessary /
String removeTrailingSlash(String serverUrl) {
if (serverUrl.isNotEmpty &&
serverUrl.substring(serverUrl.length - 1) == '/') {
return serverUrl.substring(0, serverUrl.length - 1);
} else {
return serverUrl;
}
}
Future<ParseResponse> batchRequest(
List<dynamic> requests, List<ParseObject> objects,
{ParseHTTPClient client, bool debug}) async {
debug = isDebugEnabled(objectLevelDebug: debug);
client = client ??
ParseHTTPClient(
sendSessionId: ParseCoreData().autoSendSessionId,
securityContext: ParseCoreData().securityContext);
try {
final Uri url = getSanitisedUri(client, '/batch');
final String body = json.encode(<String, dynamic>{'requests': requests});
final Response result = await client.post(url, body: body);
return handleResponse<ParseObject>(
objects, result, ParseApiRQ.batch, debug, 'parse_utils');
} on Exception catch (e) {
return handleException(e, ParseApiRQ.batch, debug, 'parse_utils');
}
}