From 3cd905a5cbe13293da1de26a75afff0a86610ea6 Mon Sep 17 00:00:00 2001 From: Thinh TRUONG <75013885+td2thinh@users.noreply.github.com> Date: Wed, 11 Jun 2025 14:02:12 +0200 Subject: [PATCH 1/5] fix: update README for v2.0.0 --- README.md | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7db673f..1d28936 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 + } } ``` From 4b36b3cca0d983b31cb680930184da232baece0c Mon Sep 17 00:00:00 2001 From: Thinh TRUONG <75013885+td2thinh@users.noreply.github.com> Date: Wed, 11 Jun 2025 14:11:31 +0200 Subject: [PATCH 2/5] feat: pass request to conditional methods --- lib/http/intercepted_client.dart | 6 ++++-- lib/models/interceptor_contract.dart | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/http/intercepted_client.dart b/lib/http/intercepted_client.dart index 5afac3a..3b52e42 100644 --- a/lib/http/intercepted_client.dart +++ b/lib/http/intercepted_client.dart @@ -316,7 +316,8 @@ 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, ); @@ -330,7 +331,8 @@ 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..07a03b4 100644 --- a/lib/models/interceptor_contract.dart +++ b/lib/models/interceptor_contract.dart @@ -28,11 +28,11 @@ import 'package:http/http.dart'; ///} ///``` abstract class InterceptorContract { - FutureOr shouldInterceptRequest() => true; + FutureOr shouldInterceptRequest({required BaseRequest request}) => true; FutureOr interceptRequest({required BaseRequest request}); - FutureOr shouldInterceptResponse() => true; + FutureOr shouldInterceptResponse({required BaseResponse response}) => true; FutureOr interceptResponse({required BaseResponse response}); } From 0047a8262c4c641f236ab4ddd3f34697e0193f36 Mon Sep 17 00:00:00 2001 From: Alejandro Ulate Date: Fri, 18 Jul 2025 11:15:42 -0600 Subject: [PATCH 3/5] fix: update formatting interceptor_contract.dart --- lib/models/interceptor_contract.dart | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/models/interceptor_contract.dart b/lib/models/interceptor_contract.dart index 07a03b4..9ea31fd 100644 --- a/lib/models/interceptor_contract.dart +++ b/lib/models/interceptor_contract.dart @@ -28,11 +28,17 @@ import 'package:http/http.dart'; ///} ///``` abstract class InterceptorContract { - FutureOr shouldInterceptRequest({required BaseRequest request}) => true; + FutureOr shouldInterceptRequest({ + required BaseRequest request, + }) => + true; - FutureOr interceptRequest({required BaseRequest request}); + FutureOr interceptRequest({ + required BaseRequest request, + }); - FutureOr shouldInterceptResponse({required BaseResponse response}) => true; - - FutureOr interceptResponse({required BaseResponse response}); + FutureOr shouldInterceptResponse({ + required BaseResponse response, + }) => + true; } From 2a5241219adf32724fa3cc3d27ac0d8a0b6c711e Mon Sep 17 00:00:00 2001 From: Alejandro Ulate Date: Fri, 18 Jul 2025 11:18:01 -0600 Subject: [PATCH 4/5] fix: update intercepted_client.dart --- lib/http/intercepted_client.dart | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/http/intercepted_client.dart b/lib/http/intercepted_client.dart index 74f8622..1627c8a 100644 --- a/lib/http/intercepted_client.dart +++ b/lib/http/intercepted_client.dart @@ -389,7 +389,8 @@ class InterceptedClient extends BaseClient { BaseRequest interceptedRequest = request.copyWith(); for (InterceptorContract interceptor in interceptors) { if (await interceptor.shouldInterceptRequest( - request: interceptedRequest)) { + request: interceptedRequest, + )) { interceptedRequest = await interceptor.interceptRequest( request: interceptedRequest, ); @@ -404,7 +405,8 @@ class InterceptedClient extends BaseClient { BaseResponse interceptedResponse = response; for (InterceptorContract interceptor in interceptors) { if (await interceptor.shouldInterceptResponse( - response: interceptedResponse)) { + response: interceptedResponse, + )) { interceptedResponse = await interceptor.interceptResponse( response: interceptedResponse, ); From 7c972daaf1ec919d6171b0916c56a21c7b9a819b Mon Sep 17 00:00:00 2001 From: Alejandro Ulate Date: Fri, 18 Jul 2025 11:18:25 -0600 Subject: [PATCH 5/5] fix: formatting update interceptor_contract.dart --- lib/models/interceptor_contract.dart | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/models/interceptor_contract.dart b/lib/models/interceptor_contract.dart index 9ea31fd..8929635 100644 --- a/lib/models/interceptor_contract.dart +++ b/lib/models/interceptor_contract.dart @@ -41,4 +41,8 @@ abstract class InterceptorContract { required BaseResponse response, }) => true; + + FutureOr interceptResponse({ + required BaseResponse response, + }); }