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 = <span class="kwrd">new</span> Hammock.RestClient {
    Authority = <span class="str">"https://api.bitbucket.org"</span>,
    VersionPath = <span class="str">"1.0"</span>,
    Credentials = <span class="kwrd">new</span> BasicAuthCredentials {
        Username = <span class="str">"bitbucket-username"</span>, 
        Password = <span class="str">"bitbucket-password"</span>
    }
};

var request = <span class="kwrd">new</span> RestRequest {
     Path = <span class="str">"repositories/[username]/[repo]/changesets"</span>,
     Method = WebMethod.Get
};

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