Skip to content

Commit 877d167

Browse files
committed
Call get/post/delete requests directly.
1 parent b38edfa commit 877d167

File tree

2 files changed

+118
-146
lines changed

2 files changed

+118
-146
lines changed

wes_client/util.py

Lines changed: 108 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import json
3-
import urlparse
4-
from bravado.client import SwaggerClient
3+
from bravado.requests_client import RequestsClient
54
import urllib
65
import logging
76
import schema_salad.ref_resolver
@@ -63,14 +62,6 @@ def build_wes_request(workflow_file, json_path, attachments=None):
6362
return parts
6463

6564

66-
def wes_client(http_client, auth, proto, host):
67-
split = urlparse.urlsplit("%s://%s/" % (proto, host))
68-
http_client.set_api_key(split.hostname, auth, param_name="Authorization", param_in="header")
69-
client = SwaggerClient.from_url("%s://%s/ga4gh/wes/v1/swagger.json" % (proto, host),
70-
http_client=http_client, config={"use_models": False})
71-
return client.WorkflowExecutionService
72-
73-
7465
def modify_jsonyaml_paths(jsonyaml_file):
7566
"""
7667
Changes relative paths in a json/yaml file to be relative
@@ -99,125 +90,116 @@ def fixpaths(d):
9990
visit(input_dict, fixpaths)
10091

10192

102-
def run_wf(workflow_file, jsonyaml, attachments, http_client, auth, proto, host):
103-
"""
104-
Composes and sends a post request that signals the wes server to run a workflow.
105-
106-
:param str workflow_file: A local/http/https path to a cwl/wdl/python workflow file.
107-
:param str jsonyaml: A local path to a json or yaml file.
108-
:param list attachments: A list of local paths to files that will be uploaded to the server.
109-
:param object http_client: bravado.requests_client.RequestsClient
110-
:param str auth: String to send in the auth header.
111-
:param proto: Schema where the server resides (http, https)
112-
:param host: Port where the post request will be sent and the wes server listens at (default 8080)
113-
114-
:return: The body of the post result as a dictionary.
115-
"""
116-
parts = build_wes_request(workflow_file, jsonyaml, attachments)
117-
postresult = http_client.session.post("%s://%s/ga4gh/wes/v1/runs" % (proto, host),
118-
files=parts,
119-
headers={"Authorization": auth})
120-
if postresult.status_code != 200:
121-
logging.error("%s", json.loads(postresult.text))
122-
exit(1)
123-
return json.loads(postresult.text)
124-
125-
126-
def cancel_wf(run_id, http_client, auth, proto, host):
127-
"""
128-
Cancel a running workflow.
129-
130-
:param run_id:
131-
:param object http_client: bravado.requests_client.RequestsClient
132-
:param str auth: String to send in the auth header.
133-
:param proto: Schema where the server resides (http, https)
134-
:param host: Port where the post request will be sent and the wes server listens at (default 8080)
135-
:return: The body of the delete result as a dictionary.
136-
"""
137-
postresult = http_client.session.delete("%s://%s/ga4gh/wes/v1/runs/%s" % (proto, host, run_id),
138-
headers={"Authorization": auth})
139-
if postresult.status_code != 200:
140-
logging.error("%s", json.loads(postresult.text))
141-
exit(1)
142-
return json.loads(postresult.text)
143-
144-
145-
def get_status(run_id, http_client, auth, proto, host):
146-
"""
147-
Get quick status info about a running workflow.
148-
149-
:param run_id:
150-
:param object http_client: bravado.requests_client.RequestsClient
151-
:param str auth: String to send in the auth header.
152-
:param proto: Schema where the server resides (http, https)
153-
:param host: Port where the post request will be sent and the wes server listens at (default 8080)
154-
:return: The body of the get result as a dictionary.
155-
"""
156-
postresult = http_client.session.get("%s://%s/ga4gh/wes/v1/runs/%s/status" % (proto, host, run_id),
157-
headers={"Authorization": auth})
158-
if postresult.status_code != 200:
159-
logging.error("%s", json.loads(postresult.text))
160-
exit(1)
161-
return json.loads(postresult.text)
162-
163-
164-
def get_wf_details(run_id, http_client, auth, proto, host):
165-
"""
166-
Get detailed info about a running workflow.
167-
168-
:param run_id:
169-
:param object http_client: bravado.requests_client.RequestsClient
170-
:param str auth: String to send in the auth header.
171-
:param proto: Schema where the server resides (http, https)
172-
:param host: Port where the post request will be sent and the wes server listens at (default 8080)
173-
:return: The body of the get result as a dictionary.
174-
"""
175-
postresult = http_client.session.get("%s://%s/ga4gh/wes/v1/runs/%s" % (proto, host, run_id),
176-
headers={"Authorization": auth})
177-
if postresult.status_code != 200:
178-
logging.error("%s", json.loads(postresult.text))
179-
exit(1)
180-
return json.loads(postresult.text)
181-
182-
183-
def get_wf_list(http_client, auth, proto, host):
184-
"""
185-
List the workflows, this endpoint will list the workflows
186-
in order of oldest to newest. There is no guarantee of
187-
live updates as the user traverses the pages, the behavior
188-
should be decided (and documented) by each implementation.
189-
190-
:param object http_client: bravado.requests_client.RequestsClient
191-
:param str auth: String to send in the auth header.
192-
:param proto: Schema where the server resides (http, https)
193-
:param host: Port where the post request will be sent and the wes server listens at (default 8080)
194-
:return: The body of the get result as a dictionary.
195-
"""
196-
postresult = http_client.session.get("%s://%s/ga4gh/wes/v1/runs" % (proto, host),
197-
headers={"Authorization": auth})
93+
def wes_reponse(postresult):
19894
if postresult.status_code != 200:
19995
logging.error("%s", json.loads(postresult.text))
20096
exit(1)
20197
return json.loads(postresult.text)
20298

20399

204-
def get_service_info(http_client, auth, proto, host):
205-
"""
206-
Get information about Workflow Execution Service. May
207-
include information related (but not limited to) the
208-
workflow descriptor formats, versions supported, the
209-
WES API versions supported, and information about general
210-
the service availability.
211-
212-
:param object http_client: bravado.requests_client.RequestsClient
213-
:param str auth: String to send in the auth header.
214-
:param proto: Schema where the server resides (http, https)
215-
:param host: Port where the post request will be sent and the wes server listens at (default 8080)
216-
:return: The body of the get result as a dictionary.
217-
"""
218-
postresult = http_client.session.get("%s://%s/ga4gh/wes/v1/service-info" % (proto, host),
219-
headers={"Authorization": auth})
220-
if postresult.status_code != 200:
221-
logging.error("%s", json.loads(postresult.text))
222-
exit(1)
223-
return json.loads(postresult.text)
100+
class WESClient(object):
101+
def __init__(self, service):
102+
self.auth = service['auth']
103+
self.proto = service['proto']
104+
self.host = service['host']
105+
self.http_client = RequestsClient()
106+
107+
def get_service_info(self):
108+
"""
109+
Get information about Workflow Execution Service. May
110+
include information related (but not limited to) the
111+
workflow descriptor formats, versions supported, the
112+
WES API versions supported, and information about general
113+
the service availability.
114+
115+
:param object http_client: bravado.requests_client.RequestsClient
116+
:param str auth: String to send in the auth header.
117+
:param proto: Schema where the server resides (http, https)
118+
:param host: Port where the post request will be sent and the wes server listens at (default 8080)
119+
:return: The body of the get result as a dictionary.
120+
"""
121+
postresult = self.http_client.session.get("%s://%s/ga4gh/wes/v1/service-info" % (self.proto, self.host),
122+
headers={"Authorization": self.auth})
123+
return wes_reponse(postresult)
124+
125+
def list_runs(self):
126+
"""
127+
List the workflows, this endpoint will list the workflows
128+
in order of oldest to newest. There is no guarantee of
129+
live updates as the user traverses the pages, the behavior
130+
should be decided (and documented) by each implementation.
131+
132+
:param object http_client: bravado.requests_client.RequestsClient
133+
:param str auth: String to send in the auth header.
134+
:param proto: Schema where the server resides (http, https)
135+
:param host: Port where the post request will be sent and the wes server listens at (default 8080)
136+
:return: The body of the get result as a dictionary.
137+
"""
138+
postresult = self.http_client.session.get("%s://%s/ga4gh/wes/v1/runs" % (self.proto, self.host),
139+
headers={"Authorization": self.auth})
140+
return wes_reponse(postresult)
141+
142+
def run(self, wf, jsonyaml, attachments):
143+
"""
144+
Composes and sends a post request that signals the wes server to run a workflow.
145+
146+
:param str workflow_file: A local/http/https path to a cwl/wdl/python workflow file.
147+
:param str jsonyaml: A local path to a json or yaml file.
148+
:param list attachments: A list of local paths to files that will be uploaded to the server.
149+
:param object http_client: bravado.requests_client.RequestsClient
150+
:param str auth: String to send in the auth header.
151+
:param proto: Schema where the server resides (http, https)
152+
:param host: Port where the post request will be sent and the wes server listens at (default 8080)
153+
154+
:return: The body of the post result as a dictionary.
155+
"""
156+
parts = build_wes_request(wf, jsonyaml, attachments)
157+
postresult = self.http_client.session.post("%s://%s/ga4gh/wes/v1/runs" % (self.proto, self.host),
158+
files=parts,
159+
headers={"Authorization": self.auth})
160+
return wes_reponse(postresult)
161+
162+
def cancel(self, run_id):
163+
"""
164+
Cancel a running workflow.
165+
166+
:param run_id:
167+
:param object http_client: bravado.requests_client.RequestsClient
168+
:param str auth: String to send in the auth header.
169+
:param proto: Schema where the server resides (http, https)
170+
:param host: Port where the post request will be sent and the wes server listens at (default 8080)
171+
:return: The body of the delete result as a dictionary.
172+
"""
173+
postresult = self.http_client.session.delete("%s://%s/ga4gh/wes/v1/runs/%s" % (self.proto, self.host, run_id),
174+
headers={"Authorization": self.auth})
175+
return wes_reponse(postresult)
176+
177+
def get_run_log(self, run_id):
178+
"""
179+
Get detailed info about a running workflow.
180+
181+
:param run_id:
182+
:param object http_client: bravado.requests_client.RequestsClient
183+
:param str auth: String to send in the auth header.
184+
:param proto: Schema where the server resides (http, https)
185+
:param host: Port where the post request will be sent and the wes server listens at (default 8080)
186+
:return: The body of the get result as a dictionary.
187+
"""
188+
postresult = self.http_client.session.get("%s://%s/ga4gh/wes/v1/runs/%s" % (self.proto, self.host, run_id),
189+
headers={"Authorization": self.auth})
190+
return wes_reponse(postresult)
191+
192+
def get_run_status(self, run_id):
193+
"""
194+
Get quick status info about a running workflow.
195+
196+
:param run_id:
197+
:param object http_client: bravado.requests_client.RequestsClient
198+
:param str auth: String to send in the auth header.
199+
:param proto: Schema where the server resides (http, https)
200+
:param host: Port where the post request will be sent and the wes server listens at (default 8080)
201+
:return: The body of the get result as a dictionary.
202+
"""
203+
postresult = self.http_client.session.get("%s://%s/ga4gh/wes/v1/runs/%s/status" % (self.proto, self.host, run_id),
204+
headers={"Authorization": self.auth})
205+
return wes_reponse(postresult)

wes_client/wes_client_main.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
import logging
99
import requests
1010
from requests.exceptions import InvalidSchema, MissingSchema
11-
from wes_client.util import (run_wf,
12-
wes_client,
13-
modify_jsonyaml_paths)
14-
from bravado.requests_client import RequestsClient
11+
from wes_client.util import modify_jsonyaml_paths, WESClient
1512

1613

1714
def main(argv=sys.argv[1:]):
@@ -52,26 +49,25 @@ def main(argv=sys.argv[1:]):
5249
print(u"%s %s" % (sys.argv[0], pkg[0].version))
5350
exit(0)
5451

55-
http_client = RequestsClient()
56-
client = wes_client(http_client, args.auth, args.proto, args.host)
52+
client = WESClient({'auth': args.auth, 'proto': args.proto, 'host': args.host})
5753

5854
if args.list:
59-
response = client.ListRuns(page_token=args.page, page_size=args.page_size)
55+
response = client.list_runs() # how to include: page_token=args.page, page_size=args.page_size ?
6056
json.dump(response.result(), sys.stdout, indent=4)
6157
return 0
6258

6359
if args.log:
64-
response = client.GetRunLog(run_id=args.log)
60+
response = client.get_run_log(run_id=args.log)
6561
sys.stdout.write(response.result()["workflow_log"]["stderr"])
6662
return 0
6763

6864
if args.get:
69-
response = client.GetRunLog(run_id=args.get)
65+
response = client.get_run_log(run_id=args.get)
7066
json.dump(response.result(), sys.stdout, indent=4)
7167
return 0
7268

7369
if args.info:
74-
response = client.GetServiceInfo()
70+
response = client.get_service_info()
7571
json.dump(response.result(), sys.stdout, indent=4)
7672
return 0
7773

@@ -91,28 +87,22 @@ def main(argv=sys.argv[1:]):
9187
logging.basicConfig(level=logging.INFO)
9288

9389
args.attachments = args.attachments if not args.attachments else args.attachments.split(',')
94-
r = run_wf(args.workflow_url,
95-
args.job_order,
96-
args.attachments,
97-
http_client,
98-
args.auth,
99-
args.proto,
100-
args.host)
90+
r = client.run(args.workflow_url, args.job_order, args.attachments)
10191

10292
if args.wait:
10393
logging.info("Workflow run id is %s", r["run_id"])
10494
else:
10595
sys.stdout.write(r["run_id"] + "\n")
10696
exit(0)
10797

108-
r = client.GetRunStatus(run_id=r["run_id"]).result()
98+
r = client.get_run_status(run_id=r["run_id"]).result()
10999
while r["state"] in ("QUEUED", "INITIALIZING", "RUNNING"):
110100
time.sleep(8)
111-
r = client.GetRunStatus(run_id=r["run_id"]).result()
101+
r = client.get_run_status(run_id=r["run_id"]).result()
112102

113103
logging.info("State is %s", r["state"])
114104

115-
s = client.GetRunLog(run_id=r["run_id"]).result()
105+
s = client.get_run_log(run_id=r["run_id"]).result()
116106

117107
try:
118108
# TODO: Only works with Arvados atm

0 commit comments

Comments
 (0)