diff --git a/lib/parse.dart b/lib/parse.dart index 516851a63..ff2ba1e03 100644 --- a/lib/parse.dart +++ b/lib/parse.dart @@ -10,6 +10,8 @@ import 'package:meta/meta.dart'; import 'package:path/path.dart' as path; import 'package:shared_preferences/shared_preferences.dart'; import 'package:web_socket_channel/io.dart'; +import 'package:uuid/uuid.dart'; +import 'package:path_provider/path_provider.dart'; part 'src/base/parse_constants.dart'; diff --git a/lib/src/base/parse_constants.dart b/lib/src/base/parse_constants.dart index 6e2ba9494..4d7c03af2 100644 --- a/lib/src/base/parse_constants.dart +++ b/lib/src/base/parse_constants.dart @@ -7,6 +7,7 @@ const String keyLibraryName= 'Flutter Parse SDK'; // End Points const String keyEndPointUserName = '/users/me'; const String keyEndPointLogin = '/login'; +const String keyEndPointUsers = '/users'; const String keyEndPointVerificationEmail = '/verificationEmailRequest'; const String keyEndPointRequestPasswordReset = '/requestPasswordReset'; const String keyEndPointClasses = '/classes/'; diff --git a/lib/src/enums/parse_enum_api_rq.dart b/lib/src/enums/parse_enum_api_rq.dart index 324e70253..6166516ff 100644 --- a/lib/src/enums/parse_enum_api_rq.dart +++ b/lib/src/enums/parse_enum_api_rq.dart @@ -12,6 +12,7 @@ enum ParseApiRQ { currentUser, signUp, login, + loginAnonymous, verificationEmailRequest, requestPasswordReset, destroy, diff --git a/lib/src/objects/parse_file.dart b/lib/src/objects/parse_file.dart index 142633156..f777ba4a6 100644 --- a/lib/src/objects/parse_file.dart +++ b/lib/src/objects/parse_file.dart @@ -13,6 +13,12 @@ class ParseFile extends ParseObject { String get url => _fileUrl; + File get file => _file; + + set url(String url) => _fileUrl = url; + + set name(String name) => _fileName = name; + bool get saved => url != null; @override @@ -24,12 +30,53 @@ class ParseFile extends ParseObject { /// Creates a new file /// /// {https://docs.parseplatform.org/rest/guide/#files/} - ParseFile(this._file, {bool debug, ParseHTTPClient client}) : super (keyFile){ + ParseFile(this._file, {String name, String url, bool debug, ParseHTTPClient client}) : super (keyFile){ client == null ? _client = ParseHTTPClient() : _client = client; _debug = isDebugEnabled(objectLevelDebug: debug); + if(_file != null) { + this._fileName = path.basename(_file.path); + this._path = 'files/$_fileName'; + } + else { + this._fileName = name; + this._fileUrl = url; + } + } + + Future loadStorage() async { + Directory tempPath = await getTemporaryDirectory(); + + if(_fileName == null) { + _file = null; + return this; + } + + File possibleFile = new File("${tempPath.path}/$_fileName"); + bool exists = await possibleFile.exists(); + + if(exists) { + _file = possibleFile; + } + else { + _file = null; + } + + return this; + } + + Future download() async { + if(_fileUrl == null) { + return this; + } + + Directory tempPath = await getTemporaryDirectory(); + this._file = new File("${tempPath.path}/$_fileName"); + await _file.create(); + + var response = await _client.get(_fileUrl); + _file.writeAsBytes(response.bodyBytes); - this._fileName = path.basename(_file.path); - this._path = 'files/$_fileName'; + return this; } /// Uploads a file to Parse Server @@ -48,4 +95,4 @@ class ParseFile extends ParseObject { final response = await _client.post(uri, headers: headers, body: body); return super.handleResponse(response, ParseApiRQ.upload); } -} \ No newline at end of file +} diff --git a/lib/src/objects/parse_user.dart b/lib/src/objects/parse_user.dart index a5a523ed4..d5dd0ec18 100644 --- a/lib/src/objects/parse_user.dart +++ b/lib/src/objects/parse_user.dart @@ -155,6 +155,37 @@ class ParseUser extends ParseObject implements ParseCloneable { } } + // Logs in a user anonymously + Future loginAnonymous() async { + try { + Uri tempUri = Uri.parse(_client.data.serverUrl); + + Uri url = Uri( + scheme: tempUri.scheme, + host: tempUri.host, + path: "${tempUri.path}$keyEndPointUsers", + ); + + var uuid = new Uuid(); + + final response = await _client.post(url, headers: { + keyHeaderRevocableSession: "1", + + }, body: jsonEncode({ + "authData": { + "anonymous": { + "id": uuid.v4() + } + } + })); + + return _handleResponse(response, ParseApiRQ.loginAnonymous); + } + on Exception catch (e) { + return _handleException(e, ParseApiRQ.loginAnonymous); + } + } + /// Removes the current user from the session data logout() { _client.data.sessionId = null; @@ -198,7 +229,9 @@ class ParseUser extends ParseObject implements ParseCloneable { var uri = _client.data.serverUrl + "$path/$objectId"; var body = json.encode(toJson(forApiRQ: true), toEncodable: dateTimeEncoder); - final response = await _client.put(uri, body: body); + final response = await _client.put(uri, + headers: {keyHeaderSessionToken: _client.data.sessionId}, + body: body); return _handleResponse(response, ParseApiRQ.save); } on Exception catch (e) { return _handleException(e, ParseApiRQ.save); diff --git a/lib/src/utils/parse_decoder.dart b/lib/src/utils/parse_decoder.dart index 9199b1a07..b0022bfde 100644 --- a/lib/src/utils/parse_decoder.dart +++ b/lib/src/utils/parse_decoder.dart @@ -64,7 +64,7 @@ dynamic parseDecode(dynamic value) { } return ParseObject(className).fromJson(map); case "File": - return new ParseFile(null).fromJson(map); + return new ParseFile(null, url: map["url"], name: map["name"]).fromJson(map); case "GeoPoint": num latitude = map["latitude"] ?? 0.0; num longitude = map["longitude"] ?? 0.0; diff --git a/pubspec.yaml b/pubspec.yaml index c25a51adb..9e7df516b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,4 +17,5 @@ dependencies: # Utils shared_preferences: ^0.4.3 - path_provider: ^0.4.1 \ No newline at end of file + path_provider: ^0.4.1 + uuid: ^1.0.0