Streamy is a feature-rich RPC/API framework for applications written using Dart programming language. It relies on Google API Discovery file format for API description. Streamy also provides out-of-the-box JSON-over-REST protocol.
Using Streamy your application can:
- Access many Google APIs, such as Google Calendar API
- Talk to your own APIs built using Google Cloud Endpoints
- Talk to your own APIs hosted on your own servers and described using Google Discovery format
- Dart SDK (add Dart's
binfolder to yourPATH) git(or Github for Windows/Mac)
While your application code will simply declare a dependency in pubspec.yaml and import it, Streamy comes with command-line tools to work with Discovery files, so you need to download the full version. The simplest way is to use git:
> git clone https://github.com/google/streamy-dart.git streamy-dart
> cd streamy-dart
Find one for the API you want to access. Let's use Google URL shortener as example:
> curl https://www.googleapis.com/discovery/v1/apis/urlshortener/v1/rest >urlshortener.json
> dart bin/apigen.dart \
--discovery_file=urlshortener.json \
--client_file=urlshortener.dart \
--library_name=urlshortener
The apigen.dart program provided by Streamy reads the discovery file (urlshortener.json in the example) and produces a Dart file (urlshortener.dart in the example) that contains an API client library for the API described in the discovery file. You can also give the library your own custom name (urlshortener in the example).
Let's create main.dart that contains this code:
import 'urlshortener.dart';
import 'package:streamy/impl_server.dart';
main() {
var requestHandler = new ServerRequestHandler();
var api = new Urlshortener(requestHandler);
api.url.insert(new Url()
..longUrl = 'http://www.google.com')
.send().listen((Url response) {
print('Shortened to ${response.id}');
});
}
We are done. Let's run the program:
> dart main.dart
This should print something like:
Shortened to http://goo.gl/fbsS
import 'urlshortener.dart';
This line imports the API client library that we just generated from the discovery file.
import 'package:streamy/impl_server.dart';
This import provides an implementation of Streamy's RequestHandler interface. This particular implementation works for server-side and command-line apps. If your app runs in the web-browser, use package:streamy/impl_html.dart and HtmlRequestHandler instead.
main() {
var requestHandler = new ServerRequestHandler();
var api = new Urlshortener(requestHandler);
The above two lines instantiate a Urlshortener API client backed by ServerRequestHandler.
api.url.insert(new Url()
..longUrl = 'http://www.google.com')
This is an example of how you create an API request. Streamy API generator generates a fully type-annotated root API class (Urlshortener), resources (url), resource methods (insert), entity classes (Url) as well as getters/setters for properties on the entities (
```longUrl``, so you can use auto-completion in your IDE and rely on compiler warnings to tell you about issues in your usage of the API.`) .send().listen((Url response) { print('Shortened to ${response.id}'); });
Finally, we .send() the request to the server and .listen(...) to a Stream of responses.
}