initialize static method
Initializes the API client's singleton with base URL, API keys and headers
Implementation
static Future<void> initialize({
///API key issued by Scrella to the partner
required String partnerApiKey,
}) async {
if (_initialized) return;
_dio = Dio(
BaseOptions(
baseUrl: partnerApiKey.startsWith("dev-")
? ScrellaConstants.baseUrlDev
: ScrellaConstants.baseUrlLive,
connectTimeout: const Duration(minutes: 3),
receiveTimeout: const Duration(minutes: 3),
headers: {
'X-Partner-Api': partnerApiKey,
'Content-Type': 'application/json',
'Accept': 'application/json',
},
),
);
// Load persisted token if it exists
final token = await TokenStorage.getToken();
if (token != null) {
_dio!.options.headers['Authorization'] = 'Bearer $token';
}
// Add interceptor to attach token automatically on every request
if (!_authInterceptorAdded) {
_dio!.interceptors.add(
InterceptorsWrapper(
onRequest: (options, handler) async {
final token = await TokenStorage.getToken();
if (token != null) {
options.headers['Authorization'] = 'Bearer $token';
} else {
options.headers.remove('Authorization');
}
handler.next(options);
},
),
);
_authInterceptorAdded = true;
}
_initialized = true;
}