Skip to content

Fix formatting #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions lib/browser_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ class BrowserClient extends BaseClient {

reader.onLoad.first.then((_) {
var body = reader.result as Uint8List;
completer.complete(new Response(
xhr.responseUrl,
xhr.status,
completer.complete(new Response(xhr.responseUrl, xhr.status,
reasonPhrase: xhr.statusText,
body: new Stream.fromIterable([body]),
headers: xhr.responseHeaders));
Expand Down
34 changes: 17 additions & 17 deletions lib/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export 'src/response.dart';
///
/// For more fine-grained control over the request, use [Request] instead.
Future<Response> head(url, {Map<String, String> headers}) =>
_withClient((client) => client.head(url, headers: headers));
_withClient((client) => client.head(url, headers: headers));

/// Sends an HTTP GET request with the given headers to the given URL, which can
/// be a [Uri] or a [String].
Expand All @@ -41,7 +41,7 @@ Future<Response> head(url, {Map<String, String> headers}) =>
///
/// For more fine-grained control over the request, use [Request] instead.
Future<Response> get(url, {Map<String, String> headers}) =>
_withClient((client) => client.get(url, headers: headers));
_withClient((client) => client.get(url, headers: headers));

/// Sends an HTTP POST request with the given headers and body to the given URL,
/// which can be a [Uri] or a [String].
Expand All @@ -62,10 +62,10 @@ Future<Response> get(url, {Map<String, String> headers}) =>
///
/// For more fine-grained control over the request, use [Request] or
/// [StreamedRequest] instead.
Future<Response> post(url, body, {Map<String, String> headers,
Encoding encoding}) =>
_withClient((client) => client.post(url, body,
headers: headers, encoding: encoding));
Future<Response> post(url, body,
{Map<String, String> headers, Encoding encoding}) =>
_withClient((client) =>
client.post(url, body, headers: headers, encoding: encoding));

/// Sends an HTTP PUT request with the given headers and body to the given URL,
/// which can be a [Uri] or a [String].
Expand All @@ -86,10 +86,10 @@ Future<Response> post(url, body, {Map<String, String> headers,
///
/// For more fine-grained control over the request, use [Request] or
/// [StreamedRequest] instead.
Future<Response> put(url, body, {Map<String, String> headers,
Encoding encoding}) =>
_withClient((client) => client.put(url, body,
headers: headers, encoding: encoding));
Future<Response> put(url, body,
{Map<String, String> headers, Encoding encoding}) =>
_withClient((client) =>
client.put(url, body, headers: headers, encoding: encoding));

/// Sends an HTTP PATCH request with the given headers and body to the given
/// URL, which can be a [Uri] or a [String].
Expand All @@ -110,10 +110,10 @@ Future<Response> put(url, body, {Map<String, String> headers,
///
/// For more fine-grained control over the request, use [Request] or
/// [StreamedRequest] instead.
Future<Response> patch(url, body, {Map<String, String> headers,
Encoding encoding}) =>
_withClient((client) => client.patch(url, body,
headers: headers, encoding: encoding));
Future<Response> patch(url, body,
{Map<String, String> headers, Encoding encoding}) =>
_withClient((client) =>
client.patch(url, body, headers: headers, encoding: encoding));

/// Sends an HTTP DELETE request with the given headers to the given URL, which
/// can be a [Uri] or a [String].
Expand All @@ -124,7 +124,7 @@ Future<Response> patch(url, body, {Map<String, String> headers,
///
/// For more fine-grained control over the request, use [Request] instead.
Future<Response> delete(url, {Map<String, String> headers}) =>
_withClient((client) => client.delete(url, headers: headers));
_withClient((client) => client.delete(url, headers: headers));

/// 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
Expand All @@ -140,7 +140,7 @@ Future<Response> delete(url, {Map<String, String> headers}) =>
/// For more fine-grained control over the request and response, use [Request]
/// instead.
Future<String> read(url, {Map<String, String> headers}) =>
_withClient((client) => client.read(url, headers: headers));
_withClient((client) => client.read(url, headers: headers));

/// 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
Expand All @@ -156,7 +156,7 @@ Future<String> read(url, {Map<String, String> headers}) =>
/// For more fine-grained control over the request and response, use [Request]
/// instead.
Future<Uint8List> readBytes(url, {Map<String, String> headers}) =>
_withClient((client) => client.readBytes(url, headers: headers));
_withClient((client) => client.readBytes(url, headers: headers));

Future<T> _withClient<T>(Future<T> fn(Client client)) async {
var client = new Client();
Expand Down
25 changes: 11 additions & 14 deletions lib/src/base_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ abstract class BaseClient implements Client {
///
/// For more fine-grained control over the request, use [send] instead.
Future<Response> head(url, {Map<String, String> headers}) =>
send(new Request.head(url, headers: headers));
send(new Request.head(url, headers: headers));

/// Sends an HTTP GET request with the given headers to the given URL, which
/// can be a [Uri] or a [String].
///
/// For more fine-grained control over the request, use [send] instead.
Future<Response> get(url, {Map<String, String> headers}) =>
send(new Request.get(url, headers: headers));
send(new Request.get(url, headers: headers));

/// Sends an HTTP POST request with the given headers and body to the given
/// URL, which can be a [Uri] or a [String].
Expand All @@ -49,10 +49,9 @@ abstract class BaseClient implements Client {
/// [encoding] defaults to UTF-8.
///
/// For more fine-grained control over the request, use [send] instead.
Future<Response> post(url, body, {Map<String, String> headers,
Encoding encoding}) =>
send(new Request.post(url, body, headers: headers,
encoding: encoding));
Future<Response> post(url, body,
{Map<String, String> headers, Encoding encoding}) =>
send(new Request.post(url, body, headers: headers, encoding: encoding));

/// Sends an HTTP PUT request with the given headers and body to the given
/// URL, which can be a [Uri] or a [String].
Expand All @@ -72,10 +71,9 @@ abstract class BaseClient implements Client {
/// [encoding] defaults to UTF-8.
///
/// For more fine-grained control over the request, use [send] instead.
Future<Response> put(url, body, {Map<String, String> headers,
Encoding encoding}) =>
send(new Request.put(url, body, headers: headers,
encoding: encoding));
Future<Response> put(url, body,
{Map<String, String> headers, Encoding encoding}) =>
send(new Request.put(url, body, headers: headers, encoding: encoding));

/// Sends an HTTP PATCH request with the given headers and body to the given
/// URL, which can be a [Uri] or a [String].
Expand All @@ -95,10 +93,9 @@ abstract class BaseClient implements Client {
/// [encoding] defaults to UTF-8.
///
/// For more fine-grained control over the request, use [send] instead.
Future<Response> patch(url, body, {Map<String, String> headers,
Encoding encoding}) =>
send(new Request.patch(url, body, headers: headers,
encoding: encoding));
Future<Response> patch(url, body,
{Map<String, String> headers, Encoding encoding}) =>
send(new Request.patch(url, body, headers: headers, encoding: encoding));

/// Sends an HTTP DELETE request with the given headers to the given URL,
/// which can be a [Uri] or a [String].
Expand Down
16 changes: 8 additions & 8 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ abstract class Client {
/// [Future<Response>]. It will be called when [Client.send] is invoked.
///
/// When [Client.close] is called the [onClose] function will be called.
factory Client.handler(Handler handler, {void onClose()})
=> new HandlerClient(handler, onClose ?? () {});
factory Client.handler(Handler handler, {void onClose()}) =>
new HandlerClient(handler, onClose ?? () {});

/// Sends an HTTP HEAD request with the given headers to the given URL, which
/// can be a [Uri] or a [String].
Expand Down Expand Up @@ -69,8 +69,8 @@ abstract class Client {
/// [encoding] defaults to [UTF8].
///
/// For more fine-grained control over the request, use [send] instead.
Future<Response> post(url, body, {Map<String, String> headers,
Encoding encoding});
Future<Response> post(url, body,
{Map<String, String> headers, Encoding encoding});

/// Sends an HTTP PUT request with the given headers and body to the given
/// URL, which can be a [Uri] or a [String].
Expand All @@ -90,8 +90,8 @@ abstract class Client {
/// [encoding] defaults to [UTF8].
///
/// For more fine-grained control over the request, use [send] instead.
Future<Response> put(url, body, {Map<String, String> headers,
Encoding encoding});
Future<Response> put(url, body,
{Map<String, String> headers, Encoding encoding});

/// Sends an HTTP PATCH request with the given headers and body to the given
/// URL, which can be a [Uri] or a [String].
Expand All @@ -111,8 +111,8 @@ abstract class Client {
/// [encoding] defaults to [UTF8].
///
/// For more fine-grained control over the request, use [send] instead.
Future<Response> patch(url, body, {Map<String, String> headers,
Encoding encoding});
Future<Response> patch(url, body,
{Map<String, String> headers, Encoding encoding});

/// Sends an HTTP DELETE request with the given headers to the given URL,
/// which can be a [Uri] or a [String].
Expand Down
3 changes: 1 addition & 2 deletions lib/src/handler_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class HandlerClient extends BaseClient {
final void Function() _close;

/// Creates a new client using the [_handler] and [onClose] functions.
HandlerClient(this._handler, void onClose())
: _close = onClose;
HandlerClient(this._handler, void onClose()) : _close = onClose;

/// Sends an HTTP request and asynchronously returns the response.
Future<Response> send(Request request) => _handler(request);
Expand Down
10 changes: 4 additions & 6 deletions lib/src/io_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class IOClient extends BaseClient {
var context = request.context;

ioRequest
..followRedirects = context['io.followRedirects'] ?? true
..maxRedirects = context['io.maxRedirects'] ?? 5
..persistentConnection = context['io.persistentConnection'] ?? true;
..followRedirects = context['io.followRedirects'] ?? true
..maxRedirects = context['io.maxRedirects'] ?? 5
..persistentConnection = context['io.persistentConnection'] ?? true;
request.headers.forEach((name, value) {
ioRequest.headers.set(name, value);
});
Expand All @@ -44,9 +44,7 @@ class IOClient extends BaseClient {
headers[key] = values.join(',');
});

return new Response(
_responseUrl(request, response),
response.statusCode,
return new Response(_responseUrl(request, response), response.statusCode,
reasonPhrase: response.reasonPhrase,
body: DelegatingStream.typed<List<int>>(response).handleError(
(error) => throw new ClientException(error.message, error.uri),
Expand Down
30 changes: 14 additions & 16 deletions lib/src/middleware.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,25 @@ typedef Client Middleware(Client inner);
/// If provided, [errorHandler] receives errors thrown by the inner handler. It
/// does not receive errors thrown by [requestHandler] or [responseHandler].
/// It can either return a new response or throw an error.
Middleware createMiddleware({
Future<Request> requestHandler(Request request),
Future<Response> responseHandler(Response response),
void onClose(),
void errorHandler(error, [StackTrace stackTrace])
}) {
Middleware createMiddleware(
{Future<Request> requestHandler(Request request),
Future<Response> responseHandler(Response response),
void onClose(),
void errorHandler(error, [StackTrace stackTrace])}) {
requestHandler ??= (request) async => request;
responseHandler ??= (response) async => response;

return (inner) {
return new HandlerClient(
(request) =>
requestHandler(request)
.then((req) => inner.send(req))
.then((res) => responseHandler(res), onError: errorHandler),
onClose == null
? inner.close
: () {
onClose();
inner.close();
},
(request) => requestHandler(request)
.then((req) => inner.send(req))
.then((res) => responseHandler(res), onError: errorHandler),
onClose == null
? inner.close
: () {
onClose();
inner.close();
},
);
};
}
25 changes: 11 additions & 14 deletions lib/src/response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ class Response extends Message {
Encoding encoding,
Map<String, String> headers,
Map<String, Object> context})
: this._(getUrl(finalUrl), statusCode, reasonPhrase ?? '',
body, encoding, headers, context);
: this._(getUrl(finalUrl), statusCode, reasonPhrase ?? '', body, encoding,
headers, context);

Response._(this.finalUrl, this.statusCode, this.reasonPhrase,
Response._(
this.finalUrl,
this.statusCode,
this.reasonPhrase,
body,
Encoding encoding,
Map<String, String> headers,
Expand All @@ -68,20 +71,12 @@ class Response extends Message {
/// [body] is the request body. It may be either a [String], a [List<int>], a
/// [Stream<List<int>>], or `null` to indicate no body.
Response change(
{Map<String, String> headers,
Map<String, Object> context,
body}) {
{Map<String, String> headers, Map<String, Object> context, body}) {
var updatedHeaders = updateMap(this.headers, headers);
var updatedContext = updateMap(this.context, context);

return new Response._(
this.finalUrl,
this.statusCode,
this.reasonPhrase,
body ?? getBody(this),
this.encoding,
updatedHeaders,
updatedContext);
return new Response._(this.finalUrl, this.statusCode, this.reasonPhrase,
body ?? getBody(this), this.encoding, updatedHeaders, updatedContext);
}

/// The date and time after which the response's data should be considered
Expand All @@ -95,6 +90,7 @@ class Response extends Message {
_expiresCache = parseHttpDate(headers['expires']);
return _expiresCache;
}

DateTime _expiresCache;

/// The date and time the source of the response's data was last modified.
Expand All @@ -107,5 +103,6 @@ class Response extends Message {
_lastModifiedCache = parseHttpDate(headers['last-modified']);
return _lastModifiedCache;
}

DateTime _lastModifiedCache;
}
3 changes: 1 addition & 2 deletions test/hybrid/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ hybridMain(StreamChannel channel) async {
requestBody = null;
} else if (request.headers.contentType != null &&
request.headers.contentType.charset != null) {
var encoding =
encodingForCharset(request.headers.contentType.charset);
var encoding = encodingForCharset(request.headers.contentType.charset);
requestBody = encoding.decode(requestBodyBytes);
} else {
requestBody = requestBodyBytes;
Expand Down
4 changes: 1 addition & 3 deletions test/multipart_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@ void main() {
// "Ã¥" encoded as ISO-8859-1 and then read as UTF-8 results in "å".
var files = [
new http.MultipartFile('file', 'non-ascii: "Ã¥"',
encoding: LATIN1,
contentType:
new MediaType('text', 'plain'))
encoding: LATIN1, contentType: new MediaType('text', 'plain'))
];
var request = new http.Request.multipart(dummyUrl, files: files);

Expand Down