Http in Dart?

45 views
Skip to first unread message

Douglas Miller

unread,
Jan 22, 2016, 12:08:35 AM1/22/16
to AngularJS
The api docs say there's an angular2/http library in JavaScript and TypeScript, but as far as I can tell it doesn't exist in the Dart version. Am I missing something? Is it not implemented yet? Something else?

The Dart api docs essentially say "see JavaScript docs" full stop, and I was under the impression that the use of code generation meant all three languages would have all exactly the same Angular features, so I'm a bit surprised and disappointed if this is missing. Searching through the Dart source that pub got for me, angular2/http does not exist and only one class (angular2/src/services/xhr_impl) does anything with http, and that class appears to be intended for internal use only.

Günter Zöchbauer

unread,
Jan 22, 2016, 7:00:53 AM1/22/16
to AngularJS
There is no Dart implementation for Http. You can just use what `dart:html` or `package:http` provide or create your own wrapper class if you want specific functionality. 

Douglas Miller

unread,
Jan 22, 2016, 1:42:35 PM1/22/16
to AngularJS
But does that work with Angular's dependency injection? I want to be able to mock it in tests.

...some quick googling says probably yes, I think. I'm new to Dart as well as Angular2, looks like I should have explored what Dart itself provides a bit more.
Message has been deleted

Günter Zöchbauer

unread,
Jan 22, 2016, 4:52:40 PM1/22/16
to AngularJS
I built this code just to get tests running I ported from TypeScript to Dart

import 'dart:async' show Future;
import 'dart:html' show HttpRequest;
import 'dart:convert' show JSON;

class Http {
Future<Response> get(String uri) async {
final HttpRequest req = new HttpRequest()..open('GET', uri);
await req.onLoadEnd.first;
req.send();
return new Response(req);
}
}

class Response {
final HttpRequest request;
Response(this.request);

String text() => request.responseText;
dynamic json() => JSON.decode(request.response);
} 

It has only a `get()` method, but other methods are quite similar.
You can get inspiration from the TypeScript source about what features to add.
For mocking, just extend it `class MockHttp extends Http {` and override the methods with dummy actions.

Douglas Miller

unread,
Jan 22, 2016, 10:46:36 PM1/22/16
to AngularJS
I just spent a few hours wrestling with it (the error messages I got weren't very meaningful), and it appears Angular 2's injector can't handle any class not annotated with @Injectable or another Angular-specific annotation. This means Dart's existing non-Angular classes for http handling cannot be injected, and thus cannot have mocks injected in their place. I can write injectable wrappers easily enough, but that's exactly the sort of boilerplate busywork that Angular is supposed to help avoid.

So, I guess I'll write injectable wrappers for now, but having angular2/http available in Dart would be much preferred. I suppose injection of non-annotated classes that have a suitable constructor might also work, but HttpRequest in dart:html seems to be intended for a single request and BrowserClient in package:http says it can't do streaming. What would be the best way to try to get this handled long term, submit an issue report on github?

Günter Zöchbauer

unread,
Jan 23, 2016, 7:59:00 AM1/23/16
to AngularJS
AFAIR there was an issue and they declined to implement it. They don't want to maintain all kinds of features usually requested around HTTP as part of Angular and I understand.
AFAIR I used streaming with the http package. I don't remember where. If I find the code I can post it. It wasn't difficult, only a bit difficult to find.

Günter Zöchbauer

unread,
Jan 23, 2016, 9:16:35 AM1/23/16
to AngularJS
Reply all
Reply to author
Forward
0 new messages