Skip to content
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

updates to dart 2.1 #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ doc/api/
*.ipr
*.iws

# VS Code
.vscode/

# Other files
test
52 changes: 8 additions & 44 deletions lib/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()}';
}

Expand All @@ -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<Map> _query(String endpoint,
// {String method: 'GET',
// Map<String, String> params,
// _Params params2,
// bool https: false}) async {
// List<String> query = [];
// params ??= new Map<String, String>();
// 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<String> handleRequest(Request);
Future<String> handleRequest(http.Request request);

Future<String> handleResponse(http.Response response) async {
if (response.statusCode == 429) {
Expand Down
8 changes: 4 additions & 4 deletions lib/src/groups/authentication.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions lib/src/params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,6 +54,6 @@ class _Params extends Object with MapMixin {
return query.join('&');
}

String toJSON() => JSON.encode(_obj);
String toJSON() => json.encode(_obj);

}
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ author: Josep Sayol <[email protected]>
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'