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

Side by Side Diff: pkg/oauth2/lib/src/client.dart

Issue 11783009: Big merge from experimental to bleeding edge. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « pkg/oauth2/lib/src/authorization_code_grant.dart ('k') | pkg/oauth2/lib/src/credentials.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 client; 5 library client;
6 6
7 import 'dart:async';
7 import 'dart:uri'; 8 import 'dart:uri';
8 9
9 import '../../../http/lib/http.dart' as http; 10 import '../../../http/lib/http.dart' as http;
10 11
11 import 'authorization_exception.dart'; 12 import 'authorization_exception.dart';
12 import 'credentials.dart'; 13 import 'credentials.dart';
13 import 'expiration_exception.dart'; 14 import 'expiration_exception.dart';
14 import 'utils.dart'; 15 import 'utils.dart';
15 16
16 // TODO(nweiz): Add an onCredentialsRefreshed event once we have some event 17 // TODO(nweiz): Add an onCredentialsRefreshed event once we have some event
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 this.identifier, 75 this.identifier,
75 this.secret, 76 this.secret,
76 this._credentials, 77 this._credentials,
77 {http.Client httpClient}) 78 {http.Client httpClient})
78 : _httpClient = httpClient == null ? new http.Client() : httpClient; 79 : _httpClient = httpClient == null ? new http.Client() : httpClient;
79 80
80 /// Sends an HTTP request with OAuth2 authorization credentials attached. This 81 /// Sends an HTTP request with OAuth2 authorization credentials attached. This
81 /// will also automatically refresh this client's [Credentials] before sending 82 /// will also automatically refresh this client's [Credentials] before sending
82 /// the request if necessary. 83 /// the request if necessary.
83 Future<http.StreamedResponse> send(http.BaseRequest request) { 84 Future<http.StreamedResponse> send(http.BaseRequest request) {
84 return async.chain((_) { 85 return async.then((_) {
85 if (!credentials.isExpired) return new Future.immediate(null); 86 if (!credentials.isExpired) return new Future.immediate(null);
86 if (!credentials.canRefresh) throw new ExpirationException(credentials); 87 if (!credentials.canRefresh) throw new ExpirationException(credentials);
87 return refreshCredentials(); 88 return refreshCredentials();
88 }).chain((_) { 89 }).then((_) {
89 request.headers['authorization'] = "Bearer ${credentials.accessToken}"; 90 request.headers['authorization'] = "Bearer ${credentials.accessToken}";
90 return _httpClient.send(request); 91 return _httpClient.send(request);
91 }).transform((response) { 92 }).then((response) {
92 if (response.statusCode != 401 || 93 if (response.statusCode != 401 ||
93 !response.headers.containsKey('www-authenticate')) { 94 !response.headers.containsKey('www-authenticate')) {
94 return response; 95 return response;
95 } 96 }
96 97
97 var authenticate; 98 var authenticate;
98 try { 99 try {
99 authenticate = new AuthenticateHeader.parse( 100 authenticate = new AuthenticateHeader.parse(
100 response.headers['www-authenticate']); 101 response.headers['www-authenticate']);
101 } on FormatException catch (e) { 102 } on FormatException catch (e) {
(...skipping 13 matching lines...) Expand all
115 /// Explicitly refreshes this client's credentials. Returns this client. 116 /// Explicitly refreshes this client's credentials. Returns this client.
116 /// 117 ///
117 /// This will throw a [StateError] if the [Credentials] can't be refreshed, an 118 /// This will throw a [StateError] if the [Credentials] can't be refreshed, an
118 /// [AuthorizationException] if refreshing the credentials fails, or a 119 /// [AuthorizationException] if refreshing the credentials fails, or a
119 /// [FormatError] if the authorization server returns invalid responses. 120 /// [FormatError] if the authorization server returns invalid responses.
120 /// 121 ///
121 /// You may request different scopes than the default by passing in 122 /// You may request different scopes than the default by passing in
122 /// [newScopes]. These must be a subset of the scopes in the 123 /// [newScopes]. These must be a subset of the scopes in the
123 /// [Credentials.scopes] field of [Client.credentials]. 124 /// [Credentials.scopes] field of [Client.credentials].
124 Future<Client> refreshCredentials([List<String> newScopes]) { 125 Future<Client> refreshCredentials([List<String> newScopes]) {
125 return async.chain((_) { 126 return async.then((_) {
126 if (!credentials.canRefresh) { 127 if (!credentials.canRefresh) {
127 var prefix = "OAuth credentials"; 128 var prefix = "OAuth credentials";
128 if (credentials.isExpired) prefix = "$prefix have expired and"; 129 if (credentials.isExpired) prefix = "$prefix have expired and";
129 throw new StateError("$prefix can't be refreshed."); 130 throw new StateError("$prefix can't be refreshed.");
130 } 131 }
131 132
132 return credentials.refresh(identifier, secret, 133 return credentials.refresh(identifier, secret,
133 newScopes: newScopes, httpClient: _httpClient); 134 newScopes: newScopes, httpClient: _httpClient);
134 }).transform((credentials) { 135 }).then((credentials) {
135 _credentials = credentials; 136 _credentials = credentials;
136 return this; 137 return this;
137 }); 138 });
138 } 139 }
139 140
140 /// Closes this client and its underlying HTTP client. 141 /// Closes this client and its underlying HTTP client.
141 void close() { 142 void close() {
142 if (_httpClient != null) _httpClient.close(); 143 if (_httpClient != null) _httpClient.close();
143 _httpClient = null; 144 _httpClient = null;
144 } 145 }
145 } 146 }
OLDNEW
« no previous file with comments | « pkg/oauth2/lib/src/authorization_code_grant.dart ('k') | pkg/oauth2/lib/src/credentials.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698