|
1 | 1 | import os
|
2 | 2 | import json
|
3 |
| -import urlparse |
4 |
| -from bravado.client import SwaggerClient |
| 3 | +from bravado.requests_client import RequestsClient |
5 | 4 | import urllib
|
6 | 5 | import logging
|
7 | 6 | import schema_salad.ref_resolver
|
@@ -63,14 +62,6 @@ def build_wes_request(workflow_file, json_path, attachments=None):
|
63 | 62 | return parts
|
64 | 63 |
|
65 | 64 |
|
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 |
| - |
74 | 65 | def modify_jsonyaml_paths(jsonyaml_file):
|
75 | 66 | """
|
76 | 67 | Changes relative paths in a json/yaml file to be relative
|
@@ -99,125 +90,116 @@ def fixpaths(d):
|
99 | 90 | visit(input_dict, fixpaths)
|
100 | 91 |
|
101 | 92 |
|
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): |
198 | 94 | if postresult.status_code != 200:
|
199 | 95 | logging.error("%s", json.loads(postresult.text))
|
200 | 96 | exit(1)
|
201 | 97 | return json.loads(postresult.text)
|
202 | 98 |
|
203 | 99 |
|
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) |
0 commit comments