diff --git a/.gitignore b/.gitignore index 23baa97..17ec080 100644 --- a/.gitignore +++ b/.gitignore @@ -21,5 +21,8 @@ doc/api/ *.ipr *.iws +# VS Code +.vscode/ + # Other files test diff --git a/lib/core.dart b/lib/core.dart index 6cd2c5e..0db186f 100644 --- a/lib/core.dart +++ b/lib/core.dart @@ -75,21 +75,21 @@ abstract class TMDBApiCore { {String method: 'GET', _Params params, bool https: false}) async { String query = 'api_key=$_apiKey'; - if (params?.needsSession) { + if (params?.needsSession ?? false) { if (authentication.sessionId == null) { throw new StateError( "Can't use this method without having a session ID."); } else { query += "&session_id=${authentication.sessionId}"; } - } else if (params?.needsGuestSession) { + } else if (params?.needsGuestSession ?? false) { if (authentication.guestSessionId == null) { throw new StateError( "Can't use this method without having a guest session ID."); } else { query += "&guest_session_id=${authentication.guestSessionId}"; } - } else if (params?.needsEitherSession) { + } else if (params?.needsEitherSession ?? false) { if (authentication.sessionId != null) { query += "&session_id=${authentication.sessionId}"; } else if (authentication.guestSessionId != null) { @@ -108,7 +108,7 @@ abstract class TMDBApiCore { // params.remove('guest_session_id'); // } - if (params?.hasElements && ['GET', 'HEAD', 'DELETE'].contains(method)) { + if ((params?.hasElements ?? false) && ['GET', 'HEAD', 'DELETE'].contains(method)) { query += '&${params.toString()}'; } @@ -122,56 +122,20 @@ abstract class TMDBApiCore { request.headers['Accept'] = 'application/json'; request.headers['Content-Type'] = 'application/json'; - if (params?.hasElements && ['POST', 'PUT', 'DELETE'].contains(method)) { + if ((params?.hasElements ?? false) && ['POST', 'PUT', 'DELETE'].contains(method)) { request.body = params?.toJSON(); } try { String response = await handleRequest(request); - return JSON.decode(response); + return json.decode(response); } catch (e) { - // TODO: Handle exceptions + rethrow; } } - // Future _query(String endpoint, - // {String method: 'GET', - // Map params, - // _Params params2, - // bool https: false}) async { - // List query = []; - // params ??= new Map(); - // params['api_key'] = _apiKey; - // - // params.forEach((String k, dynamic v) { - // query.add('$k=' + Uri.encodeQueryComponent(v.toString())); - // }); - // - // Uri url = new Uri( - // scheme: _apiUriHTTPS || https ? 'https' : 'http', - // host: _apiUriHost, - // path: '/$_apiUriVersion/$endpoint'); - // - // if (['GET', 'HEAD', 'DELETE'].contains(method)) { - // url = url.replace(query: query.join('&')); - // } - // - // http.Request request = new http.Request(method, url); - // - // if (['POST', 'PUT'].contains(method)) { - // request.body = query.join('&'); - // } - // - // try { - // String response = await handleRequest(request); - // return JSON.decode(response); - // } catch (e) { - // // TODO: Handle exceptions - // } - // } - // Abstract method to be implemented either for dart:html or dart:io. - Future handleRequest(Request); + Future handleRequest(http.Request request); Future handleResponse(http.Response response) async { if (response.statusCode == 429) { diff --git a/lib/src/groups/authentication.dart b/lib/src/groups/authentication.dart index 810d6c8..89839e7 100644 --- a/lib/src/groups/authentication.dart +++ b/lib/src/groups/authentication.dart @@ -34,7 +34,7 @@ class Authentication { return resp['request_token']; } else { String msg = "Couldn't obtain new token" + - (resp?.containsKey('status_message') + ((resp?.containsKey('status_message') ?? false) ? ': ' + resp['status_message'] : ''); throw new Exception(msg); @@ -63,7 +63,7 @@ class Authentication { return resp['request_token']; } else { String msg = "Couldn't authenticate user '$username'" + - (resp?.containsKey('status_message') + ((resp?.containsKey('status_message') ?? false) ? ': ' + resp['status_message'] : ''); throw new Exception(msg); @@ -85,7 +85,7 @@ class Authentication { return resp['session_id']; } else { String msg = "Couldn't obtain new session ID" + - (resp?.containsKey('status_message') + ((resp?.containsKey('status_message') ?? false) ? ': ' + resp['status_message'] : ''); throw new Exception(msg); @@ -107,7 +107,7 @@ class Authentication { return _guestSessionId; } else { String msg = "Couldn't obtain new guest session ID" + - (resp?.containsKey('status_message') + ((resp?.containsKey('status_message') ?? false) ? ': ' + resp['status_message'] : ''); throw new Exception(msg); diff --git a/lib/src/params.dart b/lib/src/params.dart index 1cd785f..f2f1d73 100644 --- a/lib/src/params.dart +++ b/lib/src/params.dart @@ -19,9 +19,9 @@ class _Params extends Object with MapMixin { bool get hasElements => _obj.length > 0; - operator [](String name) => _obj[name]; + operator [](dynamic name) => _obj[name]; - operator []=(String name, dynamic value) { + operator []=(dynamic name, dynamic value) { if (value != null) { if (value is bool) { _obj[name] = value; @@ -54,6 +54,6 @@ class _Params extends Object with MapMixin { return query.join('&'); } - String toJSON() => JSON.encode(_obj); + String toJSON() => json.encode(_obj); } diff --git a/pubspec.yaml b/pubspec.yaml index 52c6c41..86e9096 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,10 +5,10 @@ author: Josep Sayol homepage: https://github.com/jsayol/dart-tmdb environment: - sdk: '>=1.0.0 <2.0.0' + sdk: ">=2.0.0 <3.0.0" dependencies: - http: '>=0.11.0 <0.12.0' + http: '^0.12.0' #dev_dependencies: # test: '>=0.12.0 <0.13.0'