fresh 0.0.1-dev+2 fresh: ^0.0.1-dev+2 copied to clipboard
A Dart HTTP Client with built-in token refresh. Fresh is built on top of package:http and manages tokens transparently.
A Dart HTTP Client with built-in token refresh.
Overview #
Fresh is a package which attempts to simplify custom API authentication by integrating token refresh and caching directly into the client. Fresh is flexible and is intended to support custom token refresh mechanisms.
Usage #
Extend FreshClient #
class MyHttpClient extends FreshClient<OAuth2Token> {
// Must provide a concrete implementation of `TokenStorage` to super.
MyHttpClient() : super(InMemoryTokenStorage());
@override
Future<OAuth2Token> refreshToken(token, client) async {
// Make a token refresh request using the current token
// and the provided client and return a new token.
}
}
Make Requests #
As requests are made, FreshClient
will handle managing the token for you. Tokens will be refreshed as needed and requests will be automatically retried pending a successful refresh.
final httpClient = MyHttpClient();
final response = await httpClient.get(url, headers: headers);
Example #
See the example for a complete sample application using fresh
which integrates with jsonplaceholder.