|
| 1 | +part of flutter_parse_sdk; |
| 2 | + |
| 3 | +class ParseSession extends ParseObject implements ParseCloneable { |
| 4 | + @override |
| 5 | + clone(Map map) { |
| 6 | + print(map); |
| 7 | + return this.fromJson(map); |
| 8 | + } |
| 9 | + |
| 10 | + static final String keyVarUser = 'user'; |
| 11 | + static final String keyVarCreatedWith = 'createdWith'; |
| 12 | + static final String keyVarRestricted = 'restricted'; |
| 13 | + static final String keyVarExpiresAt = 'expiresAt'; |
| 14 | + static final String keyVarInstallationId = 'installationId'; |
| 15 | + |
| 16 | + String get sessionToken => super.get<String>(keyVarSessionToken); |
| 17 | + |
| 18 | + ParseObject get user => super.get<ParseObject>(keyVarUser); |
| 19 | + |
| 20 | + Map<String, dynamic> get createdWith => |
| 21 | + super.get<Map<String, dynamic>>(keyVarCreatedWith); |
| 22 | + |
| 23 | + bool get restricted => super.get<bool>(keyVarRestricted); |
| 24 | + |
| 25 | + DateTime get expiresAt => super.get<DateTime>(keyVarExpiresAt); |
| 26 | + |
| 27 | + String get installationId => super.get<String>(keyVarInstallationId); |
| 28 | + |
| 29 | + ParseSession({String sessionToken, bool debug, ParseHTTPClient client}) |
| 30 | + : super(keyClassSession) { |
| 31 | + _debug = isDebugEnabled(objectLevelDebug: debug); |
| 32 | + _client = client ?? |
| 33 | + ParseHTTPClient( |
| 34 | + autoSendSessionId: true, |
| 35 | + securityContext: ParseCoreData().securityContext); |
| 36 | + } |
| 37 | + |
| 38 | + Future<ParseResponse> getCurrentSessionFromServer() async { |
| 39 | + try { |
| 40 | + Uri tempUri = Uri.parse(_client.data.serverUrl); |
| 41 | + |
| 42 | + Uri url = Uri( |
| 43 | + scheme: tempUri.scheme, |
| 44 | + host: tempUri.host, |
| 45 | + path: "${tempUri.path}$keyEndPointSessions/me"); |
| 46 | + |
| 47 | + final response = await _client.get(url); |
| 48 | + |
| 49 | + return _handleResponse( |
| 50 | + this, response, ParseApiRQ.logout, _debug, className); |
| 51 | + } on Exception catch (e) { |
| 52 | + return _handleException(e, ParseApiRQ.logout, _debug, className); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /// Handles an API response and logs data if [bool] debug is enabled |
| 57 | + static ParseResponse _handleException( |
| 58 | + Exception exception, ParseApiRQ type, bool debug, String className) { |
| 59 | + ParseResponse parseResponse = ParseResponse.handleException(exception); |
| 60 | + |
| 61 | + if (debug) { |
| 62 | + logger( |
| 63 | + ParseCoreData().appName, className, type.toString(), parseResponse); |
| 64 | + } |
| 65 | + |
| 66 | + return parseResponse; |
| 67 | + } |
| 68 | + |
| 69 | + /// Handles all the response data for this class |
| 70 | + static ParseResponse _handleResponse(ParseSession session, Response response, |
| 71 | + ParseApiRQ type, bool debug, String className) { |
| 72 | + ParseResponse parseResponse = |
| 73 | + ParseResponse.handleResponse<ParseSession>(session, response); |
| 74 | + |
| 75 | + if (debug) { |
| 76 | + logger( |
| 77 | + ParseCoreData().appName, className, type.toString(), parseResponse); |
| 78 | + } |
| 79 | + |
| 80 | + return parseResponse; |
| 81 | + } |
| 82 | +} |
0 commit comments