Skip to content

Allow providing default a SecurityContext object during initialization #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/parse.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:io';
import 'dart:typed_data';

import 'package:http/http.dart';
import 'package:http/io_client.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;
import 'package:shared_preferences/shared_preferences.dart';
Expand Down Expand Up @@ -55,7 +56,6 @@ part 'src/utils/parse_utils.dart';

class Parse {
ParseCoreData data;
final ParseHTTPClient client = new ParseHTTPClient();
bool _hasBeenInitialised = false;

/// To initialise Parse Server in your application
Expand All @@ -76,14 +76,16 @@ class Parse {
String liveQueryUrl,
String clientKey,
String masterKey,
String sessionId}) {
String sessionId,
SecurityContext securityContext}) {
ParseCoreData.init(appId, serverUrl,
debug: debug,
appName: appName,
liveQueryUrl: liveQueryUrl,
masterKey: masterKey,
clientKey: clientKey,
sessionId: sessionId);
sessionId: sessionId,
securityContext: securityContext);

ParseCoreData().initStorage();

Expand All @@ -98,7 +100,7 @@ class Parse {
ParseResponse parseResponse;

try {
var response = await ParseHTTPClient()
var response = await ParseHTTPClient(ParseCoreData().securityContext)
.get("${ParseCoreData().serverUrl}$keyEndPointHealth");
parseResponse =
ParseResponse.handleResponse(this, response, returnAsResult: true);
Expand Down
10 changes: 9 additions & 1 deletion lib/src/data/parse_core_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ class ParseCoreData {
/// This class should not be user unless switching servers during the app,
/// which is odd. Should only be user by Parse.init
static void init(appId, serverUrl,
{debug, appName, liveQueryUrl, masterKey, clientKey, sessionId}) {
{debug,
appName,
liveQueryUrl,
masterKey,
clientKey,
sessionId,
securityContext}) {
_instance = ParseCoreData._init(appId, serverUrl);

if (debug != null) _instance.debug = debug;
Expand All @@ -20,6 +26,7 @@ class ParseCoreData {
if (clientKey != null) _instance.clientKey = clientKey;
if (masterKey != null) _instance.masterKey = masterKey;
if (sessionId != null) _instance.sessionId = sessionId;
if (securityContext != null) _instance.securityContext = securityContext;
}

String appName;
Expand All @@ -29,6 +36,7 @@ class ParseCoreData {
String masterKey;
String clientKey;
String sessionId;
SecurityContext securityContext;
bool debug;
SharedPreferences storage;

Expand Down
7 changes: 5 additions & 2 deletions lib/src/network/parse_http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ part of flutter_parse_sdk;

/// Creates a custom version of HTTP Client that has Parse Data Preset
class ParseHTTPClient extends BaseClient {
final Client _client = Client();
final Client _client;
final String _userAgent = "$keyLibraryName $keySdkVersion";
ParseCoreData data = ParseCoreData();
Map<String, String> additionalHeaders;

ParseHTTPClient();
ParseHTTPClient([SecurityContext securityContext])
: _client = securityContext != null
? IOClient(HttpClient(context: securityContext))
: IOClient();

/// Overrides the call method for HTTP Client and adds custom headers
@override
Expand Down
8 changes: 5 additions & 3 deletions lib/src/objects/parse_config.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
part of flutter_parse_sdk;

class ParseConfig extends ParseObject {
var _client = ParseHTTPClient();
var _client = ParseHTTPClient(ParseCoreData().securityContext);

/// Creates an instance of ParseConfig so that you can grab all configs from the server
ParseConfig({bool debug, ParseHTTPClient client}) : super('config') {
Expand All @@ -14,7 +14,8 @@ class ParseConfig extends ParseObject {
try {
var uri = "${ParseCoreData().serverUrl}/config";
var result = await _client.get(uri);
return handleResponse(this, result, ParseApiRQ.getConfigs, _debug, className);
return handleResponse(
this, result, ParseApiRQ.getConfigs, _debug, className);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.getConfigs, _debug, className);
}
Expand All @@ -26,7 +27,8 @@ class ParseConfig extends ParseObject {
var uri = "${ParseCoreData().serverUrl}/config";
var body = "{\"params\":{\"$key\": \"${parseEncode(value)}\"}}";
var result = await _client.put(uri, body: body);
return handleResponse(this, result, ParseApiRQ.addConfig, _debug, className);
return handleResponse(
this, result, ParseApiRQ.addConfig, _debug, className);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.addConfig, _debug, className);
}
Expand Down
7 changes: 5 additions & 2 deletions lib/src/objects/parse_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class ParseFile extends ParseObject {
///
/// {https://docs.parseplatform.org/rest/guide/#files/}
ParseFile(this._file, {bool debug, ParseHTTPClient client}) : super(keyFile) {
client == null ? _client = ParseHTTPClient() : _client = client;
client == null
? _client = ParseHTTPClient(ParseCoreData().securityContext)
: _client = client;
_debug = isDebugEnabled(objectLevelDebug: debug);

this._fileName = path.basename(_file.path);
Expand All @@ -46,6 +48,7 @@ class ParseFile extends ParseObject {
var uri = _client.data.serverUrl + "$_path";
final body = await _file.readAsBytes();
final response = await _client.post(uri, headers: headers, body: body);
return handleResponse<ParseFile>(this, response, ParseApiRQ.upload, _debug, className);
return handleResponse<ParseFile>(
this, response, ParseApiRQ.upload, _debug, className);
}
}
12 changes: 9 additions & 3 deletions lib/src/objects/parse_geo_point.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
part of flutter_parse_sdk;

class ParseGeoPoint extends ParseObject {

double _latitude;
double _longitude;

/// Creates a Parse Object of type GeoPoint
ParseGeoPoint({double latitude = 0.0, double longitude = 0.0, bool debug, ParseHTTPClient client}): super (keyGeoPoint) {
ParseGeoPoint(
{double latitude = 0.0,
double longitude = 0.0,
bool debug,
ParseHTTPClient client})
: super(keyGeoPoint) {
_latitude = latitude;
_longitude = longitude;

client == null ? _client = ParseHTTPClient() : _client = client;
client == null
? _client = ParseHTTPClient(ParseCoreData().securityContext)
: _client = client;
_debug = isDebugEnabled(objectLevelDebug: debug);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/objects/parse_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ParseObject extends ParseBase implements ParseCloneable {
ParseObject(String className, {bool debug: false}) : super() {
setClassName(className);
_path = "$keyEndPointClasses$className";
setClient(ParseHTTPClient());
setClient(ParseHTTPClient(ParseCoreData().securityContext));
setDebug(isDebugEnabled(objectLevelDebug: debug));
}

Expand Down Expand Up @@ -82,7 +82,7 @@ class ParseObject extends ParseBase implements ParseCloneable {
}
}

/// Removes an element from an Arary
/// Removes an element from an Array
Future<ParseResponse> remove(String key, dynamic values) async {
if (key != null) {
return await _sortArrays(ParseApiRQ.remove, "Remove", key, [values]);
Expand Down
10 changes: 6 additions & 4 deletions lib/src/objects/parse_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class ParseUser extends ParseObject implements ParseCloneable {
ParseUser(String username, String password, String emailAddress,
{bool debug, ParseHTTPClient client})
: super(keyClassUser) {
client == null ? _client = ParseHTTPClient() : _client = client;
client == null
? _client = ParseHTTPClient(ParseCoreData().securityContext)
: _client = client;
_debug = isDebugEnabled(objectLevelDebug: debug);

this.username = username;
Expand Down Expand Up @@ -83,7 +85,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
host: tempUri.host,
path: "${tempUri.path}$keyEndPointUserName");

final response = await ParseHTTPClient()
final response = await ParseHTTPClient(ParseCoreData().securityContext)
.get(uri, headers: {keyHeaderSessionToken: token});
return _handleResponse(_getEmptyUser(), response, ParseApiRQ.currentUser,
_debug, _getEmptyUser().className);
Expand Down Expand Up @@ -241,8 +243,8 @@ class ParseUser extends ParseObject implements ParseCloneable {
var emptyUser = ParseUser(null, null, null);

try {
final response =
await ParseHTTPClient().get("${ParseCoreData().serverUrl}/$path");
final response = await ParseHTTPClient(ParseCoreData().securityContext)
.get("${ParseCoreData().serverUrl}/$path");

ParseResponse parseResponse =
ParseResponse.handleResponse<ParseUser>(emptyUser, response);
Expand Down