-
Notifications
You must be signed in to change notification settings - Fork 273
/
processes.py
166 lines (123 loc) · 4.95 KB
/
processes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# =============================================================================
# Copyright (c) 2021 Tom Kralidis
#
# Author: Tom Kralidis <tomkralidis@gmail.com>
#
# Contact email: tomkralidis@gmail.com
# =============================================================================
import logging
import time
from typing import Tuple
import requests
from owslib.ogcapi import API
from owslib.util import Authentication
LOGGER = logging.getLogger(__name__)
class Processes(API):
"""Abstraction for OGC API - Processes
* https://ogcapi.ogc.org/processes/overview.html
* https://docs.opengeospatial.org/DRAFTS/18-062.html
* https://app.swaggerhub.com/apis/geoprocessing/WPS-all-in-one/1.0-draft.6-SNAPSHOT
"""
def __init__(self, url: str, json_: str = None, timeout: int = 30,
headers: dict = None, auth: Authentication = None):
__doc__ = API.__doc__ # noqa
super().__init__(url, json_, timeout, headers, auth)
def processes(self) -> list:
"""
implements: GET /processes
Lists the processes this API offers.
@returns: `list` of available processes.
"""
processes_ = []
path = 'processes'
data = self._request(path)
if 'processes' in data:
processes_.extend(data["processes"])
return processes_
def process_description(self, process_id: str) -> dict:
"""
implements: GET /processes/{process-id}
Returns a detailed description of a process.
@returns: `dict` of a process description.
"""
path = f'processes/{process_id}'
return self._request(path)
def job_list(self, process_id: str) -> dict:
"""
implements: GET /processes/{process-id}/jobs
Returns the running and finished jobs for a process (optional).
@returns: `dict` of ....
"""
path = f'processes/{process_id}/jobs'
return self._request(path)
def execute(self, process_id: str, json: dict) -> dict:
"""
implements: POST /processes/{process-id}/jobs
Executes a process, i.e. creates a new job. Inputs and outputs will have
to be specified in a JSON document that needs to be send in the POST body.
@returns: `dict` of the status location (async) or outputs (sync)
"""
result = {}
path = f'processes/{process_id}/jobs'
resp = self._request_post(path, json)
data = resp.json()
if 'outputs' in data:
result['outputs'] = data['outputs']
else:
result['location'] = resp.headers.get("Location", data.get("location"))
return result
def _request_post(self, path: str, json: dict) -> requests.Response:
# TODO: needs to be implemented in base class
url = self._build_url(path)
resp = requests.post(url, json=json)
if resp.status_code not in [requests.codes.ok, 201]:
raise RuntimeError(resp.text)
return resp
def status(self, process_id: str, job_id: str) -> dict:
"""
implements: GET /processes/{process-id}/jobs/{job-id}
Returns the status of a job of a process.
@returns: `dict` of ...
"""
path = f'processes/{process_id}/jobs/{job_id}'
return self._request(path)
def cancel(self, process_id: str, job_id: str) -> dict:
"""
implements: DELETE /processes/{process-id}/jobs/{job-id}
Cancel a job execution.
@returns: `dict` of ...
"""
path = f'processes/{process_id}/jobs/{job_id}'
return self._request(path)
def result(self, process_id: str, job_id: str) -> dict:
"""
implements: GET /processes/{process-id}/jobs/{job-id}/results
Returns the result of a job of a process.
@returns: `dict` of ...
"""
path = f'processes/{process_id}/jobs/{job_id}/results'
return self._request(path)
def monitor_execution(self, process_id: str = None, job_id: str = None, location: str = None,
timeout: int = 3600, delta: int = 10) -> Tuple[dict, bool]:
"""
Job polling of status URL until completion or timeout.
If `location` is provided, it is used instead.
@returns: results of the monitoring upon completion as `tuple` of (data, success?)
"""
time.sleep(1) # small delay to ensure process execution had a change to start before monitoring
left = timeout
once = True
data = None
while left >= 0 or once:
if location:
data = self._request(url=location)
else:
data = self.status(process_id, job_id)
if data['status'] in ['running', 'succeeded']:
break
if data['status'] == 'failed':
return data, False
time.sleep(delta)
once = False
left -= delta
return data, data['status'] == 'succeeded'