Skip to content

Commit ac1121c

Browse files
committed
Version 1.0.3 - Added decoder
1 parent a31c47a commit ac1121c

9 files changed

+58
-70
lines changed

lib/parse.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ part 'src/network/parse_http_client.dart';
1616
part 'src/network/parse_livequery.dart';
1717
part 'src/network/parse_query.dart';
1818
part 'src/objects/parse_base.dart';
19-
part 'src/objects/parse_exception.dart';
19+
part 'src/objects/parse_error.dart';
2020
part 'src/objects/parse_function.dart';
2121
part 'src/objects/parse_geo_point.dart';
2222
part 'src/objects/parse_object.dart';

lib/src/objects/parse_exception.dart renamed to lib/src/objects/parse_error.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class ParseError {
88

99
// SDK errors / Errors
1010
1: 'No Results',
11+
400: 'Bad Request',
1112

1213
// Parse specific / Exceptions
1314
100: 'ConnectionFailed',
@@ -55,10 +56,11 @@ class ParseError {
5556
252: 'UnsupportedService'};
5657

5758
final int code;
58-
String type;
5959
final String message;
60+
final bool isTypeOfException;
61+
String type;
6062

61-
ParseError({this.code = -1, this.message = "Unkown error", bool debug: false}){
63+
ParseError({this.code = -1, this.message = "Unkown error", this.isTypeOfException = false, bool debug: false}){
6264
type = exceptions[code];
6365
if (debug) print(toString());
6466
}

lib/src/objects/parse_function.dart

+9-33
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
part of flutter_parse_sdk;
22

3-
class ParseCloudFunction extends ParseBase {
3+
class ParseCloudFunction extends ParseObject {
44
final String functionName;
5-
String _path;
65
bool _debug;
76
ParseHTTPClient _client;
87

8+
@override
9+
String _path;
10+
911
/// Creates a new cloud function object
1012
///
1113
/// {https://docs.parseplatform.org/cloudcode/guide/}
12-
ParseCloudFunction(this.functionName, {bool debug, ParseHTTPClient client}) {
13-
client == null ? _client = ParseHTTPClient() : _client = client;
14-
_debug = isDebugEnabled(debug, _client);
14+
ParseCloudFunction(this.functionName, {bool debug, ParseHTTPClient client}) : super (functionName) {
1515
_path = "/functions/$functionName";
16-
setObjectData(Map<String, dynamic>());
16+
17+
if (debug != null) setDebug(debug);
18+
if (client != null) setClient(client);
1719
}
1820

1921
/// Executes a cloud function
@@ -22,32 +24,6 @@ class ParseCloudFunction extends ParseBase {
2224
execute() async {
2325
var uri = _client.data.serverUrl + "$_path";
2426
var result = await _client.post(uri, body: JsonEncoder().convert(getObjectData()));
25-
return _handleResult(result, ParseApiRQ.execute);
26-
}
27-
28-
/// Handles an API response
29-
ParseResponse _handleResult(Response response, ParseApiRQ type) {
30-
ParseResponse parseResponse = ParseResponse.handleResponse(this, response);
31-
Map<String, dynamic> responseData = JsonDecoder().convert(response.body);
32-
33-
if (_client.data.debug || _debug) {
34-
var responseString = ' \n';
35-
36-
responseString += "----"
37-
"\n${_client.data.appName} API Response ($functionName : ${type.toString()}) :";
38-
39-
if (parseResponse.success && parseResponse.result != null) {
40-
responseString += "\nStatus Code: ${parseResponse.statusCode}";
41-
responseString += "\nPayload: ${responseData.toString()}";
42-
} else if (!parseResponse.success) {
43-
responseString += "\nStatus Code: ${responseData['code'] == null ? parseResponse.statusCode : responseData['code']}";
44-
responseString += "\nException: ${responseData['error'] == null ? responseData.toString() : responseData['error']}";
45-
}
46-
47-
responseString += "\n----\n";
48-
print(responseString);
49-
}
50-
51-
return parseResponse;
27+
return super.handleResponse(result, ParseApiRQ.execute);
5228
}
5329
}

lib/src/objects/parse_geo_point.dart

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ class ParseGeoPoint extends ParseObject {
66
double _longitude;
77

88
/// Creates a Parse Object of type GeoPoint
9-
ParseGeoPoint({double latitude = 0.0, double longitude = 0.0}): super ('GeoPoint') {
9+
ParseGeoPoint({double latitude = 0.0, double longitude = 0.0, bool debug, ParseHTTPClient client}): super ('GeoPoint') {
1010
_latitude = latitude;
1111
_longitude = longitude;
12+
13+
if (debug != null) setDebug(debug);
14+
if (client != null) setClient(client);
1215
}
1316

1417
double get latitude => _latitude;

lib/src/objects/parse_object.dart

+27-18
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@ class ParseObject extends ParseBase {
1111
/// [String] className refers to the Table Name in your Parse Server,
1212
/// [bool] debug will overwrite the current default debug settings and
1313
/// [ParseHttpClient] can be overwritten to create your own HTTP Client
14-
ParseObject(this.className, {bool debug: false, ParseHTTPClient client})
15-
: super() {
16-
client == null ? _client = ParseHTTPClient() : _client = client;
17-
_debug = isDebugEnabled(debug, _client);
14+
ParseObject(this.className, {bool debug: false}): super() {
1815
_path = "/classes/$className";
16+
setClient(ParseHTTPClient());
17+
setDebug(isDebugEnabled(_client, objectLevelDebug: debug));
18+
}
19+
20+
setDebug(bool debug){
21+
_debug = debug;
22+
}
23+
24+
setClient(ParseHTTPClient client){
25+
_client = client;
1926
}
2027

2128
/// Gets an object from the server using it's [String] objectId
@@ -24,19 +31,19 @@ class ParseObject extends ParseBase {
2431
var uri = _getBasePath(_path);
2532
if (objectId != null) uri += "/$objectId";
2633
var result = await _client.get(uri);
27-
return _handleResponse(result, ParseApiRQ.get);
34+
return handleResponse(result, ParseApiRQ.get);
2835
} on Exception catch (e) {
29-
return _handleException(e, ParseApiRQ.delete);
36+
return handleException(e, ParseApiRQ.delete);
3037
}
3138
}
3239

3340
/// Gets all objects from this table - Limited response at the moment
3441
getAll() async {
3542
try {
3643
var result = await _client.get(_getBasePath(_path));
37-
return _handleResponse(result, ParseApiRQ.getAll);
44+
return handleResponse(result, ParseApiRQ.getAll);
3845
} on Exception catch (e) {
39-
return _handleException(e, ParseApiRQ.delete);
46+
return handleException(e, ParseApiRQ.delete);
4047
}
4148
}
4249

@@ -45,9 +52,9 @@ class ParseObject extends ParseBase {
4552
try {
4653
var uri = _client.data.serverUrl + "$_path";
4754
var result = await _client.post(uri, body: JsonEncoder().convert(getObjectData()));
48-
return _handleResponse(result, ParseApiRQ.create);
55+
return handleResponse(result, ParseApiRQ.create);
4956
} on Exception catch (e) {
50-
return _handleException(e, ParseApiRQ.delete);
57+
return handleException(e, ParseApiRQ.delete);
5158
}
5259
}
5360

@@ -59,9 +66,9 @@ class ParseObject extends ParseBase {
5966
try {
6067
var uri = "${_getBasePath(_path)}/$objectId";
6168
var result = await _client.put(uri, body: JsonEncoder().convert(getObjectData()));
62-
return _handleResponse(result, ParseApiRQ.save);
69+
return handleResponse(result, ParseApiRQ.save);
6370
} on Exception catch (e) {
64-
return _handleException(e, ParseApiRQ.delete);
71+
return handleException(e, ParseApiRQ.delete);
6572
}
6673
}
6774
}
@@ -71,9 +78,9 @@ class ParseObject extends ParseBase {
7178
try {
7279
var uri = "${_getBasePath(_path)}?$query";
7380
var result = await _client.get(uri);
74-
return _handleResponse(result, ParseApiRQ.query);
81+
return handleResponse(result, ParseApiRQ.query);
7582
} on Exception catch (e) {
76-
return _handleException(e, ParseApiRQ.delete);
83+
return handleException(e, ParseApiRQ.delete);
7784
}
7885
}
7986

@@ -82,17 +89,18 @@ class ParseObject extends ParseBase {
8289
try {
8390
var uri = "${_getBasePath(path)}/$objectId";
8491
var result = await _client.delete(uri);
85-
return _handleResponse(result, ParseApiRQ.delete);
92+
return handleResponse(result, ParseApiRQ.delete);
8693
} on Exception catch (e) {
87-
return _handleException(e, ParseApiRQ.delete);
94+
return handleException(e, ParseApiRQ.delete);
8895
}
8996
}
9097

9198
/// Generates the path for the object
9299
_getBasePath(String path) => "${_client.data.serverUrl}$path";
93100

94101
/// Handles an API response and logs data if [bool] debug is enabled
95-
ParseResponse _handleResponse(Response response, ParseApiRQ type) {
102+
@protected
103+
ParseResponse handleResponse(Response response, ParseApiRQ type) {
96104
ParseResponse parseResponse = ParseResponse.handleResponse(this, response);
97105

98106
if (_debug) {
@@ -103,7 +111,8 @@ class ParseObject extends ParseBase {
103111
}
104112

105113
/// Handles an API response and logs data if [bool] debug is enabled
106-
ParseResponse _handleException(Exception exception, ParseApiRQ type) {
114+
@protected
115+
ParseResponse handleException(Exception exception, ParseApiRQ type) {
107116
ParseResponse parseResponse = ParseResponse.handleException(this, exception);
108117

109118
if (_debug) {

lib/src/objects/parse_response.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ParseResponse {
3434
/// Handles exception instead of throwing an exception
3535
static handleException(ParseBase object, Exception exception) {
3636
var response = ParseResponse();
37-
response.error = ParseError(message: exception.toString());
37+
response.error = ParseError(message: exception.toString(), isTypeOfException: true);
3838
return response;
3939
}
4040

@@ -51,7 +51,7 @@ class ParseResponse {
5151
return response;
5252
}
5353

54-
/// Handles succesful response with results
54+
/// Handles successful response with results
5555
static ParseResponse _handleSuccess(ParseResponse response, ParseObject object, String responseBody) {
5656
response.success = true;
5757

lib/src/objects/parse_user.dart

+2-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ParseUser extends ParseBase {
2323
/// username and password is required to login
2424
ParseUser(this.username, this.password, this.emailAddress, {bool debug, ParseHTTPClient client}) : super() {
2525
client == null ? _client = ParseHTTPClient() : _client = client;
26-
_debug = isDebugEnabled(debug, _client);
26+
_debug = isDebugEnabled(client, objectLevelDebug: debug);
2727
}
2828

2929
/// Returns a [User] from a [Map] object
@@ -58,8 +58,7 @@ class ParseUser extends ParseBase {
5858
/// Returns a [String] that's human readable. Ideal for printing logs
5959
@override
6060
String toString() =>
61-
"Username: $username \n"
62-
"Email Address:$emailAddress";
61+
"Username: $username \nEmail Address:$emailAddress";
6362

6463
static const String USERNAME = 'Username';
6564
static const String EMAIL = 'Email';

lib/src/utils/parse_logger.dart

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ void logger(
1515
} else if (!parseResponse.success) {
1616
responseString += "\nStatus Code: ${parseResponse.error.code}";
1717
responseString += "\nType: ${parseResponse.error.type}";
18-
responseString += "\nMessage: ${parseResponse.error.message}";
18+
19+
String errorOrException = parseResponse.error.isTypeOfException ? "Exception" : "Error";
20+
21+
responseString += "\n$errorOrException: ${parseResponse.error.message}";
1922
}
2023

2124
responseString += "\n----\n";

lib/src/utils/parse_utils.dart

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
part of flutter_parse_sdk;
22

3-
/// Checks wether debug is enabled
3+
/// Checks whether debug is enabled
44
///
55
/// Debug can be set in 2 places, one global param in the Parse.initialise, and
6-
/// then can be overriden class by class
7-
bool isDebugEnabled(bool debug, ParseHTTPClient _client) {
8-
if (debug == null) {
9-
_client.data.debug != null ? debug = _client.data.debug : debug = false;
10-
} else {
11-
return debug;
12-
}
13-
6+
/// then can be overidden class by class
7+
bool isDebugEnabled(ParseHTTPClient _client, {objectLevelDebug: false}) {
8+
bool debug = objectLevelDebug;
9+
if (ParseCoreData().debug != null) debug = ParseCoreData().debug;
1410
return debug;
1511
}

0 commit comments

Comments
 (0)