OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /// A composable, [Future]-based library for making HTTP requests. | |
6 library http; | |
7 | |
8 import 'dart:async'; | |
9 import 'dart:convert'; | |
10 import 'dart:typed_data'; | |
11 | |
12 import 'src/client.dart'; | |
13 import 'src/response.dart'; | |
14 | |
15 export 'src/base_client.dart'; | |
16 export 'src/base_request.dart'; | |
17 export 'src/base_response.dart'; | |
18 export 'src/byte_stream.dart'; | |
19 export 'src/client.dart'; | |
20 export 'src/exception.dart'; | |
21 export 'src/io_client.dart'; | |
22 export 'src/multipart_file.dart'; | |
23 export 'src/multipart_request.dart'; | |
24 export 'src/request.dart'; | |
25 export 'src/response.dart'; | |
26 export 'src/streamed_request.dart'; | |
27 export 'src/streamed_response.dart'; | |
28 | |
29 /// Sends an HTTP HEAD request with the given headers to the given URL, which | |
30 /// can be a [Uri] or a [String]. | |
31 /// | |
32 /// This automatically initializes a new [Client] and closes that client once | |
33 /// the request is complete. If you're planning on making multiple requests to | |
34 /// the same server, you should use a single [Client] for all of those requests. | |
35 /// | |
36 /// For more fine-grained control over the request, use [Request] instead. | |
37 Future<Response> head(url, {Map<String, String> headers}) => | |
38 _withClient((client) => client.head(url, headers: headers)); | |
39 | |
40 /// Sends an HTTP GET request with the given headers to the given URL, which can | |
41 /// be a [Uri] or a [String]. | |
42 /// | |
43 /// This automatically initializes a new [Client] and closes that client once | |
44 /// the request is complete. If you're planning on making multiple requests to | |
45 /// the same server, you should use a single [Client] for all of those requests. | |
46 /// | |
47 /// For more fine-grained control over the request, use [Request] instead. | |
48 Future<Response> get(url, {Map<String, String> headers}) => | |
49 _withClient((client) => client.get(url, headers: headers)); | |
50 | |
51 /// Sends an HTTP POST request with the given headers and body to the given URL, | |
52 /// which can be a [Uri] or a [String]. | |
53 /// | |
54 /// [body] sets the body of the request. It can be a [String], a [List<int>] or | |
55 /// a [Map<String, String>]. If it's a String, it's encoded using [encoding] and | |
56 /// used as the body of the request. The content-type of the request will | |
57 /// default to "text/plain". | |
58 /// | |
59 /// If [body] is a List, it's used as a list of bytes for the body of the | |
60 /// request. | |
61 /// | |
62 /// If [body] is a Map, it's encoded as form fields using [encoding]. The | |
63 /// content-type of the request will be set to | |
64 /// `"application/x-www-form-urlencoded"`; this cannot be overridden. | |
65 /// | |
66 /// [encoding] defaults to [UTF8]. | |
67 /// | |
68 /// For more fine-grained control over the request, use [Request] or | |
69 /// [StreamedRequest] instead. | |
70 Future<Response> post(url, {Map<String, String> headers, body, | |
71 Encoding encoding}) => | |
72 _withClient((client) => client.post(url, | |
73 headers: headers, body: body, encoding: encoding)); | |
74 | |
75 /// Sends an HTTP PUT request with the given headers and body to the given URL, | |
76 /// which can be a [Uri] or a [String]. | |
77 /// | |
78 /// [body] sets the body of the request. It can be a [String], a [List<int>] or | |
79 /// a [Map<String, String>]. If it's a String, it's encoded using [encoding] and | |
80 /// used as the body of the request. The content-type of the request will | |
81 /// default to "text/plain". | |
82 /// | |
83 /// If [body] is a List, it's used as a list of bytes for the body of the | |
84 /// request. | |
85 /// | |
86 /// If [body] is a Map, it's encoded as form fields using [encoding]. The | |
87 /// content-type of the request will be set to | |
88 /// `"application/x-www-form-urlencoded"`; this cannot be overridden. | |
89 /// | |
90 /// [encoding] defaults to [UTF8]. | |
91 /// | |
92 /// For more fine-grained control over the request, use [Request] or | |
93 /// [StreamedRequest] instead. | |
94 Future<Response> put(url, {Map<String, String> headers, body, | |
95 Encoding encoding}) => | |
96 _withClient((client) => client.put(url, | |
97 headers: headers, body: body, encoding: encoding)); | |
98 | |
99 /// Sends an HTTP DELETE request with the given headers to the given URL, which | |
100 /// can be a [Uri] or a [String]. | |
101 /// | |
102 /// This automatically initializes a new [Client] and closes that client once | |
103 /// the request is complete. If you're planning on making multiple requests to | |
104 /// the same server, you should use a single [Client] for all of those requests. | |
105 /// | |
106 /// For more fine-grained control over the request, use [Request] instead. | |
107 Future<Response> delete(url, {Map<String, String> headers}) => | |
108 _withClient((client) => client.delete(url, headers: headers)); | |
109 | |
110 /// Sends an HTTP GET request with the given headers to the given URL, which can | |
111 /// be a [Uri] or a [String], and returns a Future that completes to the body of | |
112 /// the response as a [String]. | |
113 /// | |
114 /// The Future will emit a [ClientException] if the response doesn't have a | |
115 /// success status code. | |
116 /// | |
117 /// This automatically initializes a new [Client] and closes that client once | |
118 /// the request is complete. If you're planning on making multiple requests to | |
119 /// the same server, you should use a single [Client] for all of those requests. | |
120 /// | |
121 /// For more fine-grained control over the request and response, use [Request] | |
122 /// instead. | |
123 Future<String> read(url, {Map<String, String> headers}) => | |
124 _withClient((client) => client.read(url, headers: headers)); | |
125 | |
126 /// Sends an HTTP GET request with the given headers to the given URL, which can | |
127 /// be a [Uri] or a [String], and returns a Future that completes to the body of | |
128 /// the response as a list of bytes. | |
129 /// | |
130 /// The Future will emit a [ClientException] if the response doesn't have a | |
131 /// success status code. | |
132 /// | |
133 /// This automatically initializes a new [Client] and closes that client once | |
134 /// the request is complete. If you're planning on making multiple requests to | |
135 /// the same server, you should use a single [Client] for all of those requests. | |
136 /// | |
137 /// For more fine-grained control over the request and response, use [Request] | |
138 /// instead. | |
139 Future<Uint8List> readBytes(url, {Map<String, String> headers}) => | |
140 _withClient((client) => client.readBytes(url, headers: headers)); | |
141 | |
142 Future _withClient(Future fn(Client)) { | |
143 var client = new Client(); | |
144 var future = fn(client); | |
145 return future.whenComplete(client.close); | |
146 } | |
OLD | NEW |