init static method

Future<AutoPilotApi> init({
  1. required String baseUrl,
  2. String tokenType = 'Bearer',
  3. int timeoutSeconds = 30,
  4. bool? enableLogs,
  5. bool enableCache = false,
  6. Duration cacheDuration = const Duration(minutes: 5),
  7. Map<String, String> globalHeaders = const {},
  8. int maxRetries = 3,
  9. Duration retryDelay = const Duration(seconds: 1),
  10. bool enableTokenRefresh = false,
  11. Future<String?> onRefreshToken()?,
  12. bool enableGlobalLoader = false,
  13. void onLoadingChanged(
    1. bool loading
    )?,
  14. void onError(
    1. String message,
    2. int statusCode
    )?,
  15. void onRequestSent(
    1. String url,
    2. String method
    )?,
  16. void onResponseReceived(
    1. String url,
    2. int statusCode,
    3. Duration time
    )?,
  17. String successKey = 'status',
  18. dynamic successValue = true,
  19. String messageKey = 'message',
  20. String dataKey = 'data',
  21. bool enableDeduplication = true,
  22. bool printPayload = true,
  23. bool prettyPrint = true,
})

Initialize AutoPilot once at app startup.

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await AutoPilotApi.init(
    baseUrl            : "https://api.example.com/v1",
    enableLogs         : true,
    printPayload       : true,
    enableCache        : true,
    maxRetries         : 3,
    enableGlobalLoader : true,
    onLoadingChanged   : (v) => AppController.setLoading(v),
    onError            : (msg, code) => showSnackbar(msg),
    enableTokenRefresh : true,
    onRefreshToken     : () async => AuthService.refresh(),
  );
  runApp(const MyApp());
}

Implementation

static Future<AutoPilotApi> init({
  required String baseUrl,
  String tokenType                                           = 'Bearer',
  int timeoutSeconds                                         = 30,
  bool? enableLogs,
  bool enableCache                                           = false,
  Duration cacheDuration                                     = const Duration(minutes: 5),
  Map<String, String> globalHeaders                          = const {},
  int maxRetries                                             = 3,
  Duration retryDelay                                        = const Duration(seconds: 1),
  bool enableTokenRefresh                                    = false,
  Future<String?> Function()? onRefreshToken,
  bool enableGlobalLoader                                    = false,
  void Function(bool loading)? onLoadingChanged,
  void Function(String message, int statusCode)? onError,
  void Function(String url, String method)? onRequestSent,
  void Function(String url, int statusCode, Duration time)? onResponseReceived,
  // Response envelope keys — match your backend structure
  String successKey    = 'status',
  dynamic successValue = true,
  String messageKey    = 'message',
  String dataKey       = 'data',
  bool enableDeduplication = true,
  bool printPayload        = true,
  bool prettyPrint         = true,
}) async {
  final url = baseUrl.endsWith('/')
      ? baseUrl.substring(0, baseUrl.length - 1)
      : baseUrl;

  final cfg = AutoPilotConfig(
    baseUrl             : url,
    tokenType           : tokenType,
    timeoutSeconds      : timeoutSeconds,
    enableLogs          : enableLogs ?? kDebugMode,
    enableCache         : enableCache,
    cacheDuration       : cacheDuration,
    globalHeaders       : globalHeaders,
    maxRetries          : maxRetries,
    retryDelay          : retryDelay,
    enableTokenRefresh  : enableTokenRefresh,
    onRefreshToken      : onRefreshToken,
    enableGlobalLoader  : enableGlobalLoader,
    onLoadingChanged    : onLoadingChanged,
    onError             : onError,
    onRequestSent       : onRequestSent,
    onResponseReceived  : onResponseReceived,
    successKey          : successKey,
    successValue        : successValue,
    messageKey          : messageKey,
    dataKey             : dataKey,
    enableDeduplication : enableDeduplication,
    printPayload        : printPayload,
    prettyPrint         : prettyPrint,
  );

  AutoPilotLogger.configure(
    enabled      : cfg.enableLogs,
    printPayload : printPayload,
    prettyPrint  : prettyPrint,
  );

  final api = AutoPilotApi._();
  api._cfg  = cfg;
  _instance = api;

  AutoPilotLogger.logInfo('✅ AutoPilotApi initialized → $url');
  return api;
}