forked from ml-opensource/flutter-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ml-opensource#80): Configure Dios
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import 'package:dio/adapter.dart'; | ||
import 'package:dio/dio.dart'; | ||
import 'package:flutter_template/data/api/api_config.dart'; | ||
import 'package:flutter_template/data/interceptor/auth_interceptor.dart'; | ||
import 'package:flutter_template/data/services/http_client/dio_http_client.dart'; | ||
import 'package:flutter_template/injection/injector.dart'; | ||
import 'package:injectable/injectable.dart'; | ||
|
||
const dioAuth = 'AUTH'; | ||
const dioRegular = 'REGULAR'; | ||
|
||
Dio _getBaseDio(ApiConfig apiConfig) { | ||
final dio = Dio() | ||
..httpClientAdapter = DefaultHttpClientAdapter() | ||
..options.baseUrl = apiConfig.apiUrl; | ||
|
||
return dio; | ||
} | ||
|
||
@module | ||
abstract class NetworkModule { | ||
@singleton | ||
@Named(dioAuth) | ||
Dio getAuthDio(ApiConfig apiConfig) { | ||
final dio = _getBaseDio(apiConfig); | ||
return dio; | ||
} | ||
|
||
@singleton | ||
@Named(dioRegular) | ||
Dio getRegularDio(ApiConfig apiConfig) { | ||
final dio = _getBaseDio(apiConfig); | ||
|
||
final httpClient = DioHttpClient(dio); | ||
final refreshTokenHttpClient = DioHttpClient( | ||
injector.get<Dio>(instanceName: dioRegular), | ||
); | ||
|
||
dio.interceptors.add( | ||
AuthInterceptor( | ||
httpClient: httpClient, | ||
refreshTokenHttpClient: refreshTokenHttpClient, | ||
authPreferences: injector(), | ||
userPreferences: injector(), | ||
onTokenExpired: () { | ||
// TODO: Handle log out | ||
}, | ||
), | ||
); | ||
|
||
return dio; | ||
} | ||
} |