import "package:http/http.dart"; import 'package:http/io_client.dart'; import "package:imagini/api/auth_client/base_auth_client.dart"; import "package:imagini/core/storage_client/base_storage_client.dart"; BaseClient getAuthClient(storage) => IOAuthClient(storage); class IOAuthClient extends IOClient implements BaseAuthClient { BaseStorageClient _storage; String _cachedServer; String _cachedAccessToken; String _cachedRefreshToken; IOAuthClient(BaseStorageClient storage) { _storage = storage; } @override String getServer() { return _cachedServer; } @override Map getHeaders() { return { 'X-Imagini-AccessToken': _cachedAccessToken, 'X-Imagini-RefreshToken': _cachedRefreshToken, }; } @override Future send(BaseRequest request) async { String _accessToken = await _storage.get("accessToken"); String _refreshToken = await _storage.get("refreshToken"); _cachedAccessToken = _accessToken; _cachedRefreshToken = _refreshToken; request.headers.addAll({ 'X-Imagini-AccessToken': _accessToken, 'X-Imagini-RefreshToken': _refreshToken, }); return super.send(request).then((response) async { if (response.headers.containsKey("x-imagini-accesstoken")) { _cachedAccessToken = response.headers["x-imagini-accesstoken"]; await _storage.set("accessToken", response.headers["x-imagini-accesstoken"]); } if (response.headers.containsKey("x-imagini-refreshtoken")) { _cachedRefreshToken = response.headers["x-imagini-refreshtoken"]; await _storage.set("refreshToken", response.headers["x-imagini-refreshtoken"]); } return response; }); } }