Skip to content

Commit 0abdd45

Browse files
feat: pass request into conditional methods (#160)
* fix: update README for v2.0.0 * feat: pass request to conditional methods * fix: update formatting interceptor_contract.dart * fix: update intercepted_client.dart * fix: formatting update interceptor_contract.dart --------- Co-authored-by: Alejandro Ulate <[email protected]> Co-authored-by: Alejandro Ulate <[email protected]>
1 parent ecbada1 commit 0abdd45

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

README.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This is a plugin that lets you intercept the different requests and responses fr
2020

2121
**Already using `http_interceptor`? Check out the [1.0.0 migration guide](./guides/migration_guide_1.0.0.md) for quick reference on the changes made and how to migrate your code.**
2222

23-
- [http_interceptor](#http_interceptor)
23+
- [http\_interceptor](#http_interceptor)
2424
- [Quick Reference](#quick-reference)
2525
- [Installation](#installation)
2626
- [Features](#features)
@@ -67,9 +67,16 @@ import 'package:http_interceptor/http_interceptor.dart';
6767

6868
### Building your own interceptor
6969

70-
In order to implement `http_interceptor` you need to implement the `InterceptorContract` and create your own interceptor. This abstract class has two methods: `interceptRequest`, which triggers before the http request is called; and `interceptResponse`, which triggers after the request is called, it has a response attached to it which the corresponding to said request. You could use this to do logging, adding headers, error handling, or many other cool stuff. It is important to note that after you proccess the request/response objects you need to return them so that `http` can continue the execute.
70+
In order to implement `http_interceptor` you need to implement the `InterceptorContract` and create your own interceptor. This abstract class has four methods:
7171

72-
`interceptRequest` and `interceptResponse` use `FutureOr` syntax, which makes it easier to support both synchronous and asynchronous behaviors.
72+
- `interceptRequest`, which triggers before the http request is called
73+
- `interceptResponse`, which triggers after the request is called, it has a response attached to it which the corresponding to said request;
74+
75+
- `shouldInterceptRequest` and `shouldInterceptResponse`, which are used to determine if the request or response should be intercepted or not. These two methods are optional as they return `true` by default, but they can be useful if you want to conditionally intercept requests or responses based on certain criteria.
76+
77+
You could use this package to do logging, adding headers, error handling, or many other cool stuff. It is important to note that after you proccess the request/response objects you need to return them so that `http` can continue the execute.
78+
79+
All four methods use `FutureOr` syntax, which makes it easier to support both synchronous and asynchronous behaviors.
7380

7481
- Logging with interceptor:
7582

@@ -120,6 +127,18 @@ class WeatherApiInterceptor implements InterceptorContract {
120127
required BaseResponse response,
121128
}) =>
122129
response;
130+
131+
@override
132+
FutureOr<bool> shouldInterceptRequest({required BaseRequest request}) async {
133+
// You can conditionally intercept requests here
134+
return true; // Intercept all requests
135+
}
136+
137+
@override
138+
FutureOr<bool> shouldInterceptResponse({required BaseResponse response}) async {
139+
// You can conditionally intercept responses here
140+
return true; // Intercept all responses
141+
}
123142
}
124143
```
125144

@@ -144,6 +163,18 @@ class MultipartRequestInterceptor implements InterceptorContract {
144163
}
145164
return response;
146165
}
166+
167+
@override
168+
FutureOr<bool> shouldInterceptRequest({required BaseRequest request}) async {
169+
// You can conditionally intercept requests here
170+
return true; // Intercept all requests
171+
}
172+
173+
@override
174+
FutureOr<bool> shouldInterceptResponse({required BaseResponse response}) async {
175+
// You can conditionally intercept responses here
176+
return true; // Intercept all responses
177+
}
147178
}
148179
```
149180

lib/http/intercepted_client.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,9 @@ class InterceptedClient extends BaseClient {
388388
Future<BaseRequest> _interceptRequest(BaseRequest request) async {
389389
BaseRequest interceptedRequest = request.copyWith();
390390
for (InterceptorContract interceptor in interceptors) {
391-
if (await interceptor.shouldInterceptRequest()) {
391+
if (await interceptor.shouldInterceptRequest(
392+
request: interceptedRequest,
393+
)) {
392394
interceptedRequest = await interceptor.interceptRequest(
393395
request: interceptedRequest,
394396
);
@@ -402,7 +404,9 @@ class InterceptedClient extends BaseClient {
402404
Future<BaseResponse> _interceptResponse(BaseResponse response) async {
403405
BaseResponse interceptedResponse = response;
404406
for (InterceptorContract interceptor in interceptors) {
405-
if (await interceptor.shouldInterceptResponse()) {
407+
if (await interceptor.shouldInterceptResponse(
408+
response: interceptedResponse,
409+
)) {
406410
interceptedResponse = await interceptor.interceptResponse(
407411
response: interceptedResponse,
408412
);

lib/models/interceptor_contract.dart

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,21 @@ import 'package:http/http.dart';
2828
///}
2929
///```
3030
abstract class InterceptorContract {
31-
FutureOr<bool> shouldInterceptRequest() => true;
31+
FutureOr<bool> shouldInterceptRequest({
32+
required BaseRequest request,
33+
}) =>
34+
true;
3235

33-
FutureOr<BaseRequest> interceptRequest({required BaseRequest request});
36+
FutureOr<BaseRequest> interceptRequest({
37+
required BaseRequest request,
38+
});
3439

35-
FutureOr<bool> shouldInterceptResponse() => true;
40+
FutureOr<bool> shouldInterceptResponse({
41+
required BaseResponse response,
42+
}) =>
43+
true;
3644

37-
FutureOr<BaseResponse> interceptResponse({required BaseResponse response});
45+
FutureOr<BaseResponse> interceptResponse({
46+
required BaseResponse response,
47+
});
3848
}

0 commit comments

Comments
 (0)