Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 39a143d

Browse files
mvuksanomhevery
authored andcommitted
chore(Http): remove deprecated getString method
Closes #199 Closes #930
1 parent 90f7582 commit 39a143d

File tree

6 files changed

+101
-134
lines changed

6 files changed

+101
-134
lines changed

lib/core_dom/http.dart

+41-86
Original file line numberDiff line numberDiff line change
@@ -387,16 +387,6 @@ class Http {
387387
Http(this._cookies, this._location, this._rewriter, this._backend,
388388
this.defaults, this._interceptors);
389389

390-
/**
391-
* DEPRECATED
392-
*/
393-
async.Future<String> getString(String url, {bool withCredentials,
394-
void onProgress(dom.ProgressEvent e), Cache cache}) =>
395-
request(url,
396-
withCredentials: withCredentials,
397-
onProgress: onProgress,
398-
cache: cache).then((HttpResponse xhr) => xhr.responseText);
399-
400390
/**
401391
* Parse a [requestUrl] and determine whether this is a same-origin request as
402392
* the application document.
@@ -442,6 +432,7 @@ class Http {
442432
throw ['timeout not implemented'];
443433
}
444434

435+
url = _rewriter(url);
445436
method = method.toUpperCase();
446437

447438
if (headers == null) headers = {};
@@ -461,8 +452,7 @@ class Http {
461452
});
462453

463454
var serverRequest = (HttpResponseConfig config) {
464-
assert(config.data == null || config.data is String ||
465-
config.data is dom.File);
455+
assert(config.data == null || config.data is String || config.data is dom.File);
466456

467457
// Strip content-type if data is undefined
468458
if (config.data == null) {
@@ -471,12 +461,45 @@ class Http {
471461
.forEach((h) => headers.remove(h));
472462
}
473463

474-
return request(null,
475-
config: config,
476-
method: method,
477-
sendData: config.data,
478-
requestHeaders: config.headers,
479-
cache: cache);
464+
url = _buildUrl(config.url, config.params);
465+
466+
if (cache == false) {
467+
cache = null;
468+
} else if (cache == null) {
469+
cache = defaults.cache;
470+
}
471+
472+
// We return a pending request only if caching is enabled.
473+
if (cache != null && _pendingRequests.containsKey(url)) {
474+
return _pendingRequests[url];
475+
}
476+
var cachedResponse = (cache != null && method == 'GET') ? cache.get(url) : null;
477+
if (cachedResponse != null) {
478+
return new async.Future.value(new HttpResponse.copy(cachedResponse));
479+
}
480+
481+
var result = _backend.request(url,
482+
method: method,
483+
requestHeaders: config.headers,
484+
sendData: config.data).then((dom.HttpRequest value) {
485+
// TODO: Uncomment after apps migrate off of this class.
486+
// assert(value.status >= 200 && value.status < 300);
487+
488+
var response = new HttpResponse(value.status, value.responseText,
489+
parseHeaders(value), config);
490+
491+
if (cache != null) cache.put(url, response);
492+
_pendingRequests.remove(url);
493+
return response;
494+
}, onError: (error) {
495+
if (error is! dom.ProgressEvent) throw error;
496+
dom.ProgressEvent event = error;
497+
_pendingRequests.remove(url);
498+
dom.HttpRequest request = event.currentTarget;
499+
return new async.Future.error(
500+
new HttpResponse(request.status, request.response, parseHeaders(request), config));
501+
});
502+
return _pendingRequests[url] = result;
480503
};
481504

482505
var chain = [[serverRequest, null]];
@@ -632,81 +655,13 @@ class Http {
632655
});
633656
return parsed;
634657
}
635-
636658
/**
637659
* Returns an [Iterable] of [Future] [HttpResponse]s for the requests
638660
* that the [Http] service is currently waiting for.
639661
*/
640662
Iterable<async.Future<HttpResponse> > get pendingRequests =>
641663
_pendingRequests.values;
642664

643-
/**
644-
* DEPRECATED
645-
*/
646-
async.Future<HttpResponse> request(String rawUrl,
647-
{ HttpResponseConfig config,
648-
String method: 'GET',
649-
bool withCredentials: false,
650-
String responseType,
651-
String mimeType,
652-
Map<String, String> requestHeaders,
653-
sendData,
654-
void onProgress(dom.ProgressEvent e),
655-
/*Cache<String, HttpResponse> or false*/ cache }) {
656-
String url;
657-
658-
if (config == null) {
659-
url = _rewriter(rawUrl);
660-
config = new HttpResponseConfig(url: url);
661-
} else {
662-
url = _buildUrl(config.url, config.params);
663-
}
664-
665-
if (cache == false) {
666-
cache = null;
667-
} else if (cache == null) {
668-
cache = defaults.cache;
669-
}
670-
// We return a pending request only if caching is enabled.
671-
if (cache != null && _pendingRequests.containsKey(url)) {
672-
return _pendingRequests[url];
673-
}
674-
var cachedResponse = (cache != null && method == 'GET')
675-
? cache.get(url)
676-
: null;
677-
if (cachedResponse != null) {
678-
return new async.Future.value(new HttpResponse.copy(cachedResponse));
679-
}
680-
681-
var result = _backend.request(url,
682-
method: method,
683-
withCredentials: withCredentials,
684-
responseType: responseType,
685-
mimeType: mimeType,
686-
requestHeaders: requestHeaders,
687-
sendData: sendData,
688-
onProgress: onProgress).then((dom.HttpRequest value) {
689-
// TODO: Uncomment after apps migrate off of this class.
690-
// assert(value.status >= 200 && value.status < 300);
691-
692-
var response = new HttpResponse(value.status, value.responseText,
693-
parseHeaders(value), config);
694-
695-
if (cache != null) cache.put(url, response);
696-
_pendingRequests.remove(url);
697-
return response;
698-
}, onError: (error) {
699-
if (error is! dom.ProgressEvent) throw error;
700-
dom.ProgressEvent event = error;
701-
_pendingRequests.remove(url);
702-
dom.HttpRequest request = event.currentTarget;
703-
return new async.Future.error(
704-
new HttpResponse(request.status, request.response,
705-
parseHeaders(request), config));
706-
});
707-
return _pendingRequests[url] = result;
708-
}
709-
710665
_buildUrl(String url, Map<String, dynamic> params) {
711666
if (params == null) return url;
712667
var parts = [];

lib/core_dom/view_factory.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ class ViewCache {
138138
}
139139

140140
async.Future<ViewFactory> fromUrl(String url, DirectiveMap directives) {
141-
return http.getString(url, cache: templateCache).then(
142-
(html) => fromHtml(html, directives));
141+
return http.get(url, cache: templateCache).then(
142+
(resp) => fromHtml(resp.responseText, directives));
143143
}
144144
}
145145

@@ -182,8 +182,8 @@ class _ComponentFactory implements Function {
182182
var cssUrls = []..addAll(_baseCss.urls)..addAll(component.cssUrls);
183183
if (cssUrls.isNotEmpty) {
184184
cssUrls.forEach((css) => cssFutures.add(http
185-
.getString(css, cache: templateCache)
186-
.catchError((e) => '/*\n$e\n*/\n')
185+
.get(css, cache: templateCache).then((resp) => resp.responseText,
186+
onError: (e) => '/*\n$e\n*/\n')
187187
));
188188
} else {
189189
cssFutures.add(new async.Future.value(null));

test/core/templateurl_spec.dart

+19-11
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,12 @@ void main() {
9090
it('should replace element with template from url', async(inject(
9191
(Http http, Compiler compile, Scope rootScope, Logger log,
9292
Injector injector, MockHttpBackend backend, DirectiveMap directives) {
93-
backend.expectGET('simple.html').respond('<div log="SIMPLE">Simple!</div>');
93+
backend.expectGET('simple.html').respond(200, '<div log="SIMPLE">Simple!</div>');
9494

9595
var element = es('<div><simple-url log>ignore</simple-url><div>');
9696
compile(element, directives)(injector, element);
9797

98+
microLeap();
9899
backend.flush();
99100
microLeap();
100101

@@ -107,7 +108,7 @@ void main() {
107108
it('should load template from URL once', async(inject(
108109
(Http http, Compiler compile, Scope rootScope, Logger log,
109110
Injector injector, MockHttpBackend backend, DirectiveMap directives) {
110-
backend.whenGET('simple.html').respond('<div log="SIMPLE">Simple!</div>');
111+
backend.whenGET('simple.html').respond(200, '<div log="SIMPLE">Simple!</div>');
111112

112113
var element = es(
113114
'<div>'
@@ -116,6 +117,7 @@ void main() {
116117
'<div>');
117118
compile(element, directives)(injector, element);
118119

120+
microLeap();
119121
backend.flush();
120122
microLeap();
121123

@@ -130,12 +132,13 @@ void main() {
130132
(Http http, Compiler compile, Scope rootScope, Logger log,
131133
Injector injector, MockHttpBackend backend, DirectiveMap directives) {
132134
backend
133-
..expectGET('simple.css').respond('.hello{}')
134-
..expectGET('simple.html').respond('<div log="SIMPLE">Simple!</div>');
135+
..expectGET('simple.css').respond(200, '.hello{}')
136+
..expectGET('simple.html').respond(200, '<div log="SIMPLE">Simple!</div>');
135137

136138
var element = e('<div><html-and-css log>ignore</html-and-css><div>');
137139
compile([element], directives)(injector, [element]);
138140

141+
microLeap();
139142
backend.flush();
140143
microLeap();
141144

@@ -152,9 +155,10 @@ void main() {
152155
(Http http, Compiler compile, Scope rootScope, Injector injector,
153156
MockHttpBackend backend, DirectiveMap directives) {
154157
var element = es('<div><inline-with-css log>ignore</inline-with-css><div>');
155-
backend.expectGET('simple.css').respond('.hello{}');
158+
backend.expectGET('simple.css').respond(200, '.hello{}');
156159
compile(element, directives)(injector, element);
157160

161+
microLeap();
158162
backend.flush();
159163
microLeap();
160164
expect(element[0]).toHaveText('.hello{}inline!');
@@ -167,6 +171,7 @@ void main() {
167171
backend.expectGET('simple.css').respond(500, 'some error');
168172
compile(element, directives)(injector, element);
169173

174+
microLeap();
170175
backend.flush();
171176
microLeap();
172177
expect(element.first).toHaveText(
@@ -180,9 +185,10 @@ void main() {
180185
(Http http, Compiler compile, Scope rootScope, Injector injector,
181186
MockHttpBackend backend, DirectiveMap directives) {
182187
var element = es('<div><only-css log>ignore</only-css><div>');
183-
backend.expectGET('simple.css').respond('.hello{}');
188+
backend.expectGET('simple.css').respond(200, '.hello{}');
184189
compile(element, directives)(injector, element);
185190

191+
microLeap();
186192
backend.flush();
187193
microLeap();
188194
expect(element[0]).toHaveText('.hello{}');
@@ -192,12 +198,13 @@ void main() {
192198
(Http http, Compiler compile, Scope rootScope, Injector injector,
193199
MockHttpBackend backend, DirectiveMap directives) {
194200
backend
195-
..expectGET('simple.css').respond('.hello{}')
196-
..expectGET('simple.html').respond('<div>Simple!</div>');
201+
..expectGET('simple.css').respond(200, '.hello{}')
202+
..expectGET('simple.html').respond(200, '<div>Simple!</div>');
197203

198204
var element = es('<html-and-css>ignore</html-and-css>');
199205
compile(element, directives)(injector, element);
200206

207+
microLeap();
201208
backend.flush();
202209
microLeap();
203210
expect(element.first).toHaveText('.hello{}Simple!');
@@ -215,13 +222,14 @@ void main() {
215222
(Http http, Compiler compile, Scope rootScope, Logger log,
216223
Injector injector, MockHttpBackend backend, DirectiveMap directives) {
217224
backend
218-
..expectGET('simple.css').respond('.hello{}')
219-
..expectGET('another.css').respond('.world{}')
220-
..expectGET('simple.html').respond('<div log="SIMPLE">Simple!</div>');
225+
..expectGET('simple.css').respond(200, '.hello{}')
226+
..expectGET('another.css').respond(200, '.world{}')
227+
..expectGET('simple.html').respond(200, '<div log="SIMPLE">Simple!</div>');
221228

222229
var element = e('<div><html-and-css log>ignore</html-and-css><div>');
223230
compile([element], directives)(injector, [element]);
224231

232+
microLeap();
225233
backend.flush();
226234
microLeap();
227235

test/core_dom/compiler_spec.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ void main() {
480480
});
481481

482482
it('should fire onTemplate method', async((Compiler compile, Logger logger, MockHttpBackend backend) {
483-
backend.whenGET('some/template.url').respond('<div>WORKED</div>');
483+
backend.whenGET('some/template.url').respond(200, '<div>WORKED</div>');
484484
var scope = _.rootScope.createChild({});
485485
scope.context['isReady'] = 'ready';
486486
scope.context['logger'] = logger;
@@ -503,6 +503,7 @@ void main() {
503503
expect(logger).toEqual(expected);
504504
logger.clear();
505505

506+
microLeap();
506507
backend.flush();
507508
microLeap();
508509
expect(logger).toEqual(['templateLoaded', _.rootScope.context['shadowRoot']]);

0 commit comments

Comments
 (0)