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 {
if (addCorrelationHeaders) {
final correlationHeaders =
await RequestTracker.getServerCorrelationHeaders();
final headers =
correlationHeaders.map((key, value) => MapEntry(key, value.first));
request.headers.addAll(headers);
}
final urlString = request.url.toString();
final requestTracker = await RequestTracker.create(urlString);
tracker = requestTracker;
return _httpClient.send(request).then((response) async {
final headers = response.request?.headers ?? request.headers;
final requestHeaders = headers.map((k, v) => MapEntry(k, <String>[v]));
final responseHeaders =
response.headers.map((k, v) => MapEntry(k, <String>[v]));
await requestTracker.setRequestHeaders(requestHeaders);
await requestTracker.setResponseStatusCode(response.statusCode);
await requestTracker.setResponseHeaders(responseHeaders);
return response;
}, onError: (e, StackTrace stacktrace) async {
await requestTracker.setError(e.toString(), stacktrace.toString());
throw e;
}).whenComplete(() async {
await requestTracker.reportDone();
});
}