06 December, 2012

A few stumbling steps with RestSharp

I have this little console application for creating tasks in Outlook. I usually use it from Launchy to create tasks fast ant trying to keep the context switch as low as possible.



The code can be found here: https://github.com/nippe/CreateTask

Trellofying it

pk_book I read the book Personal Kanban a while back and thought I would give it a try. I don’t have space for a physical board plus I want it on the go. So I landed on using Trello as tool for my Personal Kanban escapades.

A natural next step for my CreateTask application was for it to create Trello cards.

Digging into how this could be done I browsed through Trello’s api documentation. Not feeling like doing raw HttpWebRequests against the Trello JSON api, I looked around for frameworks that could help. Found some alternatives:

  • Chello – a .NET wrapper for the Trello api
  • RestSharp – an open source library for talking to REST endpoints
  • Trello.NET – another open source wrapper for the Trello api

Chello felt too big for my little app so I settled on RestSharp (I’ve heard about it before, github for windows, among others, uses it). I found Trello.NET after I went ahead with my RestSharp decision but it also contains a lot that I did not need.


To be able to talk to the Trello api, two things is needed:

  • Key – a key for the application, can be generated at https://trello.com/1/appKey/generate
  • Token – a security token for the user (unfortunately for my app I seem to only be able to get a write-token for 1 day)

Here we go, my first stab at creating a Trello card using the api from RestSharp.

RestClient client = RestClient("https://api.trello.com/1");

IRestRequest createCardRequest = new RestRequest("/cards");
createCardRequest.Method = Method.POST;
createCardRequest.AddParameter("key", key);
createCardRequest.AddParameter("token", token);

IRestRequest createResponse = client.Execute(createCardRequest);

First I get ahold of a RestClient that points to the root of the resources (https://api.trello.com/1). Then I create a RestRequest for the given resource (/cards”) and then I add on a few parameters, key and token among others. Sent it of to the server with the Execute command and I get back a bunch of JSON.

Works fine but I want to do more stuff like add due dates and labels. Which means I need to extract data, primarily the card id, from the response.

Mapping JSON to C# objects

I could use some JSON-library and parse the returned JSON to get the cards id and other properties OR I could use some built in RestSharp magic.

First of we need some c# classes to put the data in. Here’s where I stumbled upon http://json2csharp.com/. Which does exactly that, you feed it some JSON and get C# back.


(Got at top on twitter from @theDiverDK that Web Essentials plugin for Visual Studio 2012 also have similar functionality, but I haven’t tried it).

So using that I got the classes and renamed RootObject to TrelloCard and sprinkled with some generics in RestSharp the solution for getting properties like the id becomes much nicer:

var createResponse = client.Execute<TrelloCard>(createCardRequest);
TrelloCard card = createResponse.Data;
Console.WriteLine("Card id: {0}, card name: {1}", card.id, card.name);

Pretty neat I think. See how I just can use the id-property on the card object card.id.

Next step for me is to get the labels and due date in place, which hopefully will hit the Github repo within a couple of days after this is published.

This is (as the title reveals) my first stumbling steps with this so any feedback on how to improve this approach is greatly appreciated!

Tags: ,