From .NET, Calling REST and getting "dynamic" results

It seems like every time I want to call a RESTful service from my .NET app I have to re-lookup the documentation for either Hammock or RestSharp. Mainly for my own future reference, here’s the code using Hammock to get a change list from bitbucket.org’s REST API, then using JsonFx to deserialize the JSON to a dynamic object (which is pretty sweet).

var client = new Hammock.RestClient {
    Authority = "https://api.bitbucket.org",
    VersionPath = "1.0",
    Credentials = new BasicAuthCredentials {
        Username = "bitbucket-username", 
        Password = "bitbucket-password"
    }
};

var request = new RestRequest {
     Path = "repositories/[username]/[repo]/changesets",
     Method = WebMethod.Get
};

dynamic result = new JsonFx.Json.JsonReader().Read(response.Content);
for(int i=0, len=result.changesets.Length; i<len; i++) {
    var date = DateTime.Parse(result.changesets[i].utctimestamp);
    string author = result.changesets[i].raw_author;
    // do what you want with the results.
}
This entry was posted in CodeMinder, JSON, Programming, REST. Bookmark the permalink.