http.dart 9.18 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright (c) 2013, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';

Adam Barth's avatar
Adam Barth committed
9 10
import 'package:mojo/core.dart' as mojo;

11 12 13 14 15 16
import 'mojo_client.dart';
import 'response.dart';

/// Sends an HTTP HEAD request with the given headers to the given URL, which
/// can be a [Uri] or a [String].
///
17 18 19
/// Network errors will be turned into [Response] object with a non-null
/// [Response.error] field.
///
20
/// This automatically initializes a new [MojoClient] and closes that client once
21
/// the request is complete. If you're planning on making multiple requests to
22 23
/// the same server, you should use a single [MojoClient] for all of those requests,
/// so that the same underlying TCP connection can be re-used (via HTTP pipelining).
24 25 26
Future<Response> head(dynamic url) {
  return _withClient/*<Response>*/((MojoClient client) => client.head(url));
}
27 28 29 30

/// Sends an HTTP GET request with the given headers to the given URL, which can
/// be a [Uri] or a [String].
///
31 32 33
/// Network errors will be turned into [Response] object with a non-null
/// [Response.error] field.
///
34
/// This automatically initializes a new [MojoClient] and closes that client once
35
/// the request is complete. If you're planning on making multiple requests to
36 37
/// the same server, you should use a single [MojoClient] for all of those requests,
/// so that the same underlying TCP connection can be re-used (via HTTP pipelining).
38 39 40
Future<Response> get(dynamic url, { Map<String, String> headers }) {
  return _withClient/*<Response>*/((MojoClient client) => client.get(url, headers: headers));
}
41 42 43 44

/// Sends an HTTP POST request with the given headers and body to the given URL,
/// which can be a [Uri] or a [String].
///
45 46 47 48 49 50 51 52 53 54 55 56 57 58
/// [body] sets the body of the request. It can be a [String], a [List<int>] or
/// a [Map<String, String>]. If it's a String, it's encoded using [encoding] and
/// used as the body of the request. The content-type of the request will
/// default to "text/plain".
///
/// If [body] is a List, it's used as a list of bytes for the body of the
/// request.
///
/// If [body] is a Map, it's encoded as form fields using [encoding]. The
/// content-type of the request will be set to
/// `"application/x-www-form-urlencoded"`; this cannot be overridden.
///
/// [encoding] defaults to [UTF8].
///
59 60 61
/// Network errors will be turned into [Response] object with a non-null
/// [Response.error] field.
///
62 63
/// This automatically initializes a new [MojoClient] and closes that client once
/// the request is complete. If you're planning on making multiple requests to
64 65
/// the same server, you should use a single [MojoClient] for all of those requests,
/// so that the same underlying TCP connection can be re-used (via HTTP pipelining).
66 67 68 69 70
Future<Response> post(dynamic url, { Map<String, String> headers, dynamic body, Encoding encoding: UTF8 }) {
  return _withClient/*<Response>*/((MojoClient client) {
    return client.post(url, headers: headers, body: body, encoding: encoding);
  });
}
71 72 73 74 75 76 77 78

/// Sends an HTTP PUT request with the given headers and body to the given URL,
/// which can be a [Uri] or a [String].
///
/// [body] sets the body of the request. It can be a [String], a [List<int>] or
/// a [Map<String, String>]. If it's a String, it's encoded using [encoding] and
/// used as the body of the request. The content-type of the request will
/// default to "text/plain".
79 80 81 82 83 84 85 86 87 88
///
/// If [body] is a List, it's used as a list of bytes for the body of the
/// request.
///
/// If [body] is a Map, it's encoded as form fields using [encoding]. The
/// content-type of the request will be set to
/// `"application/x-www-form-urlencoded"`; this cannot be overridden.
///
/// [encoding] defaults to [UTF8].
///
89 90 91
/// Network errors will be turned into [Response] object with a non-null
/// [Response.error] field.
///
92 93
/// This automatically initializes a new [MojoClient] and closes that client once
/// the request is complete. If you're planning on making multiple requests to
94 95
/// the same server, you should use a single [MojoClient] for all of those requests,
/// so that the same underlying TCP connection can be re-used (via HTTP pipelining).
96 97 98 99 100
Future<Response> put(dynamic url, { Map<String, String> headers, dynamic body, Encoding encoding: UTF8 }) {
  return _withClient/*<Response>*/((MojoClient client) {
    return client.put(url, headers: headers, body: body, encoding: encoding);
  });
}
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

/// Sends an HTTP PATCH request with the given headers and body to the given
/// URL, which can be a [Uri] or a [String].
///
/// [body] sets the body of the request. It can be a [String], a [List<int>] or
/// a [Map<String, String>]. If it's a String, it's encoded using [encoding] and
/// used as the body of the request. The content-type of the request will
/// default to "text/plain".
///
/// If [body] is a List, it's used as a list of bytes for the body of the
/// request.
///
/// If [body] is a Map, it's encoded as form fields using [encoding]. The
/// content-type of the request will be set to
/// `"application/x-www-form-urlencoded"`; this cannot be overridden.
///
/// [encoding] defaults to [UTF8].
///
119 120 121
/// Network errors will be turned into [Response] object with a non-null
/// [Response.error] field.
///
122 123
/// This automatically initializes a new [MojoClient] and closes that client once
/// the request is complete. If you're planning on making multiple requests to
124 125
/// the same server, you should use a single [MojoClient] for all of those requests,
/// so that the same underlying TCP connection can be re-used (via HTTP pipelining).
126 127 128 129 130
Future<Response> patch(dynamic url, { Map<String, String> headers, dynamic body, Encoding encoding: UTF8 }) {
  return _withClient/*<Response>*/((MojoClient client) {
     return client.patch(url, headers: headers, body: body, encoding: encoding);
  });
}
131 132 133 134

/// Sends an HTTP DELETE request with the given headers to the given URL, which
/// can be a [Uri] or a [String].
///
135 136 137
/// Network errors will be turned into [Response] object with a non-null
/// [Response.error] field.
///
138
/// This automatically initializes a new [MojoClient] and closes that client once
139
/// the request is complete. If you're planning on making multiple requests to
140 141
/// the same server, you should use a single [MojoClient] for all of those requests,
/// so that the same underlying TCP connection can be re-used (via HTTP pipelining).
142 143 144
Future<Response> delete(dynamic url, { Map<String, String> headers }) {
  return _withClient/*<Response>*/((MojoClient client) => client.delete(url, headers: headers));
}
145 146 147 148 149

/// Sends an HTTP GET request with the given headers to the given URL, which can
/// be a [Uri] or a [String], and returns a Future that completes to the body of
/// the response as a [String].
///
150 151
/// The Future will resolve with an error in the case of a network error or if
/// the response doesn't have a success status code.
152
///
153
/// This automatically initializes a new [MojoClient] and closes that client once
154
/// the request is complete. If you're planning on making multiple requests to
155 156
/// the same server, you should use a single [MojoClient] for all of those requests,
/// so that the same underlying TCP connection can be re-used (via HTTP pipelining).
157 158 159
Future<String> read(dynamic url, { Map<String, String> headers }) {
  return _withClient/*<String>*/((MojoClient client) => client.read(url, headers: headers));
}
160 161 162 163 164

/// Sends an HTTP GET request with the given headers to the given URL, which can
/// be a [Uri] or a [String], and returns a Future that completes to the body of
/// the response as a list of bytes.
///
165 166
/// The Future will resolve with an error in the case of a network error or if
/// the response doesn't have a success status code.
167
///
168
/// This automatically initializes a new [MojoClient] and closes that client once
169
/// the request is complete. If you're planning on making multiple requests to
170 171
/// the same server, you should use a single [MojoClient] for all of those requests,
/// so that the same underlying TCP connection can be re-used (via HTTP pipelining).
172 173 174
Future<Uint8List> readBytes(dynamic url, { Map<String, String> headers }) {
  return _withClient/*<Uint8List>*/((MojoClient client) => client.readBytes(url, headers: headers));
}
175

Adam Barth's avatar
Adam Barth committed
176 177 178 179
/// Sends an HTTP GET request with the given headers to the given URL, which can
/// be a [Uri] or a [String], and returns a Future that completes to a data pipe
/// containing the response bytes.
///
180 181
/// The Future will resolve with an error in the case of a network error or if
/// the response doesn't have a success status code.
Adam Barth's avatar
Adam Barth committed
182 183 184
///
/// This automatically initializes a new [MojoClient] and closes that client once
/// the request is complete. If you're planning on making multiple requests to
185 186
/// the same server, you should use a single [MojoClient] for all of those requests,
/// so that the same underlying TCP connection can be re-used (via HTTP pipelining).
Adam Barth's avatar
Adam Barth committed
187 188 189 190
Future<mojo.MojoDataPipeConsumer> readDataPipe(dynamic url, { Map<String, String> headers }) {
  return _withClient/*<mojo.MojoDataPipeConsumer>*/((MojoClient client) => client.readDataPipe(url, headers: headers));
}

191
Future<dynamic/*=T*/> _withClient/*<T>*/(Future<dynamic/*=T*/> fn(MojoClient client)) {
192
  return fn(new MojoClient());
193
}