You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[](https://github.com/codingalecr/http_interceptor)
8
8
9
-
A middleware library that lets you modify requests and responses if desired. Based of on [http_middleware](https://github.com/TEDConsulting/http_middleware)
9
+
This is a plugin that lets you intercept the different requests and responses from Dart's http package. You can use to add headers, modify query params, or print a log of the response.
10
10
11
-
## Getting Started
11
+
## Quick Reference
12
12
13
-
This is a plugin that lets you intercept the different requests and responses from Dart's http package. You can use to add headers, modify query params, or print a log of the response.
13
+
-[Installation](#installation)
14
+
-[Usage](#usage)
15
+
-[Building your own interceptor](#building-your-own-interceptor)
16
+
-[Using your interceptor](#using-your-interceptor)
17
+
-[Retrying requests](#retrying-requests)
18
+
-[Having trouble? Fill an issue](#troubleshooting)
14
19
15
-
### Installing
20
+
##Installation
16
21
17
22
Include the package with the latest version available in your `pubspec.yaml`.
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.
34
37
@@ -72,11 +75,11 @@ class WeatherApiInterceptor implements InterceptorContract {
72
75
}
73
76
```
74
77
75
-
####Using your interceptor
78
+
### Using your interceptor
76
79
77
80
Now that you actually have your interceptor implemented, now you need to use it. There are two general ways in which you can use them: by using the `HttpWithInterceptor` to do separate connections for different requests or using a `HttpClientWithInterceptor` for keeping a connection alive while making the different `http` calls. The ideal place to use them is in the service/provider class or the repository class (if you are not using services or providers); if you don't know about the repository pattern you can just google it and you'll know what I'm talking about. ;)
78
81
79
-
#####Using interceptors with Client
82
+
#### Using interceptors with Client
80
83
81
84
Normally, this approach is taken because of its ability to be tested and mocked.
82
85
@@ -107,7 +110,7 @@ class WeatherRepository {
107
110
}
108
111
```
109
112
110
-
#####Using interceptors without Client
113
+
#### Using interceptors without Client
111
114
112
115
This is mostly the straight forward approach for a one-and-only call that you might need intercepted.
113
116
@@ -137,6 +140,27 @@ class WeatherRepository {
137
140
}
138
141
```
139
142
140
-
### Issue Reporting
143
+
### Retrying requests
144
+
145
+
**(NEW 🎉)** Sometimes you need to retry a request due to different circumstances, an expired token is a really good example. Here's how you could potentially implement an expired token retry policy with `http_interceptor`.
146
+
147
+
```dart
148
+
class ExpiredTokenRetryPolicy extends RetryPolicy {
You can also set the maximum amount of retry attempts with `maxRetryAttempts` property or override the `shouldAttemptRetryOnException` if you want to retry the request after it failed with an exception.
163
+
164
+
## Troubleshooting
141
165
142
166
Open an issue and tell me, I will be happy to help you out as soon as I can.
0 commit comments