|
| 1 | +library cloud_api.browser; |
| 2 | + |
| 3 | +import "dart:async"; |
| 4 | +import "dart:html" as html; |
| 5 | +import "dart:convert"; |
| 6 | +import "dart:js" as js; |
| 7 | +import "package:google_oauth2_client/google_oauth2_browser.dart" as oauth; |
| 8 | + |
| 9 | +import 'client_base.dart'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Base class for all Browser API clients, offering generic methods for HTTP Requests to the API |
| 13 | + */ |
| 14 | +@deprecated |
| 15 | +abstract class BrowserClient implements ClientBase { |
| 16 | + |
| 17 | + static const _corsCallback = 'handleCLientLoad'; |
| 18 | + |
| 19 | + oauth.OAuth2 get auth; |
| 20 | + bool _jsClientLoaded = false; |
| 21 | + |
| 22 | + /** |
| 23 | + * Loads the JS Client Library to make CORS-Requests |
| 24 | + */ |
| 25 | + Future _loadJsClient() { |
| 26 | + |
| 27 | + if (_jsClientLoaded) { |
| 28 | + return new Future.value(); |
| 29 | + } |
| 30 | + |
| 31 | + var completer = new Completer(); |
| 32 | + |
| 33 | + js.context[_corsCallback] = () { |
| 34 | + _jsClientLoaded = true; |
| 35 | + completer.complete(); |
| 36 | + }; |
| 37 | + |
| 38 | + html.ScriptElement script = new html.ScriptElement(); |
| 39 | + script.src = "https://apis.google.com/js/client.js?onload=$_corsCallback"; |
| 40 | + script.type = "text/javascript"; |
| 41 | + html.document.body.children.add(script); |
| 42 | + |
| 43 | + return completer.future; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Makes a request via the JS Client Library to circumvent CORS-problems |
| 48 | + */ |
| 49 | + Future<Map<String, dynamic>> _makeJsClientRequest(String requestUrl, String method, {String body, String contentType, Map queryParams}) { |
| 50 | + var requestData = new Map(); |
| 51 | + requestData["path"] = requestUrl; |
| 52 | + requestData["method"] = method; |
| 53 | + requestData["headers"] = new Map(); |
| 54 | + |
| 55 | + if (queryParams != null) { |
| 56 | + requestData["params"] = queryParams; |
| 57 | + } |
| 58 | + |
| 59 | + if (body != null) { |
| 60 | + requestData["body"] = body; |
| 61 | + requestData["headers"]["Content-Type"] = contentType; |
| 62 | + } |
| 63 | + if (makeAuthRequests && auth != null && auth.token != null) { |
| 64 | + requestData["headers"]["Authorization"] = "${auth.token.type} ${auth.token.data}"; |
| 65 | + } |
| 66 | + |
| 67 | + var completer = new Completer(); |
| 68 | + var request = js.context["gapi"]["client"].callMethod("request", |
| 69 | + [new js.JsObject.jsify(requestData)]); |
| 70 | + var callback = (jsonResp, rawResp) { |
| 71 | + if (jsonResp == null || (jsonResp is bool && jsonResp == false)) { |
| 72 | + var raw = JSON.decode(rawResp); |
| 73 | + if (raw["gapiRequest"]["data"]["status"] >= 400) { |
| 74 | + completer.completeError(new APIRequestError("JS Client - ${raw["gapiRequest"]["data"]["status"]} ${raw["gapiRequest"]["data"]["statusText"]} - ${raw["gapiRequest"]["data"]["body"]}")); |
| 75 | + } else { |
| 76 | + completer.complete({}); |
| 77 | + } |
| 78 | + } else { |
| 79 | + completer.complete(js.context["JSON"].callMethod("stringify", [jsonResp])); |
| 80 | + } |
| 81 | + }; |
| 82 | + request.execute(callback); |
| 83 | + |
| 84 | + return completer.future; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Sends a HTTPRequest using [method] (usually GET or POST) to [requestUrl] using the specified [urlParams] and [queryParams]. Optionally include a [body] in the request. |
| 89 | + */ |
| 90 | + Future<Map<String, dynamic>> request(String requestUrl, String method, {String body, String contentType:"application/json", Map urlParams, Map queryParams}) { |
| 91 | + |
| 92 | + if (urlParams == null) urlParams = {}; |
| 93 | + if (queryParams == null) queryParams = {}; |
| 94 | + |
| 95 | + params.forEach((key, param) { |
| 96 | + if (param != null && queryParams[key] == null) { |
| 97 | + queryParams[key] = param; |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + var path; |
| 102 | + if (requestUrl.substring(0,1) == "/") { |
| 103 | + path ="$rootUrl${requestUrl.substring(1)}"; |
| 104 | + } else { |
| 105 | + path ="$rootUrl${basePath.substring(1)}$requestUrl"; |
| 106 | + } |
| 107 | + var url = oauth.UrlPattern.generatePattern(path, urlParams, queryParams); |
| 108 | + |
| 109 | + var request = new html.HttpRequest(); |
| 110 | + var completer = new Completer(); |
| 111 | + |
| 112 | + void handleError() { |
| 113 | + if (request.status == 0) { |
| 114 | + _loadJsClient().then((v) { |
| 115 | + if (requestUrl.substring(0,1) == "/") { |
| 116 | + path = requestUrl; |
| 117 | + } else { |
| 118 | + path ="$basePath$requestUrl"; |
| 119 | + } |
| 120 | + url = oauth.UrlPattern.generatePattern(path, urlParams, {}); |
| 121 | + _makeJsClientRequest(url, method, body: body, contentType: contentType, queryParams: queryParams) |
| 122 | + .then((response) { |
| 123 | + var data = JSON.decode(response); |
| 124 | + completer.complete(data); |
| 125 | + }) |
| 126 | + .catchError((e) { |
| 127 | + completer.completeError(e); |
| 128 | + return true; |
| 129 | + }); |
| 130 | + }); |
| 131 | + } else { |
| 132 | + var error = ""; |
| 133 | + if (request.responseText != null) { |
| 134 | + var errorJson; |
| 135 | + try { |
| 136 | + errorJson = JSON.decode(request.responseText); |
| 137 | + } on FormatException { |
| 138 | + errorJson = null; |
| 139 | + } |
| 140 | + if (errorJson != null && errorJson.containsKey("error")) { |
| 141 | + error = "${errorJson["error"]["code"]} ${errorJson["error"]["message"]}"; |
| 142 | + } |
| 143 | + } |
| 144 | + if (error == "") { |
| 145 | + error = "${request.status} ${request.statusText}"; |
| 146 | + } |
| 147 | + completer.completeError(new APIRequestError(error)); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + request.onLoad.listen((_) { |
| 152 | + if (request.status > 0 && request.status < 400) { |
| 153 | + var data = {}; |
| 154 | + if (!request.responseText.isEmpty) { |
| 155 | + data = JSON.decode(request.responseText); |
| 156 | + } |
| 157 | + completer.complete(data); |
| 158 | + } else { |
| 159 | + handleError(); |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + request.onError.listen((_) => handleError()); |
| 164 | + |
| 165 | + request.open(method, url); |
| 166 | + request.setRequestHeader("Content-Type", contentType); |
| 167 | + if (makeAuthRequests && auth != null) { |
| 168 | + auth.authenticate(request).then((request) => request.send(body)); |
| 169 | + } else { |
| 170 | + request.send(body); |
| 171 | + } |
| 172 | + |
| 173 | + return completer.future; |
| 174 | + } |
| 175 | +} |
0 commit comments