postApi method

Future<ActiveResponse> postApi(
  1. Params params,
  2. RequestSetUp setUp, {
  3. String savedResponseName = '',
  4. bool saveResponse = false,
})

Implementation

Future<ActiveResponse> postApi(
    Params params,
    RequestSetUp setUp, {
      String savedResponseName = '',
      bool saveResponse = false,
    }) async {
  final endpoint = Uri.parse(params.endpoint);

  Uri uri = Uri(
    scheme: endpoint.scheme,
    queryParameters: params.queryParameters,
    path: endpoint.path,
    host: endpoint.host,
  );

  final io.HttpClient apiRequest = _httpClientSetup(
      setUp.idleTimeout!,
      setUp.withTrustedRoots!,
      setUp.connectionTimeout!,
      setUp.privateKeyPath,
      setUp.privateKeyPassword);

  final io.HttpClientRequest request = await apiRequest.postUrl(uri);
  setUp.httpHeaders?.forEach((String name, String value) {
    request.headers.add(name, value);
  });

  final io.HttpClientResponse response = await request.close();
  if (response.statusCode != io.HttpStatus.ok) {
    // The network may be only temporarily unavailable
    ActiveResponse activeResponse = ActiveResponse(
        statusCode: response.statusCode,
        endpoint: params.endpoint,
        errors: response.toString(),
        data: await response.transform(utf8.decoder).join());
    // await response.drain<List<int>>(<int>[]);
    return activeResponse;

  }

  ActiveResponse activeResponse = ActiveResponse(
      statusCode: response.statusCode,
      errors: null,
      data: await response.transform(utf8.decoder).join(),
      endpoint: params.endpoint);

  if (setUp.logResponse == true) {
    await _logApiRequests(activeResponse);
  }

  if (saveResponse == true) {
    savedResponseName =
    savedResponseName == '' ? uri.path : savedResponseName;
    await _saveApiRequests(activeResponse, savedResponseName);
  }

  return activeResponse;
}