request method

dynamic request(
  1. String operation,
  2. Map<String, dynamic> params, {
  3. String? endpoint,
  4. String? service,
})

Makes requests on AWS API service provider

Implementation

dynamic request(String operation, Map<String, dynamic> params,
    {String? endpoint, String? service}) async {
  final endpointReq = endpoint ?? this.endpoint;
  final targetService = service ?? _service;
  final body = json.encode(params);

  final headersReq = <String, String>{
    'Content-Type': 'application/x-amz-json-1.1',
    'X-Amz-Target': '$targetService.$operation',
    'X-Amz-User-Agent': _userAgent,
  };

  http.Response response;
  try {
    response = await _client.post(
      Uri.parse(endpointReq),
      headers: headersReq,
      body: body,
    );
  } catch (e) {
    if (e.toString().contains('Failed host lookup:')) {
      throw CognitoClientException(
        'SocketException',
        code: 'NetworkError',
      );
    }
    throw CognitoClientException('Unknown Error', code: 'Unknown error');
  }

  dynamic data;

  try {
    data = json.decode(utf8.decode(response.bodyBytes));
  } catch (_) {
    // expect json
  }

  if (response.statusCode < 200 || response.statusCode > 299) {
    var errorType = 'UnknownError';
    for (final header in response.headers.keys) {
      if (header.toLowerCase() == 'x-amzn-errortype') {
        errorType = response.headers[header]!.split(':')[0];
        break;
      }
    }
    if (data == null) {
      throw CognitoClientException(
        'Cognito client request error with unknown message',
        code: errorType,
        name: errorType,
        statusCode: response.statusCode,
      );
    }
    final String? dataType = data['__type'];
    final String? dataCode = data['code'];
    final code = (dataType ?? dataCode ?? errorType).split('#').removeLast();
    throw CognitoClientException(
      data['message'] ?? 'Cognito client request error with unknown message',
      code: code,
      name: code,
      statusCode: response.statusCode,
    );
  }
  return data;
}