ApiClient constructor

ApiClient({
  1. bool shouldUsePersistentUrl = false,
  2. bool shouldUseDeviceInfo = false,
  3. bool shouldUseLanguage = true,
  4. RefreshInterceptor? refreshInterceptor,
})

Creates an ApiClient instance with configurable interceptors.

Parameters:

  • shouldUsePersistentUrl: Enable dynamic base URL switching (default: false)
  • shouldUseDeviceInfo: Include device information headers (default: false)
  • shouldUseLanguage: Include user language headers (default: true)
  • refreshInterceptor: Custom token refresh interceptor (optional)

Interceptors Added (in order):

  1. DeviceInfoInterceptor (if enabled)
  2. LanguageInterceptor (if enabled)
  3. Cookie storage interceptor (native platforms only)
  4. Persistent URL interceptor (if enabled, native platforms only)
  5. TimezoneInterceptor (always added)
  6. GeneralErrorLogInterceptor (always added)
  7. RefreshInterceptor (always added)

Implementation

ApiClient({
  bool shouldUsePersistentUrl = false,
  bool shouldUseDeviceInfo = false,
  bool shouldUseLanguage = true,
  RefreshInterceptor? refreshInterceptor,
}) : dio = Dio() {
  if (refreshInterceptor != null) {
    this.refreshInterceptor = refreshInterceptor;
  }

  dio.options.baseUrl = baseUrl;

  if (shouldUseDeviceInfo) {
    dio.interceptors.add(DeviceInfoInterceptor());
  }

  if (shouldUseLanguage) {
    dio.interceptors.add(LanguageInterceptor());
  }

  dio.addCookieStorageInterceptor();
  if (shouldUsePersistentUrl) {
    dio.addBaseUrlInterceptor();
  }

  dio.interceptors
    ..add(TimezoneInterceptor())
    ..add(GeneralErrorLogInterceptor())
    ..add(this.refreshInterceptor);
}