Calling the GenomeSpace API from Python

For today's technology tidbit, we're going to explore how to call the GenomeSpace REST-ful web interfaces from the Python programming language.  (Full disclosure: this was my first attempt to use Python so I am sure there are stylistic issues and more elegant ways to do this). 

Below, we'll review the steps to connect to the GenomeSpace Data Manager and list the user's home directory in an interactive Python session.

1. Import the libraries we'll be using

# set up imports
import urllib2
import json

2. Set up a password manager basic authentication

# set some properties
gsUsername = 'ted'
gsPassword = '*****'

3. Retrieve the GenomeSpace server URLs from the published web page

# get all the GenomeSpace URLs from the main web page
opener = urllib2.build_opener()
allUrls = opener.open('http://www.genomespace.org/sites/genomespacefiles/config/serverurl.properties')
urlMap = {}
for line in allUrls:
     tokens = line.split("=")
     if (len(tokens) == 2):
         urlMap[tokens[0]] = tokens[1][:-1] #the trailing \n is not handled properly

idUrl = urlMap['prod.identityServerUrl']
dmUrl = urlMap['prod.dmServer'] + '/v1.0/'

4. Use basic authentication to get a gs-token to use for the rest of the calls

auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm=" GenomeSpace ", uri=idUrl, user=gsUsername, passwd=gsPassword)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
urllib2.urlopen(idUrl)

5. Get the GSDirectory JSON object

req = urllib2.Request(dmUrl + 'file/Home/' + gsUsername)
req.add_header('Cookie','gs-token='+token)
mydir = sslopener.open(req)
homedirJson = mydir.read()

# turn it into a real object from a JSON string
homeDirObj = json.loads(homedirJson)

6. Get the URL for the first file and download its contents

firstUrl = homeDirObj['contents'][0]['url']

req2 = urllib2.Request(firstUrl)
req2.add_header('Cookie','gs-token='+token)
firstFileContents = sslopener.open(req2)
print firstFileContents

That's it!  For more details on how to use the GenomeSpace REST-ful APIs, the structure of the JSON objects, etc., please refer to the documents on the Technical Documentation page.