Line data Source code
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 : import 'dart:async';
7 : import 'dart:convert';
8 : import 'dart:typed_data';
9 :
10 : import 'src/client.dart';
11 : import 'src/response.dart';
12 :
13 : export 'src/base_client.dart';
14 : export 'src/base_request.dart';
15 : export 'src/base_response.dart';
16 : export 'src/byte_stream.dart';
17 : export 'src/client.dart';
18 : export 'src/exception.dart';
19 : export 'src/io_client.dart';
20 : export 'src/multipart_file.dart';
21 : export 'src/multipart_request.dart';
22 : export 'src/request.dart';
23 : export 'src/response.dart';
24 : export 'src/streamed_request.dart';
25 : export 'src/streamed_response.dart';
26 :
27 : /// Sends an HTTP HEAD request with the given headers to the given URL, which
28 : /// can be a [Uri] or a [String].
29 : ///
30 : /// This automatically initializes a new [Client] and closes that client once
31 : /// the request is complete. If you're planning on making multiple requests to
32 : /// the same server, you should use a single [Client] for all of those requests.
33 : ///
34 : /// For more fine-grained control over the request, use [Request] instead.
35 : Future<Response> head(url, {Map<String, String> headers}) =>
36 0 : _withClient((client) => client.head(url, headers: headers));
37 :
38 : /// Sends an HTTP GET request with the given headers to the given URL, which can
39 : /// be a [Uri] or a [String].
40 : ///
41 : /// This automatically initializes a new [Client] and closes that client once
42 : /// the request is complete. If you're planning on making multiple requests to
43 : /// the same server, you should use a single [Client] for all of those requests.
44 : ///
45 : /// For more fine-grained control over the request, use [Request] instead.
46 : Future<Response> get(url, {Map<String, String> headers}) =>
47 0 : _withClient((client) => client.get(url, headers: headers));
48 :
49 : /// Sends an HTTP POST request with the given headers and body to the given URL,
50 : /// which can be a [Uri] or a [String].
51 : ///
52 : /// [body] sets the body of the request. It can be a [String], a [List<int>] or
53 : /// a [Map<String, String>]. If it's a String, it's encoded using [encoding] and
54 : /// used as the body of the request. The content-type of the request will
55 : /// default to "text/plain".
56 : ///
57 : /// If [body] is a List, it's used as a list of bytes for the body of the
58 : /// request.
59 : ///
60 : /// If [body] is a Map, it's encoded as form fields using [encoding]. The
61 : /// content-type of the request will be set to
62 : /// `"application/x-www-form-urlencoded"`; this cannot be overridden.
63 : ///
64 : /// [encoding] defaults to [UTF8].
65 : ///
66 : /// For more fine-grained control over the request, use [Request] or
67 : /// [StreamedRequest] instead.
68 : Future<Response> post(url, {Map<String, String> headers, body,
69 : Encoding encoding}) =>
70 0 : _withClient((client) => client.post(url,
71 : headers: headers, body: body, encoding: encoding));
72 :
73 : /// Sends an HTTP PUT request with the given headers and body to the given URL,
74 : /// which can be a [Uri] or a [String].
75 : ///
76 : /// [body] sets the body of the request. It can be a [String], a [List<int>] or
77 : /// a [Map<String, String>]. If it's a String, it's encoded using [encoding] and
78 : /// used as the body of the request. The content-type of the request will
79 : /// default to "text/plain".
80 : ///
81 : /// If [body] is a List, it's used as a list of bytes for the body of the
82 : /// request.
83 : ///
84 : /// If [body] is a Map, it's encoded as form fields using [encoding]. The
85 : /// content-type of the request will be set to
86 : /// `"application/x-www-form-urlencoded"`; this cannot be overridden.
87 : ///
88 : /// [encoding] defaults to [UTF8].
89 : ///
90 : /// For more fine-grained control over the request, use [Request] or
91 : /// [StreamedRequest] instead.
92 : Future<Response> put(url, {Map<String, String> headers, body,
93 : Encoding encoding}) =>
94 0 : _withClient((client) => client.put(url,
95 : headers: headers, body: body, encoding: encoding));
96 :
97 : /// Sends an HTTP PATCH request with the given headers and body to the given
98 : /// URL, which can be a [Uri] or a [String].
99 : ///
100 : /// [body] sets the body of the request. It can be a [String], a [List<int>] or
101 : /// a [Map<String, String>]. If it's a String, it's encoded using [encoding] and
102 : /// used as the body of the request. The content-type of the request will
103 : /// default to "text/plain".
104 : ///
105 : /// If [body] is a List, it's used as a list of bytes for the body of the
106 : /// request.
107 : ///
108 : /// If [body] is a Map, it's encoded as form fields using [encoding]. The
109 : /// content-type of the request will be set to
110 : /// `"application/x-www-form-urlencoded"`; this cannot be overridden.
111 : ///
112 : /// [encoding] defaults to [UTF8].
113 : ///
114 : /// For more fine-grained control over the request, use [Request] or
115 : /// [StreamedRequest] instead.
116 : Future<Response> patch(url, {Map<String, String> headers, body,
117 : Encoding encoding}) =>
118 0 : _withClient((client) => client.patch(url,
119 : headers: headers, body: body, encoding: encoding));
120 :
121 : /// Sends an HTTP DELETE request with the given headers to the given URL, which
122 : /// can be a [Uri] or a [String].
123 : ///
124 : /// This automatically initializes a new [Client] and closes that client once
125 : /// the request is complete. If you're planning on making multiple requests to
126 : /// the same server, you should use a single [Client] for all of those requests.
127 : ///
128 : /// For more fine-grained control over the request, use [Request] instead.
129 : Future<Response> delete(url, {Map<String, String> headers}) =>
130 0 : _withClient((client) => client.delete(url, headers: headers));
131 :
132 : /// Sends an HTTP GET request with the given headers to the given URL, which can
133 : /// be a [Uri] or a [String], and returns a Future that completes to the body of
134 : /// the response as a [String].
135 : ///
136 : /// The Future will emit a [ClientException] if the response doesn't have a
137 : /// success status code.
138 : ///
139 : /// This automatically initializes a new [Client] and closes that client once
140 : /// the request is complete. If you're planning on making multiple requests to
141 : /// the same server, you should use a single [Client] for all of those requests.
142 : ///
143 : /// For more fine-grained control over the request and response, use [Request]
144 : /// instead.
145 : Future<String> read(url, {Map<String, String> headers}) =>
146 0 : _withClient((client) => client.read(url, headers: headers));
147 :
148 : /// Sends an HTTP GET request with the given headers to the given URL, which can
149 : /// be a [Uri] or a [String], and returns a Future that completes to the body of
150 : /// the response as a list of bytes.
151 : ///
152 : /// The Future will emit a [ClientException] if the response doesn't have a
153 : /// success status code.
154 : ///
155 : /// This automatically initializes a new [Client] and closes that client once
156 : /// the request is complete. If you're planning on making multiple requests to
157 : /// the same server, you should use a single [Client] for all of those requests.
158 : ///
159 : /// For more fine-grained control over the request and response, use [Request]
160 : /// instead.
161 : Future<Uint8List> readBytes(url, {Map<String, String> headers}) =>
162 0 : _withClient((client) => client.readBytes(url, headers: headers));
163 :
164 : Future<T> _withClient<T>(Future<T> fn(Client client)) async {
165 0 : var client = new Client();
166 : try {
167 0 : return await fn(client);
168 : } finally {
169 0 : client.close();
170 : }
171 0 : }
|