Created
November 18, 2009 10:13
-
-
Save sb25/237707 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/python | |
| import httplib | |
| import base64 | |
| try: | |
| import json # Python 2.6 | |
| except ImportError: | |
| import simplejson as json # Python 2.4+ - http://pypi.python.org/pypi/simplejson/2.0.9 | |
| DOMAIN = "localhost:3000" | |
| USER, PASS = 'username', 'password' | |
| # Generic helper class for handling the web requests to the repository. | |
| class Connection(object): | |
| def __init__(self, domain, user, password): | |
| self.connection = httplib.HTTPConnection( domain ) | |
| auth = base64.encodestring( user + ':' + password ).strip() | |
| self.headers = { | |
| 'Content-Type': 'application/json', | |
| 'Authorization': 'Basic ' + auth | |
| } | |
| def request(self, method, url, data = ''): | |
| method = method.upper() | |
| if method not in ['GET', 'POST', 'PUT', 'DELETE']: | |
| raise "This method (%s) is not a valid HTTP method." % method | |
| # Send request, print response status and return response body | |
| self.connection.request( method, "/%s" % url, data, self.headers ) | |
| response = self.connection.getresponse() | |
| print method, url, "-", response.status, (response.reason or response.msg) | |
| return response.read() | |
| def close(self): | |
| self.connection.close() | |
| # Set connection to the server | |
| connection = Connection( DOMAIN, USER, PASS ) | |
| ## | |
| ## Scenarios: | |
| ## - GET, CREATE, UPDATE some allele(s) | |
| ## - CREATE a Targeting Vector | |
| ## - CREATE a ES Cell | |
| ## - DELETE created objects | |
| ## | |
| # GET allele list - first page | |
| response = connection.request( 'GET', 'alleles.json?page=1' ) | |
| alleles = json.loads( response ) | |
| first_allele = alleles[0] | |
| print first_allele | |
| # GET first allele - no need to keep track of the ids: | |
| # Take a look at the extended example to know how to search for an allele | |
| connection.request( 'GET', "alleles/%s.json" % first_allele['id'] ) | |
| # CREATE an allele | |
| data = { | |
| 'pipeline_id' : 1, | |
| 'mgi_accession_id' : "MGI:44556", | |
| 'project_design_id' : 10000, | |
| 'cassette' : "L1L2_gt2", | |
| 'backbone' : "L3L4_pZero_kan", | |
| 'assembly' : "NCBIM37", | |
| 'chromosome' : "11", | |
| 'strand' : "+", | |
| 'design_type' : "Knock Out", | |
| 'design_subtype' : "Frameshift", | |
| 'homology_arm_start' : 10, | |
| 'homology_arm_end' : 10000, | |
| 'cassette_start' : 50, | |
| 'cassette_end' : 500, | |
| 'loxp_start' : 1000, | |
| 'loxp_end' : 1500 | |
| } | |
| allele_json = json.dumps( {'molecular_structure': data} ) | |
| response = connection.request( 'POST', 'alleles.json', allele_json ) | |
| created_allele = json.loads( response ) | |
| print created_allele | |
| # UPDATE an allele (for example: the one created above) | |
| data = { 'subtype_description': 'SOME NEW INFORMATION' } | |
| allele_json = json.dumps( {'molecular_structure': data} ) | |
| response = connection.request( 'PUT', "alleles/%s.json" % created_allele['id'], allele_json ) | |
| updated_allele = json.loads( response ) | |
| print updated_allele | |
| # CREATE a targeting vector (related to the above allele) | |
| data = { | |
| 'molecular_structure_id': created_allele['id'], | |
| 'name' : 'PGDGR001', | |
| 'ikmc_project_id' : 1 | |
| } | |
| targ_vec_json = json.dumps( {'targeting_vector' : data} ) | |
| response = connection.request( 'POST', 'targeting_vectors.json', targ_vec_json ) | |
| created_targ_vec = json.loads( response ) | |
| print created_targ_vec | |
| # CREATE an ES Cell (related to the above targeting vector and allele) | |
| data = { | |
| 'molecular_structure_id': created_allele['id'], | |
| 'targeting_vector_id' : created_targ_vec['id'], | |
| 'name' : 'EPD001' | |
| } | |
| es_cell_json = json.dumps( {'es_cell': data} ) | |
| response = connection.request( 'POST', 'es_cells.json', es_cell_json ) | |
| created_es_cell = json.loads( response ) | |
| print created_es_cell | |
| # DELETE created objects | |
| connection.request( 'DELETE', "es_cells/%s" % created_es_cell['id'] ) | |
| connection.request( 'DELETE', "targeting_vectors/%s" % created_targ_vec['id'] ) | |
| connection.request( 'DELETE', "alleles/%s" % created_allele['id'] ) | |
| connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment