init static method
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?> onRefreshToken()?, - bool enableGlobalLoader = false,
- void onLoadingChanged(
- bool loading
- void onError()?,
- void onRequestSent()?,
- void onResponseReceived()?,
- String successKey = 'status',
- dynamic successValue = true,
- String messageKey = 'message',
- String dataKey = 'data',
- bool enableDeduplication = true,
- bool printPayload = true,
- 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;
}