ApiClient constructor

ApiClient({
  1. required SharedPreferences sharedPreferences,
  2. String baseUrl = 'https://api.jusbid.com/v1',
})

Implementation

ApiClient({
  required this.sharedPreferences,
  String baseUrl = 'https://api.jusbid.com/v1',
}) {
  dio = Dio(
    BaseOptions(
      baseUrl: baseUrl,
      connectTimeout: const Duration(seconds: 15),
      receiveTimeout: const Duration(seconds: 15),
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      },
    ),
  );

  dio.interceptors.add(
    InterceptorsWrapper(
      onRequest: (options, handler) {
        final token = sharedPreferences.getString(AppConstants.keyAuthToken);
        if (token != null && token.isNotEmpty) {
          options.headers['Authorization'] = 'Bearer $token';
        }
        return handler.next(options);
      },
      onError: (DioException error, handler) {
        return handler.next(error);
      },
    ),
  );
}