|
1 | 1 | # http_interceptor |
2 | 2 |
|
3 | | -A new flutter plugin project. |
| 3 | +A middleware library that lets you modify requests and responses if desired. Based of on (http_middleware)[https://github.com/TEDConsulting/http_middleware] |
4 | 4 |
|
5 | 5 | ## Getting Started |
6 | 6 |
|
7 | | -This project is a starting point for a Flutter |
8 | | -[plug-in package](https://flutter.io/developing-packages/), |
9 | | -a specialized package that includes platform-specific implementation code for |
10 | | -Android and/or iOS. |
| 7 | +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. |
11 | 8 |
|
12 | | -For help getting started with Flutter, view our |
13 | | -[online documentation](https://flutter.io/docs), which offers tutorials, |
14 | | -samples, guidance on mobile development, and a full API reference. |
| 9 | +### Installing |
| 10 | + |
| 11 | +Include the package with the latest version available in your `pubspec.yaml`. |
| 12 | + |
| 13 | +```dart |
| 14 | + http_interceptor: any |
| 15 | +``` |
| 16 | + |
| 17 | +### Importing |
| 18 | + |
| 19 | +```dart |
| 20 | +import 'package:http_interceptor/http_interceptor.dart'; |
| 21 | +``` |
| 22 | + |
| 23 | +### Using `http_interceptor` |
| 24 | + |
| 25 | +#### Building your own interceptor |
| 26 | + |
| 27 | +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. |
| 28 | + |
| 29 | +- Logging with interceptor: |
| 30 | + |
| 31 | +```dart |
| 32 | +class LogginInterceptor implements InterceptorContract { |
| 33 | + @override |
| 34 | + Future<RequestData> interceptRequest({RequestData data}) async { |
| 35 | + print(data); |
| 36 | + return data; |
| 37 | + } |
| 38 | +
|
| 39 | + @override |
| 40 | + Future<ResponseData> interceptResponse({ResponseData data}) async { |
| 41 | + print(data); |
| 42 | + return data; |
| 43 | + } |
| 44 | +
|
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +- Changing headers with interceptor: |
| 49 | + |
| 50 | +```dart |
| 51 | +class WeatherApiInterceptor implements InterceptorContract { |
| 52 | + @override |
| 53 | + Future<RequestData> interceptRequest({RequestData data}) async { |
| 54 | + try { |
| 55 | + data.url = "${data.url}&appid=$OPEN_WEATHER_API_KEY"; |
| 56 | + data.url = "${data.url}&units=metric"; |
| 57 | + data.headers["Content-Type"] = "application/json"; |
| 58 | + } catch (e) { |
| 59 | + print(e); |
| 60 | + } |
| 61 | + return data; |
| 62 | + } |
| 63 | +
|
| 64 | + @override |
| 65 | + Future<ResponseData> interceptResponse({ResponseData data}) async => data; |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +#### Using your interceptor |
| 70 | + |
| 71 | +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. ;) |
| 72 | + |
| 73 | +##### Using interceptors with Client |
| 74 | + |
| 75 | +Normally, this approach is taken because of its ability to be tested and mocked. |
| 76 | + |
| 77 | +Here is an example with a repository using the `HttpClientWithInterceptor` class. |
| 78 | + |
| 79 | +```dart |
| 80 | +class WeatherRepository { |
| 81 | + Client client = HttpClientWithInterceptor.build(interceptors: [ |
| 82 | + WeatherApiInterceptor(), |
| 83 | + ]); |
| 84 | +
|
| 85 | + Future<Map<String, dynamic>> fetchCityWeather(int id) async { |
| 86 | + var parsedWeather; |
| 87 | + try { |
| 88 | + final response = await client.get("$baseUrl/weather?id=$id"); |
| 89 | + if (response.statusCode == 200) { |
| 90 | + parsedWeather = json.decode(response.body); |
| 91 | + } else { |
| 92 | + throw Exception("Error while fetching. \n ${response.body}"); |
| 93 | + } |
| 94 | + } catch (e) { |
| 95 | + print(e); |
| 96 | + } |
| 97 | + return parsedWeather; |
| 98 | + } |
| 99 | +
|
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +##### Using interceptors without Client |
| 104 | + |
| 105 | +This is mostly the straight forward approach for a one-and-only call that you might need intercepted. |
| 106 | + |
| 107 | +Here is an example with a repository using the `HttpWithInterceptor` class. |
| 108 | + |
| 109 | +```dart |
| 110 | +class WeatherRepository { |
| 111 | +
|
| 112 | +
|
| 113 | + Future<Map<String, dynamic>> fetchCityWeather(int id) async { |
| 114 | + HttpWithInterceptor http = HttpWithInterceptor.build( |
| 115 | + interceptors: [WeatherApiInterceptor()]); |
| 116 | +
|
| 117 | + var parsedWeather; |
| 118 | + try { |
| 119 | + var response = await http.get("$baseUrl/weather?id=$id"); |
| 120 | +
|
| 121 | + if (response.statusCode == 200) { |
| 122 | + parsedWeather = json.decode(response.body); |
| 123 | + } else { |
| 124 | + throw Exception("Error while fetching. \n ${response.body}"); |
| 125 | + } |
| 126 | + } catch (e) { |
| 127 | + print(e); |
| 128 | + } |
| 129 | + return parsedWeather; |
| 130 | + } |
| 131 | +
|
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +### Need help? |
| 136 | +Open an issue and tell me, I will be happy to help you out as soon as I can. |
0 commit comments