interceptWithChuck method

Future<HttpClientResponse> interceptWithChuck(
  1. Chuck chuck, {
  2. Object? body,
  3. Map<String, dynamic>? headers,
})

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

Implementation

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