Skip to content

Commit 459c1ba

Browse files
committed
Merge branch 'feature/demo' into develop
2 parents 24958ef + 6090bcb commit 459c1ba

16 files changed

+5835
-145
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
## 0.0.1
22

3-
* TODO: Describe initial release.
3+
* Added: Initial plugin implementation.
4+
* Added: Example of usage for the plugin.
5+
* Added: README.md and LICENSE files.

LICENSE

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1-
TODO: Add your license here.
1+
MIT License
2+
3+
Copyright (c) 2019 Alejandro Ulate Fallas (codingalecr)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 130 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,136 @@
11
# http_interceptor
22

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]
44

55
## Getting Started
66

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.
118

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.

example/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@
6767
!**/ios/**/default.mode2v3
6868
!**/ios/**/default.pbxuser
6969
!**/ios/**/default.perspectivev3
70-
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
70+
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages

0 commit comments

Comments
 (0)