11import 'dart:async' ;
22
3- import 'package:oauth2/oauth2.dart' ;
3+ import 'package:http/http.dart' as http;
4+ import 'package:oauth2/oauth2.dart' as oauth2;
45import 'package:oauth_chopper/src/oauth_authenticator.dart' ;
56import 'package:oauth_chopper/src/oauth_grant.dart' ;
67import 'package:oauth_chopper/src/oauth_interceptor.dart' ;
@@ -27,6 +28,7 @@ class OAuthChopper {
2728 required this .identifier,
2829 required this .secret,
2930 this .endSessionEndpoint,
31+ this .httpClient,
3032
3133 /// OAuth storage for storing credentials.
3234 /// By default it will use a in memory storage [MemoryStorage] .
@@ -53,6 +55,10 @@ class OAuthChopper {
5355 /// See [OAuthStorage] for more information.
5456 final OAuthStorage _storage;
5557
58+ /// Provide a custom [http.Client] which will be passed to [oauth2] and used
59+ /// for making new requests.
60+ final http.Client ? httpClient;
61+
5662 /// Get stored [OAuthToken] .
5763 Future <OAuthToken ?> get token async {
5864 final credentialsJson = await _storage.fetchCredentials ();
@@ -76,18 +82,21 @@ class OAuthChopper {
7682 /// Tries to refresh the available credentials and returns a new [OAuthToken]
7783 /// instance.
7884 /// Throws an exception when refreshing fails. If the exception is a
79- /// [AuthorizationException] it clears the storage.
80- /// See [Credentials.refresh]
85+ /// [oauth2. AuthorizationException] it clears the storage.
86+ /// See [oauth2. Credentials.refresh]
8187 Future <OAuthToken ?> refresh () async {
8288 final credentialsJson = await _storage.fetchCredentials ();
8389 if (credentialsJson == null ) return null ;
84- final credentials = Credentials .fromJson (credentialsJson);
90+ final credentials = oauth2. Credentials .fromJson (credentialsJson);
8591 try {
86- final newCredentials =
87- await credentials.refresh (identifier: identifier, secret: secret);
92+ final newCredentials = await credentials.refresh (
93+ identifier: identifier,
94+ secret: secret,
95+ httpClient: httpClient,
96+ );
8897 await _storage.saveCredentials (newCredentials.toJson ());
8998 return OAuthToken .fromCredentials (newCredentials);
90- } on AuthorizationException {
99+ } on oauth2. AuthorizationException {
91100 _storage.clear ();
92101 rethrow ;
93102 }
@@ -99,11 +108,15 @@ class OAuthChopper {
99108 /// Currently supported grants:
100109 /// - [ResourceOwnerPasswordGrant]
101110 /// - [ClientCredentialsGrant]
102- ///
111+ /// - [AuthorizationCodeGrant]
103112 /// Throws an exception if the grant fails.
104113 Future <OAuthToken > requestGrant (OAuthGrant grant) async {
105- final credentials =
106- await grant.handle (authorizationEndpoint, identifier, secret);
114+ final credentials = await grant.handle (
115+ authorizationEndpoint,
116+ identifier,
117+ secret,
118+ httpClient,
119+ );
107120
108121 await _storage.saveCredentials (credentials);
109122
0 commit comments