Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(636)

Unified Diff: sdk/lib/html/dartium/html_dartium.dart

Issue 12087077: Adding ease-of-use methods to HttpRequest. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
Download patch
Index: sdk/lib/html/dartium/html_dartium.dart
diff --git a/sdk/lib/html/dartium/html_dartium.dart b/sdk/lib/html/dartium/html_dartium.dart
index 2cf25d8b4276f58330ca7074d74a8a4935b1e706..04a7c52950a4f4a00420119fe8e31c28696ae409 100644
--- a/sdk/lib/html/dartium/html_dartium.dart
+++ b/sdk/lib/html/dartium/html_dartium.dart
@@ -13504,6 +13504,69 @@ class HttpRequest extends EventTarget {
onComplete(HttpRequest request)) =>
_HttpRequestUtils.get(url, onComplete, true);
+
+ /**
+ * Creates a URL get request for the specified `url`.
+ *
+ * The server response must be a `text/` mime type for this request to
+ * succeed.
+ */
+ static Future<String> getString(String url,
+ {bool withCredentials}) {
+ return request(url, withCredentials: withCredentials).then(
+ (xhr) {
+ return xhr.responseText;
+ });
+ }
+
+ /**
+ * Creates a URL request for the specified `url`.
+ *
+ * By default this will do an HTTP GET request, this can be overridden with
+ * [method].
+ *
+ * The Future is completed when the response is available.
+ */
+ static Future<HttpRequest> request(String url,
+ {String method, bool withCredentials, String responseType, sendData}) {
+ var completer = new Completer<String>();
+
+ var xhr = new HttpRequest();
+ if (method == null) {
+ method = 'GET';
+ }
+ xhr.open(method, url, true);
+
+ if (withCredentials != null) {
+ xhr.withCredentials = withCredentials;
+ }
+
+ if (responseType != null) {
+ xhr.responseType = responseType;
+ }
+
+ xhr.onLoad.listen((e) {
+ if (xhr.status >= 200 && xhr.status < 300 ||
+ xhr.status == 304 ) {
+ completer.complete(xhr);
+ } else {
+ completer.completeError(e);
+ }
+ });
+
+ xhr.onError.listen((e) {
+ completer.completeError(e);
+ });
+
+ if (sendData != null) {
+ xhr.send(sendData);
+ } else {
+ xhr.send();
+ }
+
+ return completer.future;
+ }
+
HttpRequest.internal() : super.internal();
@DomName('XMLHttpRequest.abort')

Powered by Google App Engine
This is Rietveld 408576698