Python GitHub API Client - please fork!

Github.com has distinguished itself, in my mind, as one of the few actually useful social network sites. As its subtitle, "Social coding", suggests, its geared towards programmers. Its based around the popular version control software Git. You put your code online, other users can 'fork' it, modifying it as they please - it really lives up to its claim of social coding.

Anyways. They've got a new REST API which allows you to access data on your and other people's public repos. I'm in the process of converting this site to Django which may be the topic of another post, but in the meantime some of the features I am working on for my new site are:

  • Have my projects page sync up with my repos
  • When I post snippets, automatically create a gist

The API is pretty straightforward. On your account settings page you will see a token - that's what you use for authenticating:

I wanted some basic functionality so I wrote a github api client (which is, of course, available on github).

I would love suggestions on improving the api client. The gist stuff is still special-cased and I've had trouble getting it to create authenticated gists -- it's been returning 401's when I try to POST my login and token -- so only anonymous gists at the moment. The pattern I'm using is:

  • define a method that will call a specific API function and takes matching parameters
  • that function defines a certain type of 'processor'
  • a call is made to the API and the return data is handed off to the processor
  • the processor churns through the data, returning some very basic objects

Once again - I welcome suggestions! I look forward to improving this into something a bit more robust.

And here's a snippet:

# if you want to get the whole tree for a repo:
commit = github_client.get_commits(login, repo)[0]
def process_tree(tree, path=/):
    objs = github_client.get_tree(login, repo, tree)
    for obj in objs:
        setattr(obj, path, path)
        if obj.type == tree:
            setattr(obj, children, process_tree(obj.sha, path + obj.name + /))
    return objs
tree = process_tree(commit.tree)

Check out the github api client

Comments (0)

Commenting has been disabled for this entry