Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Unified Diff: lib/src/client.dart

Issue 1308163004: Async-ify. (Closed) Base URL: git@github.com:dart-lang/oauth2.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/src/client.dart
diff --git a/lib/src/client.dart b/lib/src/client.dart
index d473be19aeda0b8f4120ecf269e5ac0b9141bd28..60495a0ab540805036b28de0fd5a7a6d01bc7647 100644
--- a/lib/src/client.dart
+++ b/lib/src/client.dart
@@ -80,37 +80,34 @@ class Client extends http.BaseClient {
/// Sends an HTTP request with OAuth2 authorization credentials attached. This
/// will also automatically refresh this client's [Credentials] before sending
/// the request if necessary.
- Future<http.StreamedResponse> send(http.BaseRequest request) {
- return async.then((_) {
- if (!credentials.isExpired) return new Future.value();
+ Future<http.StreamedResponse> send(http.BaseRequest request) async {
+ if (credentials.isExpired) {
if (!credentials.canRefresh) throw new ExpirationException(credentials);
- return refreshCredentials();
- }).then((_) {
- request.headers['authorization'] = "Bearer ${credentials.accessToken}";
- return _httpClient.send(request);
- }).then((response) {
- if (response.statusCode != 401 ||
- !response.headers.containsKey('www-authenticate')) {
- return response;
- }
-
- var authenticate;
- try {
- authenticate = new AuthenticateHeader.parse(
- response.headers['www-authenticate']);
- } on FormatException catch (e) {
- return response;
- }
-
- if (authenticate.scheme != 'bearer') return response;
-
- var params = authenticate.parameters;
- if (!params.containsKey('error')) return response;
-
- throw new AuthorizationException(
- params['error'], params['error_description'],
- params['error_uri'] == null ? null : Uri.parse(params['error_uri']));
- });
+ await refreshCredentials();
+ }
+
+ request.headers['authorization'] = "Bearer ${credentials.accessToken}";
+ var response = await _httpClient.send(request);
+
+ if (response.statusCode != 401) return response;
+ if (!response.headers.containsKey('www-authenticate')) return response;
+
+ var authenticate;
+ try {
+ authenticate = new AuthenticateHeader.parse(
+ response.headers['www-authenticate']);
+ } on FormatException catch (e) {
+ return response;
+ }
+
+ if (authenticate.scheme != 'bearer') return response;
+
+ var params = authenticate.parameters;
+ if (!params.containsKey('error')) return response;
+
+ throw new AuthorizationException(
+ params['error'], params['error_description'],
+ params['error_uri'] == null ? null : Uri.parse(params['error_uri']));
}
/// Explicitly refreshes this client's credentials. Returns this client.
@@ -122,20 +119,18 @@ class Client extends http.BaseClient {
/// You may request different scopes than the default by passing in
/// [newScopes]. These must be a subset of the scopes in the
/// [Credentials.scopes] field of [Client.credentials].
- Future<Client> refreshCredentials([List<String> newScopes]) {
- return async.then((_) {
- if (!credentials.canRefresh) {
- var prefix = "OAuth credentials";
- if (credentials.isExpired) prefix = "$prefix have expired and";
- throw new StateError("$prefix can't be refreshed.");
- }
-
- return credentials.refresh(identifier, secret,
- newScopes: newScopes, httpClient: _httpClient);
- }).then((credentials) {
- _credentials = credentials;
- return this;
- });
+ Future<Client> refreshCredentials([List<String> newScopes]) async {
+ if (!credentials.canRefresh) {
+ var prefix = "OAuth credentials";
+ if (credentials.isExpired) prefix = "$prefix have expired and";
+ throw new StateError("$prefix can't be refreshed.");
+ }
+
+ _credentials = await credentials.refresh(
+ identifier, secret,
+ newScopes: newScopes, httpClient: _httpClient);
+
+ return this;
}
/// Closes this client and its underlying HTTP client.

Powered by Google App Engine
This is Rietveld 408576698