fetch method
Implement this method to make real HTTP requests.
options are the request options.
requestStream is the request stream. It will not be null only when
the request body is not empty.
Use requestStream if your code rely on RequestOptions.onSendProgress.
cancelFuture corresponds to CancelToken handling.
When the request is canceled, cancelFuture will be resolved.
To await if a request has been canceled:
cancelFuture?.then((_) => print('request cancelled!'));
Implementation
@override
Future<ResponseBody> fetch(
RequestOptions options,
Stream<Uint8List>? requestStream,
Future<void>? cancelFuture,
) async {
final recorded = RecordedRequest(
method: options.method,
path: options.path,
uri: options.uri,
headers: Map<String, Object?>.from(options.headers),
extra: Map<String, Object?>.from(options.extra),
query: Map<String, Object?>.from(options.queryParameters),
body: options.data,
);
requests.add(recorded);
final response = await _handler(recorded);
final delay = response.delay;
if (delay != null) {
await Future<void>.delayed(delay);
}
if (response.isNetworkError) {
throw DioException.connectionError(
requestOptions: options,
reason: 'стаб: связи нет',
);
}
final payload = switch (response.body) {
null => '',
final String s => s,
final Object other => jsonEncode(other),
};
return ResponseBody.fromString(
payload,
response.statusCode,
headers: {
Headers.contentTypeHeader: [Headers.jsonContentType],
...response.headers,
},
);
}