configureNetworkService static method

Future<void> configureNetworkService({
  1. String baseURL = '',
  2. Duration connectTimeout = const Duration(seconds: 15),
  3. Duration receiveTimeout = const Duration(seconds: 25),
  4. String contentType = Headers.jsonContentType,
  5. Map<String, dynamic> headers = const {'Accept' : 'application/json'},
  6. String? pemCertificate,
  7. Uint8List? localCertificateBytes,
  8. bool isDevEnv = false,
  9. required String cacheEncryptionKey,
  10. int cacheVersion = 1,
  11. void onUnauthorized()?,
})

Initialises the network service. Must be called before any HTTP request.

Parameters:

  • baseURL: Base URL prepended to every endpoint.
  • connectTimeout: Max time to open a connection (default 15 s).
  • receiveTimeout: Max time to receive a response (default 25 s).
  • contentType: Default Content-Type (default application/json).
  • headers: Additional default headers for every request.
  • pemCertificate: PEM-encoded certificate string for SSL pinning.
  • localCertificateBytes: Raw certificate bytes for SSL pinning.
  • isDevEnv: Disables TLS certificate validation in development only.
  • cacheEncryptionKey: Required 32-byte key for AES-256 cache encryption.
  • cacheVersion: Hive box version (default 1). Increment this whenever the cache schema changes — the old box is abandoned and a fresh one is created automatically. No manual migration code required.
  • onUnauthorized: Global callback fired on every 401/403 response. Individual requests can override this with their own unAuthorizedCallBack.

Implementation

static Future<void> configureNetworkService({
  String baseURL = '',
  Duration connectTimeout = const Duration(seconds: 15),
  Duration receiveTimeout = const Duration(seconds: 25),
  String contentType = Headers.jsonContentType,
  Map<String, dynamic> headers = const {'Accept': 'application/json'},
  String? pemCertificate,
  Uint8List? localCertificateBytes,
  bool isDevEnv = false,
  required String cacheEncryptionKey,
  int cacheVersion = 1,
  void Function()? onUnauthorized,
}) async {
  _globalUnauthorizedCallback = onUnauthorized;

  await NetworkCacheService().initCacheService(
    encryptionKey: cacheEncryptionKey,
    cacheVersion: cacheVersion,
  );

  _apiManager = ApiManagerImpl();
  _apiRequest = ApiRequestImpl();

  _apiManager.initialize(
    baseURL: baseURL,
    headers: headers,
    connectTimeout: connectTimeout,
    contentType: contentType,
    isToEnableSSLCertificate:
        !isDevEnv && (pemCertificate != null || localCertificateBytes != null),
    localCertificateBytes: localCertificateBytes,
    pemCertificate: pemCertificate,
    receiveTimeout: receiveTimeout,
    isDevEnv: isDevEnv,
  );
}