Skip to content

Commit

Permalink
nit(ml-opensource#80): Update Config & NetworkModule
Browse files Browse the repository at this point in the history
  • Loading branch information
nivisi committed Jan 5, 2023
1 parent 1ca53d6 commit 96627e1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
2 changes: 2 additions & 0 deletions lib/injection/config_module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'package:flutter_template/presentation/app_flavor.dart';
import 'package:flutter_template/presentation/application_config.dart';

abstract class ConfigModule {
/// Can't use injectable with preresolve as it fails with a stack overflow exception
/// because here we depend on the flutter/services package.
static Future<void> inject(AppFlavor flavor) async {
const basePath = 'assets/configuration/application_configuration';
final configPath = '$basePath.${flavor.name}.json';
Expand Down
37 changes: 15 additions & 22 deletions lib/injection/network_module.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
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/api/new/authenticator/authenticator.dart';
import 'package:flutter_template/data/api/new/interceptor/api_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';
const authDioClient = 'AUTHDIOCLIENT';

Dio _getBaseDio(ApiConfig apiConfig) {
Dio _createBaseDio(ApiConfig apiConfig) {
final dio = Dio()
..httpClientAdapter = DefaultHttpClientAdapter()
..options.baseUrl = apiConfig.apiUrl;
Expand All @@ -19,34 +21,25 @@ Dio _getBaseDio(ApiConfig apiConfig) {

@module
abstract class NetworkModule {
@singleton
@Named(authDioClient)
DioHttpClient getAuthDioClient(ApiConfig apiConfig) {
final dio = _createBaseDio(apiConfig);
return DioHttpClient(dio);
}

@singleton
@Named(dioAuth)
Dio getAuthDio(ApiConfig apiConfig) {
final dio = _getBaseDio(apiConfig);
final dio = _createBaseDio(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
},
),
);
Dio getRegularDio(ApiConfig apiConfig, Authenticator authenticator) {
final dio = _createBaseDio(apiConfig)
..interceptors.add(injector<ApiAuthInterceptor>());

return dio;
}
Expand Down

0 comments on commit 96627e1

Please sign in to comment.