Marcos Placona Blog

Programming, technology and the taming of the web.

Dart Pub packages stats

Reading time: 2 – 2 minutes

So modulecounts came to my attention, and I thought the idea was pretty neat.

I then contacted Erik (the author), and asked if he would mind also adding Dart.

He replied saying he wouldn’t mind, but quite rightly pointed out there seemed to be no way on Dart’s package manager to actually find out how many packages were live.

I asked some community members on G+, and while they seemed to have some solutions that kinda worked, there didn’t seem to be anything accurate or likely to work every time.

My solution was to then quickly knock-up a scraper up that would navigate through all pages on the website, grab some information and counts, aggregate and then generate a JSON output that could then be used by anyone trying to get some package information.

The JSON package is generated once a day as to not overload the website.

I then created a very simple app-engine client application that consumes the JSON packet, and shows information about it.

Check it out here: http://pub-stats.appspot.com

Source code can also be seen in my GitHub account: https://github.com/mplacona/pub-stats

I have also exposed another endpoint for anyone wanting to use the JSON packet on their applications.

The packet is also cached daily, to make sure my app-engine account doesn’t get abused :-)

You can see the JSON endpoint here: http://pub-stats.appspot.com/json

Collaboration and pull requests welcome!

Sending and Receiving SMS messages with Dart

Reading time: 5 – 8 minutes

Dart - SMS example

I wanted to be able to send SMS messages with Dart, and because it doesn’t actually support it natively (not that I would expect it to anyway), I decided to write a library that allows me to do just that.

The library itself is a wrapper to the Twilio API, which is a wicked service that allows you to not only send or receive SMS/MMS messages, but also lets you make phone calls, start conference calls etc, all through their API. This offers immense scope to build applications on top of, since you could build an entire telecommunications central on top of their infrastructure without having to invest largely on buying and maintaining your own kit.

The principle is that all you should need to do before you can get coding, is crate a Twilio account, and make a note of the account ID and the token it will provide you with. After that, you can get coding.

Intro

In a Dart application, you can do the following to get the library installed:

  1. Add a dependency to twilio_dart to your pubspec.yaml
  2. Run pub get

With the library now installed on your application, you can start using it as such:

import 'package:twilio_dart/twilio.dart';
//
var key = "your_twilio_key";
var authToken = "your_auth_token";
var version = "2010-04-01";
Twilio twilio = new Twilio(key, authToken, version);

This gives you a Twilio object, and you can use that to call the methods in the API. Make sure you replace key and authToken with the appropriate values provided upon registration with Twilio. You can also find them on your dashboard.

Everything being OK so far, you should now be able to actually send an SMS message using the number Twilio gave you when you created your account. You can also find this on your Twilio account under the numbers section.

Sending

An SMS message is composed of three main parts:

  1. A from number – this should be the number Twilio provided you.
  2. A to number – this can be any number capable of receiving text messages (i.e. your mobile number).
  3. A message – This is the text message you want to send.
var from = "your_twilio_phone";
var to = "your_mobile_number";
var body = "Look ma! Dart can now send SMS's in under 15 lines";

All there is to do now is actually try and send this message. To do so, we will use the sendSMS method. The signature for it is as follows:

Future sendSMS(String from, String to, String body, [String mediaUrl = null])

So straight away we know this method will return a Future, and that it takes a from, a to, a body, and optionally the URL for an image, which will then turn your message into an MMS by attaching that media directly to your message.

Therefore our code should look like the following when completed:

import 'package:twilio_dart/twilio.dart';
main() {
    var key = "your_twilio_key";
    var authToken = "your_auth_token";
    var version = "2010-04-01";
    var from = "your_twilio_phone";
    var to = "your_mobile_number";
    var body = "Look ma! Dart can now send SMS's in under 15 lines";
    //
    //create a new twilio object
    Twilio twilio = new Twilio(key, authToken, version);
    //
    // Send SMS for LOLZ
    twilio.sendSMS(from, to, body).then((response) => print("Your message has been sent!")).catchError((error) => print(error.toString()));
}

Which even with all the line breaks and comments, still is just 15 lines.

If you execute this code, you will be able to verify two things:

  • you’ve now got a new message on the phone you’ve chosen
  • under your Twilio account, you will also be able to read this message if you view the logs.

Listing

I bet you’re somehow impressed by now as to how little you actually had to code to get our little application to send an SMS message. But we need a way to be able to retrieve those messages from the server without having to have a device in our hands all the time. the next thing we’re going to do, is come up with a way to list our messages. We can do that by using the method readSMSList, which does exactly what it says in the tin, it will give you a list of all the SMS messages in an account.

twilio.readSMSList().then((response){
        JSON.decode(response)['messages'].forEach((e) => print(e));       
    }).catchError((error) => print(error.toString()));

Reading

Very simple and very neat. Get a JSON packet back, and iterate through the results. Each of the messages has a sid, which is the ID for this message, so the next thing we need to do, is to be able to drill down to each of the messages from a list of messages.

To do so, we will use the method readSMS, which takes a message id as a parameter, and returns its full contents.

twilio.readSMS(sId).then((response) => print(response.toString())).catchError((error) => print(error.toString()));

And the above will give you all the information pertaining a single message.

Wrap up…

So in this article, we’ve gone through the following:

  • how to send an SMS message with Dart through the Twilio API by using the method sendSMS.
  • how to read a list of received messages from the server by using the method readSMSList.
  • how to read the contents of a single message by using the method readSMS.

The source code to the library can be found on my GitHub account along with samples and documentation.

Creating files with Dart

Reading time: < 1 minute

This is a very simple example of how to create files with Dart. I am working on another example that involves file manipulation, but thought I’d quickly post this one here as to have it out of the way.

In this post I will show you quickly how to create a text file using Futures in Dart. The example will:

  • create the file
  • write some content to it synchronously
  • return a message when the contents have been written

Our code will therefore go as follows:

Dart - Create File

The gist can also be found on GitHub.

 

Dart – Futures and HTTP requests

Reading time: 2 – 3 minutes

I wrote a little article about how to retrieve gists from GitHub, and +Seth Ladd quite rightly pointed something out.

Seth Ladd makes a good point

Let me elaborate a little bit on what he means.

 getGistsForUser(String user){
this.user = user;
var url = "https://api.github.com/users/${this.user}/gists";
http.get(url)
  .then(_processResults)
  .catchError(_handleError);
}

In my sample code (cut down for brevity, but you can still check it out here), I am indeed using Futures, so the thread is not locked to this request, and my code could be happily doing other things. However, I’m not informing the client about this future, or allowing it to make use of it.

So when I make a request to it, I’m simply doing:

gists.getGistsForUser(username);

Which works in this case as my getGistsForUser method is dealing with displaying the gists itself; therefore the client isn’t waiting as the method returns void.

If I then wanted to get my client know exactly when the method had returned, I would need to make changes to my code by simply making the method getGistsForUser return a Future. This is what the code would look like after making a few changes:

As you can see, I’ve only updated a few things, but now my response is handled by my client.

To simplify, I have written a much simpler example that only handles an HTTP request, without prompting the user for any information.

And this is how you go about returning futures from HTTP requests

Retrieving GitHub’s gists with Dart

Reading time: 1 – 2 minutes

Dart Gists

I just thought I’d write this simple example that shows how to retrieve your GitHub’s gists with Dart language. The focus on this is to show how to handle Futures and make HTTP requests in Dart.

The code is very simple, and runs as a CLI application. It asks the user for a valid Github login, and tries to fetch the last 30 gist entries for that user.

The main class is as follows:

As you can see, I do an HTTP get to Github’s API requesting the gists for my user. I then use Futures to make sure my application doesn’t cause blockage while we’re fetching the results via the API. I also do some processing on the results, by parsing the JSON response and printing each of the URL’s.

If the user is invalid, we then display an error message.

Full code with a sample call:

« Older posts