BitBucket API from Windows Phone 7 and C#

Posted by on

C#

With my latest update of CodeTrack (1.2) I implemented BitBucket as a provider, so you can watch repositories from there now as well. On the app itself there are a few search functions using the BitBucket API, this post will provide some insight and example code for doing this.

So basically I wanted to go from having nothing at all (no BitBucket user, no username, no repo slug/id) to being able to search for repositories by either their name or their owners username.

Note: The BitBucket API has recently changed to allow you to do this by giving the repository responses the Onwer property.

As always, RestSharp was used in the making of this code sample.

The 2 requests share the same response class BitBucketRepoList, this class is pretty simple because we are calling the API anonymously where using a BitBucket username/password would return a more detailed response.

public class BitBucketRepoList
{
    public List<BitBucketRepo> Repositories { get; set; }
}

public class BitBucketRepo
{
    public string Website { get; set; }
    public string Slug { get; set; }
    public string Name { get; set; }
    public string Owner { get; set; }
    public int FollowersCount { get; set; }
    public string Description { get; set; }
}

Searching by Repo Name

This request is pretty straight forward, RestClient with the base url for the API, RestRequest with the Resource “repositories” then add a get parameter of "name” with the search term. (API Doco)

_client = new RestClient("https://api.bitbucket.org/1.0");
var request = new RestRequest("repositories/");

request.AddParameter("name", searchTerm);

_client.ExecuteAsync&lt;BitBucketRepoList&gt;(request, (response) =&gt;
{
    //Do stuff here with response
});

Searching by Repo Owner

Now for this request, BitBucket API doesnt actually support searching on usernames so these must be exact matches on the username.

_client = new RestClient("https://api.bitbucket.org/1.0");
var request = new RestRequest("users/{username}");

request.AddParameter("username", username, ParameterType.UrlSegment);

_client.ExecuteAsync&lt;BitBucketRepoList&gt;(request, (response) =&gt;
{
    //Do stuff here with response
});

And there you have it, BitBucket API from Windows Phone 7. (The above code will also work in Silverlight/WPF as well as Win forms or ASP.NET depending on the version of RestSharp used.)