Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions example/lib/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,26 @@ class LoggerInterceptor extends InterceptorContract {
}
return response;
}

@override
void interceptError({
BaseRequest? request,
BaseResponse? response,
Exception? error,
StackTrace? stackTrace,
}) {
log('----- Error -----');
if (request != null) {
log('Request: ${request.toString()}');
}
if (response != null) {
log('Response: ${response.toString()}');
}
if (error != null) {
log('Error: ${error.toString()}');
}
if (stackTrace != null) {
log('StackTrace: $stackTrace');
}
}
}
73 changes: 35 additions & 38 deletions lib/extensions/base_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,39 @@ extension BaseRequestCopyWith on BaseRequest {
List<MultipartFile>? files,
// StreamedRequest only properties.
Stream<List<int>>? stream,
}) {
if (this is Request) {
return RequestCopyWith(this as Request).copyWith(
method: method,
url: url,
headers: headers,
body: body,
encoding: encoding,
followRedirects: followRedirects,
maxRedirects: maxRedirects,
persistentConnection: persistentConnection,
);
} else if (this is StreamedRequest) {
return StreamedRequestCopyWith(this as StreamedRequest).copyWith(
method: method,
url: url,
headers: headers,
stream: stream,
followRedirects: followRedirects,
maxRedirects: maxRedirects,
persistentConnection: persistentConnection,
);
} else if (this is MultipartRequest) {
return MultipartRequestCopyWith(this as MultipartRequest).copyWith(
method: method,
url: url,
headers: headers,
fields: fields,
files: files,
followRedirects: followRedirects,
maxRedirects: maxRedirects,
persistentConnection: persistentConnection,
);
}

throw UnsupportedError(
'Cannot copy unsupported type of request $runtimeType');
}
}) =>
switch (this) {
Request req => req.copyWith(
method: method,
url: url,
headers: headers,
body: body,
encoding: encoding,
followRedirects: followRedirects,
maxRedirects: maxRedirects,
persistentConnection: persistentConnection,
),
StreamedRequest req => req.copyWith(
method: method,
url: url,
headers: headers,
stream: stream,
followRedirects: followRedirects,
maxRedirects: maxRedirects,
persistentConnection: persistentConnection,
),
MultipartRequest req => req.copyWith(
method: method,
url: url,
headers: headers,
fields: fields,
files: files,
followRedirects: followRedirects,
maxRedirects: maxRedirects,
persistentConnection: persistentConnection,
),
_ => throw UnsupportedError(
'Cannot copy unsupported type of request $runtimeType',
),
};
}
75 changes: 36 additions & 39 deletions lib/extensions/base_response_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,40 @@ extension BaseResponseCopyWith on BaseResponse {
int? contentLength,
// `IOStreamedResponse` only properties.
HttpClientResponse? inner,
}) {
if (this is Response) {
return ResponseCopyWith(this as Response).copyWith(
statusCode: statusCode,
body: body,
request: request,
headers: headers,
isRedirect: isRedirect,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase,
);
} else if (this is StreamedResponse) {
return StreamedResponseCopyWith(this as StreamedResponse).copyWith(
stream: stream,
statusCode: statusCode,
contentLength: contentLength,
request: request,
headers: headers,
isRedirect: isRedirect,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase,
);
} else if (this is IOStreamedResponse) {
return IOStreamedResponseCopyWith(this as IOStreamedResponse).copyWith(
stream: stream,
statusCode: statusCode,
contentLength: contentLength,
request: request,
headers: headers,
isRedirect: isRedirect,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase,
inner: inner,
);
}

throw UnsupportedError(
'Cannot copy unsupported type of response $runtimeType');
}
}) =>
switch (this) {
Response res => res.copyWith(
statusCode: statusCode,
body: body,
request: request,
headers: headers,
isRedirect: isRedirect,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase,
),
IOStreamedResponse res => res.copyWith(
stream: stream,
statusCode: statusCode,
contentLength: contentLength,
request: request,
headers: headers,
isRedirect: isRedirect,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase,
inner: inner,
),
StreamedResponse res => res.copyWith(
stream: stream,
statusCode: statusCode,
contentLength: contentLength,
request: request,
headers: headers,
isRedirect: isRedirect,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase,
),
_ => throw UnsupportedError(
'Cannot copy unsupported type of response $runtimeType',
),
};
}
52 changes: 25 additions & 27 deletions lib/extensions/base_response_none.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,29 @@ extension BaseResponseCopyWith on BaseResponse {
// `StreamedResponse` only properties.
Stream<List<int>>? stream,
int? contentLength,
}) {
if (this is Response) {
return ResponseCopyWith(this as Response).copyWith(
statusCode: statusCode,
body: body,
request: request,
headers: headers,
isRedirect: isRedirect,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase,
);
} else if (this is StreamedResponse) {
return StreamedResponseCopyWith(this as StreamedResponse).copyWith(
stream: stream,
statusCode: statusCode,
contentLength: contentLength,
request: request,
headers: headers,
isRedirect: isRedirect,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase,
);
}

throw UnsupportedError(
'Cannot copy unsupported type of response $runtimeType');
}
}) =>
switch (this) {
Response res => res.copyWith(
statusCode: statusCode,
body: body,
request: request,
headers: headers,
isRedirect: isRedirect,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase,
),
StreamedResponse res => res.copyWith(
stream: stream,
statusCode: statusCode,
contentLength: contentLength,
request: request,
headers: headers,
isRedirect: isRedirect,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase,
),
_ => throw UnsupportedError(
'Cannot copy unsupported type of response $runtimeType',
),
};
}
25 changes: 12 additions & 13 deletions lib/extensions/io_streamed_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ extension IOStreamedResponseCopyWith on IOStreamedResponse {
bool? persistentConnection,
String? reasonPhrase,
HttpClientResponse? inner,
}) {
return IOStreamedResponse(
stream ?? this.stream,
statusCode ?? this.statusCode,
contentLength: contentLength ?? this.contentLength,
request: request ?? this.request,
headers: headers ?? this.headers,
isRedirect: isRedirect ?? this.isRedirect,
persistentConnection: persistentConnection ?? this.persistentConnection,
reasonPhrase: reasonPhrase ?? this.reasonPhrase,
inner: inner,
);
}
}) =>
IOStreamedResponse(
stream ?? this.stream,
statusCode ?? this.statusCode,
contentLength: contentLength ?? this.contentLength,
request: request ?? this.request,
headers: headers ?? this.headers,
isRedirect: isRedirect ?? this.isRedirect,
persistentConnection: persistentConnection ?? this.persistentConnection,
reasonPhrase: reasonPhrase ?? this.reasonPhrase,
inner: inner,
);
}
4 changes: 2 additions & 2 deletions lib/extensions/multipart_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ extension MultipartRequestCopyWith on MultipartRequest {
int? maxRedirects,
bool? persistentConnection,
}) {
var clonedRequest =
final MultipartRequest clonedRequest =
MultipartRequest(method?.asString ?? this.method, url ?? this.url)
..headers.addAll(headers ?? this.headers)
..fields.addAll(fields ?? this.fields);

for (var file in this.files) {
for (final MultipartFile file in this.files) {
clonedRequest.files.add(MultipartFile(
file.field,
file.finalize(),
Expand Down
2 changes: 1 addition & 1 deletion lib/extensions/request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension RequestCopyWith on Request {
int? maxRedirects,
bool? persistentConnection,
}) {
final copied = Request(
final Request copied = Request(
method?.asString ?? this.method,
url ?? this.url,
)..bodyBytes = this.bodyBytes;
Expand Down
13 changes: 6 additions & 7 deletions lib/extensions/streamed_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@ extension StreamedRequestCopyWith on StreamedRequest {
bool? persistentConnection,
}) {
// Create a new StreamedRequest with the same method and URL
var clonedRequest =
final StreamedRequest clonedRequest =
StreamedRequest(method?.asString ?? this.method, url ?? this.url)
..headers.addAll(headers ?? this.headers);

// Use a broadcast stream to allow multiple listeners
var broadcastStream =
final Stream<List<int>> broadcastStream =
stream?.asBroadcastStream() ?? finalize().asBroadcastStream();

// Pipe the broadcast stream into the cloned request's sink
broadcastStream.listen((data) {
clonedRequest.sink.add(data);
}, onDone: () {
clonedRequest.sink.close();
});
broadcastStream.listen(
(List<int> data) => clonedRequest.sink.add(data),
onDone: () => clonedRequest.sink.close(),
);

this.persistentConnection =
persistentConnection ?? this.persistentConnection;
Expand Down
44 changes: 18 additions & 26 deletions lib/extensions/uri.dart
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
import 'package:http_interceptor/extensions/string.dart';
import 'package:http_interceptor/utils/utils.dart';

/// Extends `Uri` to allow adding parameters to already created intstances
/// Extends `Uri` to allow adding parameters to already created instances.
extension AddParameters on Uri {
/// Returns a new `Uri` instance based on `this` and adds [parameters].
Uri addParameters(Map<String, dynamic>? parameters) {
if (parameters == null) return this;

String paramUrl = origin + path;

Map<String, dynamic> newParameters = {};

queryParametersAll.forEach((key, values) {
newParameters[key] = values;
});

parameters.forEach((key, value) {
newParameters[key] = value;
});

String finalUrl = buildUrlString(paramUrl, newParameters);

// Preserve the fragment if it exists
if (fragment.isNotEmpty) {
finalUrl += '#$fragment';
}

return finalUrl.toUri();
}
/// Returns a new [Uri] instance based on `this` and adds [parameters].
Uri addParameters([Map<String, dynamic>? parameters]) =>
parameters?.isNotEmpty ?? false
? (StringBuffer()
..writeAll([
buildUrlString(
"$origin$path",
{
...queryParametersAll,
...?parameters,
},
),
if (fragment.isNotEmpty) '#$fragment',
]))
.toString()
.toUri()
: this;
}
Loading
Loading