forked from CodingAleCR/http_interceptor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamed_request.dart
More file actions
39 lines (34 loc) · 1.41 KB
/
streamed_request.dart
File metadata and controls
39 lines (34 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import 'package:http/http.dart';
import 'package:http_interceptor/http/http_methods.dart';
/// Extends [StreamedRequest] to provide copied instances.
extension StreamedRequestCopyWith on StreamedRequest {
/// Creates a new instance of [StreamedRequest] based of on `this`. It copies
/// all the properties and overrides the ones sent via parameters.
StreamedRequest copyWith({
HttpMethod? method,
Uri? url,
Map<String, String>? headers,
Stream<List<int>>? stream,
bool? followRedirects,
int? maxRedirects,
bool? persistentConnection,
}) {
// Create a new StreamedRequest with the same method and URL
final StreamedRequest clonedRequest =
StreamedRequest(method?.asString ?? this.method, url ?? this.url)
..headers.addAll(headers ?? this.headers);
// Use a broadcast stream to allow multiple listeners
final Stream<List<int>> broadcastStream =
stream?.asBroadcastStream() ?? finalize().asBroadcastStream();
// Pipe the broadcast stream into the cloned request's sink
broadcastStream.listen(
(List<int> data) => clonedRequest.sink.add(data),
onDone: () => clonedRequest.sink.close(),
);
this.persistentConnection =
persistentConnection ?? this.persistentConnection;
this.followRedirects = followRedirects ?? this.followRedirects;
this.maxRedirects = maxRedirects ?? this.maxRedirects;
return clonedRequest;
}
}