Skip to content

Commit

Permalink
move withCredentials from BaseClient to BrowserClient
Browse files Browse the repository at this point in the history
  • Loading branch information
johnryan-wf committed Apr 5, 2015
1 parent 59483ad commit 9d76e5e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
4 changes: 3 additions & 1 deletion lib/browser_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ class BrowserClient extends BaseClient {
/// Creates a new HTTP client.
BrowserClient();

bool withCredentials = false;

/// Sends an HTTP request and asynchronously returns the response.
Future<StreamedResponse> send(BaseRequest request) {
return request.finalize().toBytes().then((bytes) {
var xhr = new HttpRequest();
_xhrs.add(xhr);
xhr.open(request.method, request.url.toString(), async: true);
xhr.responseType = 'blob';
xhr.withCredentials = request.withCredentials;
xhr.withCredentials = withCredentials;
request.headers.forEach(xhr.setRequestHeader);

var completer = new Completer();
Expand Down
31 changes: 15 additions & 16 deletions lib/src/base_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ abstract class BaseClient implements Client {
/// can be a [Uri] or a [String].
///
/// For more fine-grained control over the request, use [send] instead.
Future<Response> head(url, {Map<String, String> headers, bool withCredentials}) =>
_sendUnstreamed("HEAD", url, headers, null, null, withCredentials);
Future<Response> head(url, {Map<String, String> headers}) =>
_sendUnstreamed("HEAD", url, 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, bool withCredentials}) =>
_sendUnstreamed("GET", url, headers, null, null, withCredentials);
Future<Response> get(url, {Map<String, String> headers}) =>
_sendUnstreamed("GET", url, 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 @@ -53,8 +53,8 @@ abstract class BaseClient implements Client {
///
/// For more fine-grained control over the request, use [send] instead.
Future<Response> post(url, {Map<String, String> headers, body,
Encoding encoding, bool withCredentials}) =>
_sendUnstreamed("POST", url, headers, body, encoding, withCredentials);
Encoding encoding}) =>
_sendUnstreamed("POST", url, headers, body, 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 @@ -75,15 +75,15 @@ abstract class BaseClient implements Client {
///
/// For more fine-grained control over the request, use [send] instead.
Future<Response> put(url, {Map<String, String> headers, body,
Encoding encoding, bool withCredentials}) =>
_sendUnstreamed("PUT", url, headers, body, encoding, withCredentials);
Encoding encoding}) =>
_sendUnstreamed("PUT", url, headers, body, encoding);

/// Sends an HTTP DELETE 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> delete(url, {Map<String, String> headers, bool withCredentials}) =>
_sendUnstreamed("DELETE", url, headers, null, null, withCredentials);
Future<Response> delete(url, {Map<String, String> headers}) =>
_sendUnstreamed("DELETE", url, 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
Expand All @@ -94,8 +94,8 @@ abstract class BaseClient implements Client {
///
/// For more fine-grained control over the request and response, use [send] or
/// [get] instead.
Future<String> read(url, {Map<String, String> headers, bool withCredentials}) {
return get(url, headers: headers, withCredentials: withCredentials).then((response) {
Future<String> read(url, {Map<String, String> headers}) {
return get(url, headers: headers).then((response) {
_checkResponseSuccess(url, response);
return response.body;
});
Expand All @@ -110,8 +110,8 @@ abstract class BaseClient implements Client {
///
/// For more fine-grained control over the request and response, use [send] or
/// [get] instead.
Future<Uint8List> readBytes(url, {Map<String, String> headers, bool withCredentials}) {
return get(url, headers: headers, withCredentials: withCredentials).then((response) {
Future<Uint8List> readBytes(url, {Map<String, String> headers}) {
return get(url, headers: headers).then((response) {
_checkResponseSuccess(url, response);
return response.bodyBytes;
});
Expand All @@ -128,12 +128,11 @@ abstract class BaseClient implements Client {

/// Sends a non-streaming [Request] and returns a non-streaming [Response].
Future<Response> _sendUnstreamed(String method, url,
Map<String, String> headers, [body, Encoding encoding, bool withCredentials = false]) {
Map<String, String> headers, [body, Encoding encoding]) {
return syncFuture(() {
if (url is String) url = Uri.parse(url);
var request = new Request(method, url);

request.withCredentials = withCredentials;
if (headers != null) request.headers.addAll(headers);
if (encoding != null) request.encoding = encoding;
if (body != null) {
Expand Down
2 changes: 0 additions & 2 deletions lib/src/base_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ abstract class BaseRequest {
/// The URL to which the request will be sent.
final Uri url;

bool withCredentials = false;

/// The size of the request body, in bytes.
///
/// This defaults to `null`, which indicates that the size of the request is
Expand Down

0 comments on commit 9d76e5e

Please sign in to comment.