diff --git a/README.md b/README.md index cb501cc..e5c0e76 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ This is a plugin that lets you intercept the different requests and responses fr **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.** -- [http_interceptor](#http_interceptor) +- [http\_interceptor](#http_interceptor) - [Quick Reference](#quick-reference) - [Installation](#installation) - [Features](#features) @@ -67,9 +67,16 @@ import 'package:http_interceptor/http_interceptor.dart'; ### Building your own interceptor -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. +In order to implement `http_interceptor` you need to implement the `InterceptorContract` and create your own interceptor. This abstract class has four methods: -`interceptRequest` and `interceptResponse` use `FutureOr` syntax, which makes it easier to support both synchronous and asynchronous behaviors. + - `interceptRequest`, which triggers before the http request is called + - `interceptResponse`, which triggers after the request is called, it has a response attached to it which the corresponding to said request; + +- `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. + +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. + +All four methods use `FutureOr` syntax, which makes it easier to support both synchronous and asynchronous behaviors. - Logging with interceptor: @@ -120,6 +127,18 @@ class WeatherApiInterceptor implements InterceptorContract { required BaseResponse response, }) => response; + + @override + FutureOr shouldInterceptRequest({required BaseRequest request}) async { + // You can conditionally intercept requests here + return true; // Intercept all requests + } + + @override + FutureOr shouldInterceptResponse({required BaseResponse response}) async { + // You can conditionally intercept responses here + return true; // Intercept all responses + } } ``` @@ -144,6 +163,18 @@ class MultipartRequestInterceptor implements InterceptorContract { } return response; } + + @override + FutureOr shouldInterceptRequest({required BaseRequest request}) async { + // You can conditionally intercept requests here + return true; // Intercept all requests + } + + @override + FutureOr shouldInterceptResponse({required BaseResponse response}) async { + // You can conditionally intercept responses here + return true; // Intercept all responses + } } ``` diff --git a/lib/http/intercepted_client.dart b/lib/http/intercepted_client.dart index 1f19856..1627c8a 100644 --- a/lib/http/intercepted_client.dart +++ b/lib/http/intercepted_client.dart @@ -388,7 +388,9 @@ class InterceptedClient extends BaseClient { Future _interceptRequest(BaseRequest request) async { BaseRequest interceptedRequest = request.copyWith(); for (InterceptorContract interceptor in interceptors) { - if (await interceptor.shouldInterceptRequest()) { + if (await interceptor.shouldInterceptRequest( + request: interceptedRequest, + )) { interceptedRequest = await interceptor.interceptRequest( request: interceptedRequest, ); @@ -402,7 +404,9 @@ class InterceptedClient extends BaseClient { Future _interceptResponse(BaseResponse response) async { BaseResponse interceptedResponse = response; for (InterceptorContract interceptor in interceptors) { - if (await interceptor.shouldInterceptResponse()) { + if (await interceptor.shouldInterceptResponse( + response: interceptedResponse, + )) { interceptedResponse = await interceptor.interceptResponse( response: interceptedResponse, ); diff --git a/lib/models/interceptor_contract.dart b/lib/models/interceptor_contract.dart index 6da1aa9..8929635 100644 --- a/lib/models/interceptor_contract.dart +++ b/lib/models/interceptor_contract.dart @@ -28,11 +28,21 @@ import 'package:http/http.dart'; ///} ///``` abstract class InterceptorContract { - FutureOr shouldInterceptRequest() => true; + FutureOr shouldInterceptRequest({ + required BaseRequest request, + }) => + true; - FutureOr interceptRequest({required BaseRequest request}); + FutureOr interceptRequest({ + required BaseRequest request, + }); - FutureOr shouldInterceptResponse() => true; + FutureOr shouldInterceptResponse({ + required BaseResponse response, + }) => + true; - FutureOr interceptResponse({required BaseResponse response}); + FutureOr interceptResponse({ + required BaseResponse response, + }); }