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<http.StreamedResponse> send(http.BaseRequest request) async {
String ts = DateTime.now().toUtc().toIso8601String();
request = trace ? await _logRequest(ts, request) : request;
http.StreamedResponse response;
try {
final ex = forceException;
if (ex != null) {
forceException = null;
throw ex;
}
if (forceThrottleDelay != null) {
final delay = forceThrottleDelay!.inMilliseconds;
forceThrottleDelay = null;
response = http.StreamedResponse(
Stream<List<int>>.fromIterable([]),
HttpStatusCode.tooManyRequests,
reasonPhrase: 'Forced Throttle',
headers: {HttpHeader.msRetryAfterMs: delay.toString()},
);
} else if (forceForbidden) {
response = http.StreamedResponse(
Stream<List<int>>.fromIterable([]),
HttpStatusCode.forbidden,
reasonPhrase: 'Forced Forbidden',
);
} else {
response = await _http.send(request);
}
response = trace ? await _logResponse(ts, response) : response;
} catch (ex) {
if (trace) {
_logException(ts, ex);
}
rethrow;
}
return response;
}