interceptWithAlice method

Future<HttpClientResponse> interceptWithAlice(
  1. Alice alice, {
  2. dynamic body,
  3. Map<String, dynamic>? headers,
})

Intercept http client with alice. This extension method provides additional helpful method to intercept httpClientResponse.

Implementation

Future<HttpClientResponse> interceptWithAlice(Alice alice,
    {dynamic body, Map<String, dynamic>? headers}) async {
  HttpClientRequest request = await this;
  if (body != null) {
    request.write(body);
  }
  if (headers != null) {
    headers.forEach(
      (String key, dynamic value) {
        request.headers.add(key, value);
      },
    );
  }
  alice.onHttpClientRequest(request, body: body);
  var httpResponse = await request.close();
  var responseBody = await utf8.decoder.bind(httpResponse).join();
  alice.onHttpClientResponse(httpResponse, request, body: responseBody);
  return httpResponse;
}