Playing with the GitHub API in your ASP.NET MVC application


Guest Post by Ivan Lukianchuk

Ivan Lukianchuk is a seasoned startup founder and award winning pitch artist turned consultant who currently runs Strattenburg Inc.   He's a full stack .NET developer who has done a lot of work with Azure over the years building entire solutions from the ground up for clients ranging from independent professionals, to funded startups to $50MM organizations.You can follow Ivan on twitter: @Chronos or contact him via email at ivan@strattenburg.com

Lately I’ve been playing around a lot with various APIs as I hunt for interesting public information and demographic trends. As a developer, what better place to start than with our near and dear GitHub! GitHub is a fantastic place for developers to store their work, post new projects to share, and find helpful bits of code to make their lives easier. Many Hacker News articles link directly to GitHub repositories (repos) and many in the field consider their GitHub account as their true resume.

For me, I’m more of a private user of GitHub, it’s a place where I store all my various contracts and startup projects and it allows me to keep code in sync between my multiple machines easily. As a technical site for technical people, you better believe that they’ve got a good API that’s easy to use and helpful! If you are here, there is a good chance you are a .NET or C# developer and that means you can use Octokit client library for .NET! Program in Ruby or Objective-C? They’ve got client libraries for those as well!

Let’s jump right in and start examining what we get to start with our new found friend. We see a lot of files and folders, most of which we don’t need as we can easily install straight from Visual Studio in the NuGet package manager and doing a search for Octokit. There are some examples and docs folders with some handy stuff though, but we are also lucky to get some actual docs below the fold in the readme.md file. Getting started is as simple as 3 lines of code:

   1: var github = new GitHubClient(new ProductHeaderValue("MyAmazingApp"));
   2:  
   3: var user = await github.User.Get("half-ogre");
   4:  
   5: Console.WriteLine(user.Followers + " folks love the half ogre!");
   6:  

Of course if you want to really dig in a little deeper, you need to authenticate! There are 2 different ways to do this: oAuth and basic. Depending on your use case, one will work better than the other. For myself, I just need basic as I’m not authenticating on behalf of a user, just myself. Getting started is pretty simple:

   1: var user = await client.User.Get("shiftkey");
   2: Console.WriteLine("{0} has {1} public repositories - go check out their profile at {2}",    
   3:     user.Name,
   4:     user.PublicRepos,
   5:     user.Url);

From this code you can see that we are using our github client “client”, utilizing the User class to get a specific user, in this case “shiftkey”. While the example only pulls out 3 data points, you get a whole lot more when you start inspecting what a user contains.

The docs don’t give you the greatest sense of what is where, so you have to play with it a bit to get a feel for things. Within the client you’ll find these useful classes where mostly everything you want is grouped under: User, Search, Repository, Organization, Notification, Miscellaneous, Issue, Gist, Activity and more. Within each you’ll find functions for navigating those particular APIs and getting back the data you seek.

With this data you can start to do interesting things like building profiles of people you know on GitHub, or pulling in stats on each user who has starred a certain repo. In my case, I can find out the full profile information of a user, find out who they are following, who is following them, how many public repos they have, how many starred repos they have, what organizations they belong to and so forth. I can start to build out a net of interconnected people and do all sorts of things. You can hook into the activity stream and actually pull in events that the API generates, although I haven’t tried playing with this yet.

If you’ve always wanted to start playing with APIs, but weren’t sure where to start, I’d say this is a decent starting place to learn. The official API documentation is fairly good and can give you an idea of what data will be returned, what can be queried for, etc. In my current example project I’m working on, I’ve simply got an asynchronous controller setup to poll for a set of users in my database, and then a view which returns those profiles filled! Once you know the username of an account, it’s easy to pull stuff in on the fly as needed, like if you wanted to dynamically display a GitHub user profile or badge on your site.

As I explore more APIs, I’ll keep sharing them here and what tools and libraries I’ve found to best navigate them.


Comments (0)

Skip to main content