@@ -13,136 +13,46 @@ import 'exception.dart';
13
13
import 'request.dart' ;
14
14
import 'response.dart' ;
15
15
16
- /// The abstract base class for an HTTP client. This is a mixin-style class;
17
- /// subclasses only need to implement [send] and maybe [close] , and then they
16
+ /// The abstract base class for an HTTP client.
17
+ ///
18
+ /// Subclasses only need to implement [send] and maybe [close] , and then they
18
19
/// get various convenience methods for free.
19
20
abstract class BaseClient implements Client {
20
- /// Sends an HTTP HEAD request with the given headers to the given URL, which
21
- /// can be a [Uri] or a [String] .
22
- ///
23
- /// For more fine-grained control over the request, use [send] instead.
24
21
Future <Response > head (url, {Map <String , String > headers}) =>
25
22
send (new Request .head (url, headers: headers));
26
23
27
- /// Sends an HTTP GET request with the given headers to the given URL, which
28
- /// can be a [Uri] or a [String] .
29
- ///
30
- /// For more fine-grained control over the request, use [send] instead.
31
24
Future <Response > get (url, {Map <String , String > headers}) =>
32
25
send (new Request .get (url, headers: headers));
33
26
34
- /// Sends an HTTP POST request with the given headers and body to the given
35
- /// URL, which can be a [Uri] or a [String] .
36
- ///
37
- /// [body] sets the body of the request. It can be a [String] , a [List<int>]
38
- /// or a [Map<String, String>] . If it's a String, it's encoded using
39
- /// [encoding] and used as the body of the request. The content-type of the
40
- /// request will default to "text/plain".
41
- ///
42
- /// If [body] is a List, it's used as a list of bytes for the body of the
43
- /// request.
44
- ///
45
- /// If [body] is a Map, it's encoded as form fields using [encoding] . The
46
- /// content-type of the request will be set to
47
- /// `"application/x-www-form-urlencoded"` ; this cannot be overridden.
48
- ///
49
- /// [encoding] defaults to UTF-8.
50
- ///
51
- /// For more fine-grained control over the request, use [send] instead.
52
27
Future <Response > post (url, body,
53
28
{Map <String , String > headers, Encoding encoding}) =>
54
29
send (new Request .post (url, body, headers: headers, encoding: encoding));
55
30
56
- /// Sends an HTTP PUT request with the given headers and body to the given
57
- /// URL, which can be a [Uri] or a [String] .
58
- ///
59
- /// [body] sets the body of the request. It can be a [String] , a [List<int>]
60
- /// or a [Map<String, String>] . If it's a String, it's encoded using
61
- /// [encoding] and used as the body of the request. The content-type of the
62
- /// request will default to "text/plain".
63
- ///
64
- /// If [body] is a List, it's used as a list of bytes for the body of the
65
- /// request.
66
- ///
67
- /// If [body] is a Map, it's encoded as form fields using [encoding] . The
68
- /// content-type of the request will be set to
69
- /// `"application/x-www-form-urlencoded"` ; this cannot be overridden.
70
- ///
71
- /// [encoding] defaults to UTF-8.
72
- ///
73
- /// For more fine-grained control over the request, use [send] instead.
74
31
Future <Response > put (url, body,
75
32
{Map <String , String > headers, Encoding encoding}) =>
76
33
send (new Request .put (url, body, headers: headers, encoding: encoding));
77
34
78
- /// Sends an HTTP PATCH request with the given headers and body to the given
79
- /// URL, which can be a [Uri] or a [String] .
80
- ///
81
- /// [body] sets the body of the request. It can be a [String] , a [List<int>]
82
- /// or a [Map<String, String>] . If it's a String, it's encoded using
83
- /// [encoding] and used as the body of the request. The content-type of the
84
- /// request will default to "text/plain".
85
- ///
86
- /// If [body] is a List, it's used as a list of bytes for the body of the
87
- /// request.
88
- ///
89
- /// If [body] is a Map, it's encoded as form fields using [encoding] . The
90
- /// content-type of the request will be set to
91
- /// `"application/x-www-form-urlencoded"` ; this cannot be overridden.
92
- ///
93
- /// [encoding] defaults to UTF-8.
94
- ///
95
- /// For more fine-grained control over the request, use [send] instead.
96
35
Future <Response > patch (url, body,
97
36
{Map <String , String > headers, Encoding encoding}) =>
98
37
send (new Request .patch (url, body, headers: headers, encoding: encoding));
99
38
100
- /// Sends an HTTP DELETE request with the given headers to the given URL,
101
- /// which can be a [Uri] or a [String] .
102
- ///
103
- /// For more fine-grained control over the request, use [send] instead.
104
39
Future <Response > delete (url, {Map <String , String > headers}) =>
105
40
send (new Request .delete (url, headers: headers));
106
41
107
- /// Sends an HTTP GET request with the given headers to the given URL, which
108
- /// can be a [Uri] or a [String] , and returns a Future that completes to the
109
- /// body of the response as a String.
110
- ///
111
- /// The Future will emit a [ClientException] if the response doesn't have a
112
- /// success status code.
113
- ///
114
- /// For more fine-grained control over the request and response, use [send] or
115
- /// [get] instead.
116
42
Future <String > read (url, {Map <String , String > headers}) async {
117
43
var response = await get (url, headers: headers);
118
44
_checkResponseSuccess (url, response);
119
45
120
46
return await response.readAsString ();
121
47
}
122
48
123
- /// Sends an HTTP GET request with the given headers to the given URL, which
124
- /// can be a [Uri] or a [String] , and returns a Future that completes to the
125
- /// body of the response as a list of bytes.
126
- ///
127
- /// The Future will emit an [ClientException] if the response doesn't have a
128
- /// success status code.
129
- ///
130
- /// For more fine-grained control over the request and response, use [send] or
131
- /// [get] instead.
132
49
Future <Uint8List > readBytes (url, {Map <String , String > headers}) async {
133
50
var response = await get (url, headers: headers);
134
51
_checkResponseSuccess (url, response);
135
52
136
53
return await collectBytes (response.read ());
137
54
}
138
55
139
- /// Sends an HTTP request and asynchronously returns the response.
140
- ///
141
- /// Implementers should call [BaseRequest.finalize] to get the body of the
142
- /// request as a [ByteStream] . They shouldn't make any assumptions about the
143
- /// state of the stream; it could have data written to it asynchronously at a
144
- /// later point, or it could already be closed when it's returned. Any
145
- /// internal HTTP errors should be wrapped as [ClientException] s.
146
56
Future <Response > send (Request request);
147
57
148
58
/// Throws an error if [response] is not successful.
@@ -156,8 +66,5 @@ abstract class BaseClient implements Client {
156
66
throw new ClientException ("$message ." , url);
157
67
}
158
68
159
- /// Closes the client and cleans up any resources associated with it. It's
160
- /// important to close each client when it's done being used; failing to do so
161
- /// can cause the Dart process to hang.
162
69
void close () {}
163
70
}
0 commit comments