1 """Common functions for accessing the HTS Workflow REST API
3 from ConfigParser import SafeConfigParser
6 # try to deal with python <2.6
10 import simplejson as json
13 from optparse import OptionGroup
18 LOGGER = logging.getLogger(__name__)
20 def add_auth_options(parser):
21 """Add options OptParser configure authentication options
23 # Load defaults from the config files
24 config = SafeConfigParser()
25 config.read([os.path.expanduser('~/.htsworkflow.ini'),
26 '/etc/htsworkflow.ini'
29 sequence_archive = None
33 SECTION = 'sequence_archive'
34 if config.has_section(SECTION):
35 sequence_archive = config.get(SECTION, 'sequence_archive',sequence_archive)
36 sequence_archive = os.path.expanduser(sequence_archive)
37 apiid = config.get(SECTION, 'apiid', apiid)
38 apikey = config.get(SECTION, 'apikey', apikey)
39 apihost = config.get(SECTION, 'host', apihost)
41 # configuration options
42 group = OptionGroup(parser, "htsw api authentication")
43 group.add_option('--apiid', default=apiid, help="Specify API ID")
44 group.add_option('--apikey', default=apikey, help="Specify API KEY")
45 group.add_option('--host', default=apihost,
46 help="specify HTSWorkflow host",)
47 group.add_option('--sequence', default=sequence_archive,
48 help="sequence repository")
49 parser.add_option_group(group)
51 def make_auth_from_opts(opts, parser):
52 """Create htsw auth info dictionary from optparse info
54 if opts.host is None or opts.apiid is None or opts.apikey is None:
55 parser.error("Please specify host url, apiid, apikey")
57 return {'apiid': opts.apiid, 'apikey': opts.apikey }
60 def library_url(root_url, library_id):
62 Return the url for retrieving information about a specific library.
65 library_id (str): the library id of interest
66 root_url (str): the root portion of the url, e.g. http://localhost
69 str. The url to use for this REST api.
71 >>> print library_url('http://localhost', '12345')
72 http://localhost/samples/library/12345/json
75 url_fragment = '/samples/library/%s/json' % (library_id,)
76 url = urlparse.urljoin(root_url, url_fragment)
81 def flowcell_url(root_url, flowcell_id):
83 Return the url for retrieving information about a specific flowcell.
86 root_url (str): the root portion of the url, e.g. http://localhost
87 flowcell_id (str): the flowcell id of interest
90 str. The url to use for this REST api.
92 >>> print flowcell_url('http://localhost', '1234AAXX')
93 http://localhost/experiments/config/1234AAXX/json
95 url_fragment = '/experiments/config/%s/json' % (flowcell_id,)
96 url = urlparse.urljoin(root_url, url_fragment)
101 def lanes_for_user_url(root_url, username):
103 Return the url for returning all the lanes associated with a username
106 username (str): a username in your target filesystem
107 root_url (str): the root portion of the url, e.g. http://localhost
110 str. The url to use for this REST api.
112 >>> print lanes_for_user_url('http://localhost', 'diane')
113 http://localhost/lanes_for/diane/json
116 url_fragment = '/lanes_for/%s/json' % (username,)
117 url = urlparse.urljoin(root_url, url_fragment)
121 def retrieve_info(url, apidata):
123 Return a dictionary from the HTSworkflow API
126 apipayload = urllib.urlencode(apidata)
127 web = urllib2.urlopen(url, apipayload)
128 except urllib2.URLError, e:
129 if hasattr(e, 'code') and e.code == 404:
130 LOGGER.info("%s was not found" % (url,))
133 errmsg = 'URLError: %s' % (str(e))
134 raise IOError(errmsg)
136 contents = web.read()
139 return json.loads(contents)
141 class HtswApi(object):
142 def __init__(self, root_url, authdata):
143 self.root_url = root_url
144 self.authdata = authdata
146 def get_flowcell(self, flowcellId):
147 url = flowcell_url(self.root_url, flowcellId)
148 return retrieve_info(url, self.authdata)
150 def get_library(self, libraryId):
151 url = library_url(self.root_url, libraryId)
152 return retrieve_info(url, self.authdata)
154 def get_lanes_for_user(self, user):
155 url = lanes_for_user(self.root_url, user)
156 return retrieve_info(url, self.authdata)
158 def get_url(self, url):
159 return retrieve_info(url, self.authdata)