Posts

Showing posts from December, 2011

Getting the command line arguments in Dart (vm)

Another hidden gem in the Dart library today, can you tell I am trying to write a command line thingy? Reading the command line options is really easy. void main() { Options opts = new Options(); for (String arg in opts.arguments) { print(arg); } } Let us play: $ dart opts.dart a a $ dart opts.dart a b c d a b c d Nothing to add, I think, except check the source . Comments on Google+ .

OAuth2.0 with Dart in a web browser

Image
I was trying to access a Google GData API using Dart in a browser. More for fun than profit, but it posed a few challenges, so I thought I should document them. The first step was to Authorize using OAuth2.0 which is the easiest way of Authorizing an application to use Google APIs. This is pretty trivial using server-side Dart, but I wanted to achieve the entire thing inside a web browser, and so this involves a different OAuth2.0 flow to the web-server flow that I am used to using, in this case the OAuth2.0 Client-side Application Flow . The main difference with the web-server flow is that the entire OAuth2.0 dance occurs in a single step, i.e., there is no step to exchange a code for a token. This single step is performed by asking for a token in the initial redirect step with a response_type parameter of token . Additionally, the client secret is not passed at any time, because this would not be safe information for the client application to give up to a web browser (sin...

Running a subprocess with Dart and reading its output

Starting a foreign program, and reading its standard output stream is useful, and here is an example of listing the root directory. void main() { Process p = new Process('/bin/ls', ['/']); p.start(); StringInputStream stdoutStream = new StringInputStream(p.stdout); print(stdoutStream.read()); p.close(); } Notes: Argument list does not include the executable name like it usually does Use a StringInputStream to read an InputStream as a String Close the process or the script will hang forever Comments on Google+ .

Reading a file as a String in Dart

It took me a minute to work out how to read a file in Dart (vm), so here it is for posterity: void main() { File f = new File('my_filename.txt'); FileInputStream fileStream = f.openInputStream(); StringInputStream stringStream = new StringInputStream(fileStream); print(stringStream.read()); fileStream.close(); } Notes: A StringInputStream reads strings from an InputStream A FileInputStream is an implementor of InputStream that is opened from files Remember to close the FileInputStream Comments on Google+