init method

void init({
  1. bool? usehttp,
  2. bool? showSnackbar,
  3. bool? showLogs,
  4. List<UnifiedInterceptor>? interceptors,
  5. String? baseUrl,
  6. Map<String, dynamic>? queryParameters,
  7. Duration? connectTimeout,
  8. Duration? receiveTimeout,
  9. Duration? sendTimeout,
  10. Map<String, Object?>? extra,
  11. Map<String, Object?>? headers,
  12. UnifiedResponseType? responseType,
  13. String? contentType,
  14. bool? followRedirects,
  15. int? maxRedirects,
  16. bool? persistentConnection,
  17. String? refreshTokenEndpoint,
  18. List<String>? refreshWhitelist,
  19. Future<void> onTokenRefreshed(
    1. Map<String, dynamic> newTokens
    )?,
  20. Map<String, dynamic> getRefreshTokenBody()?,
  21. VoidCallback? onLogout,
})

by default it will use http and show snackbar

Implementation

void init({
  bool? usehttp,
  bool? showSnackbar,
  bool? showLogs,
  List<UnifiedInterceptor>? interceptors,
  String? baseUrl,
  Map<String, dynamic>? queryParameters,
  Duration? connectTimeout,
  Duration? receiveTimeout,
  Duration? sendTimeout,
  Map<String, Object?>? extra,
  Map<String, Object?>? headers,
  UnifiedResponseType? responseType,
  String? contentType,
  bool? followRedirects,
  int? maxRedirects,
  bool? persistentConnection,
  String? refreshTokenEndpoint,
  List<String>? refreshWhitelist,
  Future<void> Function(Map<String, dynamic> newTokens)? onTokenRefreshed,
  Map<String, dynamic> Function()? getRefreshTokenBody,
  VoidCallback? onLogout,
}) {
  UnifiedHttpClient.useHttp = usehttp ?? true;
  UnifiedHttpClient.showSnackbar = showSnackbar ?? true;
  UnifiedHttpClient.showLogs = showLogs ?? false;
  UnifiedHttpClient.refreshTokenEndpoint = refreshTokenEndpoint;
  UnifiedHttpClient.refreshWhitelist = refreshWhitelist;
  UnifiedHttpClient.onTokenRefreshed = onTokenRefreshed;
  UnifiedHttpClient.getRefreshTokenBody = getRefreshTokenBody;
  UnifiedHttpClient.onLogout = onLogout;

  UnifiedHttpClient._interceptors = <UnifiedInterceptor>[
    ApiInterceptor(showLogs: UnifiedHttpClient.showLogs),
    ...?interceptors,
    NetworkLogInterceptor(),
  ];

  // Normalize and store default headers (shared by http & dio).
  // Values are converted to String to satisfy both clients.
  UnifiedHttpClient.defaultHeaders = {
    if (headers != null)
      for (final entry in headers.entries)
        if (entry.value != null) entry.key: entry.value.toString(),
  };

  // DIO Setup
  if (!useHttp) {
    PackageDio.addInterceptors(_interceptors);
    PackageDio.setBaseOptions(
      baseUrl: baseUrl,
      connectTimeout: connectTimeout,
      receiveTimeout: receiveTimeout,
      headers: headers,
      responseType: responseType,
      contentType: contentType,
      extra: extra,
      followRedirects: followRedirects,
      maxRedirects: maxRedirects,
      persistentConnection: persistentConnection,
      queryParameters: queryParameters,
      sendTimeout: sendTimeout,
    );
    PackageDio.setUpDio();
  }

  // HTTP Setup
  else {
    PackageHttp.setup(baseUrl: baseUrl ?? '');
    PackageHttp.configureInterceptors(_interceptors);
  }
}