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

Side by Side 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, 3 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library oauth2_client; 5 library oauth2_client;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:http/http.dart' as http; 9 import 'package:http/http.dart' as http;
10 10
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 Client( 73 Client(
74 this.identifier, 74 this.identifier,
75 this.secret, 75 this.secret,
76 this._credentials, 76 this._credentials,
77 {http.Client httpClient}) 77 {http.Client httpClient})
78 : _httpClient = httpClient == null ? new http.Client() : httpClient; 78 : _httpClient = httpClient == null ? new http.Client() : httpClient;
79 79
80 /// Sends an HTTP request with OAuth2 authorization credentials attached. This 80 /// Sends an HTTP request with OAuth2 authorization credentials attached. This
81 /// will also automatically refresh this client's [Credentials] before sending 81 /// will also automatically refresh this client's [Credentials] before sending
82 /// the request if necessary. 82 /// the request if necessary.
83 Future<http.StreamedResponse> send(http.BaseRequest request) { 83 Future<http.StreamedResponse> send(http.BaseRequest request) async {
84 return async.then((_) { 84 if (credentials.isExpired) {
85 if (!credentials.isExpired) return new Future.value();
86 if (!credentials.canRefresh) throw new ExpirationException(credentials); 85 if (!credentials.canRefresh) throw new ExpirationException(credentials);
87 return refreshCredentials(); 86 await refreshCredentials();
88 }).then((_) { 87 }
89 request.headers['authorization'] = "Bearer ${credentials.accessToken}";
90 return _httpClient.send(request);
91 }).then((response) {
92 if (response.statusCode != 401 ||
93 !response.headers.containsKey('www-authenticate')) {
94 return response;
95 }
96 88
97 var authenticate; 89 request.headers['authorization'] = "Bearer ${credentials.accessToken}";
98 try { 90 var response = await _httpClient.send(request);
99 authenticate = new AuthenticateHeader.parse(
100 response.headers['www-authenticate']);
101 } on FormatException catch (e) {
102 return response;
103 }
104 91
105 if (authenticate.scheme != 'bearer') return response; 92 if (response.statusCode != 401) return response;
93 if (!response.headers.containsKey('www-authenticate')) return response;
106 94
107 var params = authenticate.parameters; 95 var authenticate;
108 if (!params.containsKey('error')) return response; 96 try {
97 authenticate = new AuthenticateHeader.parse(
98 response.headers['www-authenticate']);
99 } on FormatException catch (e) {
100 return response;
101 }
109 102
110 throw new AuthorizationException( 103 if (authenticate.scheme != 'bearer') return response;
111 params['error'], params['error_description'], 104
112 params['error_uri'] == null ? null : Uri.parse(params['error_uri'])); 105 var params = authenticate.parameters;
113 }); 106 if (!params.containsKey('error')) return response;
107
108 throw new AuthorizationException(
109 params['error'], params['error_description'],
110 params['error_uri'] == null ? null : Uri.parse(params['error_uri']));
114 } 111 }
115 112
116 /// Explicitly refreshes this client's credentials. Returns this client. 113 /// Explicitly refreshes this client's credentials. Returns this client.
117 /// 114 ///
118 /// This will throw a [StateError] if the [Credentials] can't be refreshed, an 115 /// This will throw a [StateError] if the [Credentials] can't be refreshed, an
119 /// [AuthorizationException] if refreshing the credentials fails, or a 116 /// [AuthorizationException] if refreshing the credentials fails, or a
120 /// [FormatError] if the authorization server returns invalid responses. 117 /// [FormatError] if the authorization server returns invalid responses.
121 /// 118 ///
122 /// You may request different scopes than the default by passing in 119 /// You may request different scopes than the default by passing in
123 /// [newScopes]. These must be a subset of the scopes in the 120 /// [newScopes]. These must be a subset of the scopes in the
124 /// [Credentials.scopes] field of [Client.credentials]. 121 /// [Credentials.scopes] field of [Client.credentials].
125 Future<Client> refreshCredentials([List<String> newScopes]) { 122 Future<Client> refreshCredentials([List<String> newScopes]) async {
126 return async.then((_) { 123 if (!credentials.canRefresh) {
127 if (!credentials.canRefresh) { 124 var prefix = "OAuth credentials";
128 var prefix = "OAuth credentials"; 125 if (credentials.isExpired) prefix = "$prefix have expired and";
129 if (credentials.isExpired) prefix = "$prefix have expired and"; 126 throw new StateError("$prefix can't be refreshed.");
130 throw new StateError("$prefix can't be refreshed."); 127 }
131 }
132 128
133 return credentials.refresh(identifier, secret, 129 _credentials = await credentials.refresh(
134 newScopes: newScopes, httpClient: _httpClient); 130 identifier, secret,
135 }).then((credentials) { 131 newScopes: newScopes, httpClient: _httpClient);
136 _credentials = credentials; 132
137 return this; 133 return this;
138 });
139 } 134 }
140 135
141 /// Closes this client and its underlying HTTP client. 136 /// Closes this client and its underlying HTTP client.
142 void close() { 137 void close() {
143 if (_httpClient != null) _httpClient.close(); 138 if (_httpClient != null) _httpClient.close();
144 _httpClient = null; 139 _httpClient = null;
145 } 140 }
146 } 141 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698