Skip to content

Commit

Permalink
chore(ml-opensource#80): Configure Dios
Browse files Browse the repository at this point in the history
  • Loading branch information
nivisi committed Jan 3, 2023
1 parent 893f0be commit d80d25f
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions lib/injection/network_module.dart
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;
}
}

0 comments on commit d80d25f

Please sign in to comment.