Mercurial > repos > stevecassidy > alveoimport
annotate item_list_util.py @ 18:5e1b7d922ea3 draft default tip
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
| author | stevecassidy |
|---|---|
| date | Mon, 15 Jan 2018 18:34:57 -0500 |
| parents | |
| children |
| rev | line source |
|---|---|
|
18
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
1 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
2 import json |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
3 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
4 API_URL = 'https://app.alveo.edu.au' |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
5 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
6 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
7 try: |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
8 from urllib.request import Request, build_opener, HTTPHandler, HTTPError |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
9 from urllib.parse import urlencode, unquote |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
10 except ImportError: |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
11 from urllib2 import Request, build_opener, HTTPHandler, HTTPError |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
12 from urllib import urlencode, unquote |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
13 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
14 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
15 def api_request(url, api_key): |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
16 """ Perform an API GET request to the given URL |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
17 """ |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
18 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
19 headers = {'X-API-KEY': api_key, 'Accept': 'application/json'} |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
20 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
21 req = Request(url, headers=headers) |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
22 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
23 try: |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
24 opener = build_opener(HTTPHandler()) |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
25 response = opener.open(req) |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
26 except HTTPError as err: |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
27 raise APIError(err.code, err.reason, "Error accessing API (url: %s, method: %s)\nData: %s" % (url, req.get_method() or "GET", data or 'None')) |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
28 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
29 content = response.read() |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
30 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
31 return json.loads(content.decode('utf-8')) |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
32 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
33 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
34 def ds_item_lists_options(api_key): |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
35 """Return options for item lists for this user""" |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
36 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
37 fname = api_key.get_file_name() |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
38 with open(fname) as fd: |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
39 key = fd.read().strip() |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
40 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
41 url = API_URL + "/item_lists" |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
42 itemlists = api_request(url, key) |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
43 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
44 result = [(x['name'], x['item_list_url'], False) for x in itemlists['own']] |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
45 result.extend([(x['name'], x['item_list_url'], False) for x in itemlists['shared']]) |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
46 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
47 return result |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
48 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
49 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
50 if __name__=='__main__': |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
51 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
52 class FakeHistObj: |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
53 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
54 def get_file_name(self): |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
55 return "../../test-data/api-key.dat" |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
56 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
57 fake = FakeHistObj() |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
58 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
59 il = ds_item_lists_options(fake) |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
60 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
61 import pprint |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
62 |
|
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
diff
changeset
|
63 pprint.pprint(il) |
