getAuthenticationToken method

Future<String> getAuthenticationToken(
  1. String apiKey
)

Get API authentication token

Implementation

Future<String> getAuthenticationToken(String apiKey) async {
  try {
    final response = await _apiService.post<Map<String, dynamic>>(
      ApiConstants.authorization,
      data: {"api_key": apiKey},
    );

    if (response.statusCode != null && response.statusCode! >= 200 && response.statusCode! < 300) {
      _authToken = response.data?["token"]?.toString();
      _apiService.setAuthToken(_authToken!);
      return _authToken!;
    } else {
      throw const InvalidApiKeyException();
    }
  } catch (e) {
    if (e is PaymobException) rethrow;
    throw const InvalidApiKeyException('Failed to authenticate with Paymob API');
  }
}