Skip to content

Commit ba3659b

Browse files
committed
Fixed: HTTP method misaligned parameters.
1 parent 504d801 commit ba3659b

File tree

6 files changed

+76
-35
lines changed

6 files changed

+76
-35
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# Changelog
22

3+
## 0.1.1
4+
5+
* Fixed: HTTP Methods have misaligned parameters. Now they are called via named parameters to avoid type mismatch exceptions when being used.
6+
37
## 0.1.0
48

5-
* Added: Documentation for the example.
9+
* Added: Query Parameters to GET requests, it allows you to set proper parameters without having to add them to the URL beforehand.
10+
* Modified: Documentation for the example to include the new Query Parameters usage.
611

712
## 0.0.3
813

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ packages:
5959
path: ".."
6060
relative: true
6161
source: path
62-
version: "0.1.0"
62+
version: "0.1.1"
6363
http_parser:
6464
dependency: transitive
6565
description:

lib/http_client_with_interceptor.dart

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import 'dart:async';
22
import 'dart:convert';
33
import 'dart:typed_data';
4+
import 'package:flutter/foundation.dart';
45
import 'package:http/http.dart' as http;
56
import 'package:http/http.dart';
67
import 'package:http_interceptor/models/models.dart';
78
import 'package:http_interceptor/interceptor_contract.dart';
89

10+
import 'http_methods.dart';
11+
912
///Class to be used by the user to set up a new `http.Client` with interceptor supported.
1013
///call the `build()` constructor passing in the list of interceptors.
1114
///Example:
@@ -50,27 +53,57 @@ class HttpClientWithInterceptor extends http.BaseClient {
5053
);
5154
}
5255

53-
Future<Response> head(url, {Map<String, String> headers}) =>
54-
_sendUnstreamed("HEAD", url, headers, null);
56+
Future<Response> head(url, {Map<String, String> headers}) => _sendUnstreamed(
57+
method: Method.HEAD,
58+
url: url,
59+
headers: headers,
60+
);
5561

5662
Future<Response> get(url,
5763
{Map<String, String> headers, Map<String, String> params}) =>
58-
_sendUnstreamed("GET", url, headers, params);
64+
_sendUnstreamed(
65+
method: Method.GET,
66+
url: url,
67+
headers: headers,
68+
params: params,
69+
);
5970

6071
Future<Response> post(url,
6172
{Map<String, String> headers, body, Encoding encoding}) =>
62-
_sendUnstreamed("POST", url, headers, body, encoding);
73+
_sendUnstreamed(
74+
method: Method.POST,
75+
url: url,
76+
headers: headers,
77+
body: body,
78+
encoding: encoding,
79+
);
6380

6481
Future<Response> put(url,
6582
{Map<String, String> headers, body, Encoding encoding}) =>
66-
_sendUnstreamed("PUT", url, headers, body, encoding);
83+
_sendUnstreamed(
84+
method: Method.PUT,
85+
url: url,
86+
headers: headers,
87+
body: body,
88+
encoding: encoding,
89+
);
6790

6891
Future<Response> patch(url,
6992
{Map<String, String> headers, body, Encoding encoding}) =>
70-
_sendUnstreamed("PATCH", url, headers, body, encoding);
93+
_sendUnstreamed(
94+
method: Method.PATCH,
95+
url: url,
96+
headers: headers,
97+
body: body,
98+
encoding: encoding,
99+
);
71100

72101
Future<Response> delete(url, {Map<String, String> headers}) =>
73-
_sendUnstreamed("DELETE", url, headers, null);
102+
_sendUnstreamed(
103+
method: Method.DELETE,
104+
url: url,
105+
headers: headers,
106+
);
74107

75108
Future<String> read(url, {Map<String, String> headers}) {
76109
return get(url, headers: headers).then((response) {
@@ -88,22 +121,26 @@ class HttpClientWithInterceptor extends http.BaseClient {
88121

89122
Future<StreamedResponse> send(BaseRequest request) => _client.send(request);
90123

91-
Future<Response> _sendUnstreamed(String method, url,
92-
Map<String, String> headers, Map<String, String> params,
93-
[body, Encoding encoding]) async {
94-
if (url is String) {
95-
var paramUrl = url;
96-
if (params != null && params.length > 0) {
97-
paramUrl += "?";
98-
params.forEach((key, value) {
99-
paramUrl += "$key=$value&";
100-
});
101-
paramUrl = paramUrl.substring(0, paramUrl.length); // to remove the last '&' character
102-
}
103-
url = Uri.parse(paramUrl);
124+
Future<Response> _sendUnstreamed({
125+
@required Method method,
126+
@required String url,
127+
@required Map<String, String> headers,
128+
Map<String, String> params,
129+
dynamic body,
130+
Encoding encoding,
131+
}) async {
132+
String paramUrl = url;
133+
if (params != null && params.length > 0) {
134+
paramUrl += "?";
135+
params.forEach((key, value) {
136+
paramUrl += "$key=$value&";
137+
});
138+
paramUrl = paramUrl.substring(
139+
0, paramUrl.length); // to remove the last '&' character
104140
}
105141

106-
var request = new Request(method, url);
142+
var requestUrl = Uri.parse(paramUrl);
143+
var request = new Request(methodToString(method), requestUrl);
107144

108145
if (headers != null) request.headers.addAll(headers);
109146
if (encoding != null) request.encoding = encoding;

lib/http_with_interceptor.dart

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'dart:async';
22
import 'dart:convert';
3-
import 'dart:io';
43
import 'dart:typed_data';
4+
import 'package:flutter/foundation.dart';
55
import 'package:http/http.dart';
66
import 'package:http_interceptor/http_methods.dart';
77
import 'package:http_interceptor/models/models.dart';
@@ -54,7 +54,8 @@ class HttpWithInterceptor {
5454
{Map<String, String> headers, Map<String, String> params}) async {
5555
RequestData data = await _sendInterception(
5656
method: Method.GET, headers: headers, url: url, params: params);
57-
return _withClient((client) => client.get(data.requestUrl, headers: data.headers));
57+
return _withClient(
58+
(client) => client.get(data.requestUrl, headers: data.headers));
5859
}
5960

6061
Future<Response> post(url,
@@ -108,12 +109,12 @@ class HttpWithInterceptor {
108109
_withClient((client) => client.readBytes(url, headers: headers));
109110

110111
Future<RequestData> _sendInterception({
111-
Method method,
112-
Encoding encoding,
113-
dynamic body,
114-
String url,
115-
Map<String, String> headers,
112+
@required Method method,
113+
@required String url,
114+
@required Map<String, String> headers,
116115
Map<String, String> params,
116+
dynamic body,
117+
Encoding encoding,
117118
}) async {
118119
RequestData data = RequestData(
119120
method: method,

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: http_interceptor
2-
description: A Plugin that allows you to intercept request and response objects and modify them if desired.
3-
version: 0.1.0
2+
description: A lightweight, simple plugin that allows you to intercept request and response objects and modify them if desired.
3+
version: 0.1.1
44
author: Alejandro Ulate (codingalecr) <[email protected]>
55
homepage: https://github.com/CodingAleCR/http_interceptor
66

test/http_interceptor_test.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
void main() {
2-
//TODO: Add Tests.
3-
}
1+
void main() {}

0 commit comments

Comments
 (0)