initSession function

Future<InitSessionResponse> initSession(
  1. String providerId,
  2. String appId,
  3. String timestamp,
  4. String signature,
)

Initializes a session with the provided parameters @param providerId - The ID of the provider @param appId - The ID of the application @param timestamp - The timestamp of the request @param signature - The signature for authentication @returns A Future that resolves to an InitSessionResponse @throws InitSessionError if the session initialization fails

Implementation

Future<InitSessionResponse> initSession(
  String providerId,
  String appId,
  String timestamp,
  String signature,
) async {
  logger
      .info('Initializing session for providerId: $providerId, appId: $appId');
  try {
    final response = await http.post(
      Uri.parse('${Constants.BACKEND_BASE_URL}/api/sdk/init-session/'),
      headers: {'Content-Type': 'application/json'},
      body: jsonEncode({
        'providerId': providerId,
        'appId': appId,
        'timestamp': timestamp,
        'signature': signature,
      }),
    );

    final res = jsonDecode(response.body);

    if (response.statusCode != 201) {
      logger.info(
          'Session initialization failed: ${res['message'] ?? 'Unknown error'}');
      throw initSessionError(res['message'] ??
          'Error initializing session with providerId: $providerId');
    }

    return InitSessionResponse(
      sessionId: res['sessionId'],
      provider: ProviderData.fromJson(res['provider']),
    );
  } catch (err) {
    logger.info({
      'message': 'Failed to initialize session',
      'providerId': providerId,
      'appId': appId,
      'timestamp': timestamp,
      'error': err.toString(),
    });
    rethrow;
  }
}