11import 'dart:async' ;
22import 'dart:convert' ;
33import 'dart:typed_data' ;
4+ import 'package:flutter/foundation.dart' ;
45import 'package:http/http.dart' as http;
56import 'package:http/http.dart' ;
67import 'package:http_interceptor/models/models.dart' ;
78import '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;
0 commit comments