send method

  1. @override
Future<StreamedResponse> send(
  1. BaseRequest request
)

Sends an HTTP request and asynchronously returns the response.

Implementers should call BaseRequest.finalize to get the body of the request as a ByteStream. They shouldn't make any assumptions about the state of the stream; it could have data written to it asynchronously at a later point, or it could already be closed when it's returned. Any internal HTTP errors should be wrapped as ClientExceptions.

Implementation

@override
Future<http.StreamedResponse> send(http.BaseRequest request) async {
  final id = CoreLogger.generateId();
  final startTime = DateTime.now().millisecondsSinceEpoch;

  CoreLogger.log(
    id: id,
    type: LogType.request,
    message: '${request.method} ${request.url}',
  );

  try {
    final response = await _inner.send(request);
    final duration = DateTime.now().millisecondsSinceEpoch - startTime;
    final bytes = await response.stream.toBytes();
    final responseBody = utf8.decode(bytes, allowMalformed: true);

    CoreLogger.log(
      id: id,
      type: LogType.response,
      message: 'Response received',
      statusCode: response.statusCode,
      durationMs: duration,
      data: responseBody,
    );

    return http.StreamedResponse(
      http.ByteStream.fromBytes(bytes),
      response.statusCode,
      contentLength: response.contentLength,
      request: response.request,
      headers: response.headers,
      isRedirect: response.isRedirect,
      persistentConnection: response.persistentConnection,
      reasonPhrase: response.reasonPhrase,
    );
  } catch (e) {
    CoreLogger.log(
      id: id,
      type: LogType.error,
      message: e.toString(),
    );
    rethrow;
  }
}