send method
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 ClientException
s.
Implementation
@override
Future<StreamedResponse> send(BaseRequest request) async {
final timestamp = DateTime.now().toUtc();
final stopwatch = Stopwatch()..start();
final response = await inner.send(request);
stopwatch.stop();
final contentLength = request.contentLength;
final appendHeader = this.appendHeader ?? (_) => true;
final headers = request.headers.entries
.where((e) => appendHeader(e.key))
.map((e) => '${e.key}=${e.value}')
.join(',');
telemetryClient.trackRequest(
id: _generateRequestId(),
url: request.url.toString(),
duration: stopwatch.elapsed,
responseCode: response.statusCode.toString(),
success: response.statusCode >= 200 && response.statusCode < 300,
additionalProperties: <String, Object>{
'method': request.method,
if (headers.isNotEmpty) 'headers': headers,
if (contentLength != null) 'contentLength': contentLength,
},
timestamp: timestamp,
);
return response;
}