-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpollination.py
340 lines (274 loc) · 9.03 KB
/
pollination.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"""
A module demonstrating how to wrap the Pollination (api.pollination.cloud) REST
API for use in a custom application.
"""
import os
import httpx
from pprint import pprint as print
from dataclasses import dataclass, asdict
from queenbee.io.inputs.job import JobPathArgument
from typing import List
class Payload:
class Base:
def to_dict(self):
return asdict(self)
@dataclass
class Create(Base):
"""
Payload to create a Pollination project.
Schema: https://api.pollination.cloud/redoc#operation/create_project
"""
name: str
description: str
public: bool
@dataclass
class RecipeFilter(Base):
"""
Payload for adding a recipe to a project.
# operation/create_project_recipe_filter
Schema: https://api.pollination.cloud/redoc
"""
owner: str
name: str
tag: str
@dataclass
class Artifact(Base):
"""
Payload for adding a file to a project.
Schema: https://api.pollination.cloud/redoc#operation/create_artifact
"""
key: str
def upload(self, url: str, fields: dict) -> httpx.Response:
"""
Upload the local file indicated by `self.key` to the bucket pointed
to by `url` using the authorization in `fields`.
Assumes a file named `self.key` exists in the directory where
`python` is executed.
We use a new httpx client here because the files are actually
sent to Pollination's bulk storage server which is hosted on a
different domain.
"""
with open(self.key, 'rb') as fp:
files = {'file': (self.key, fp)}
res = httpx.post(
url=url, data=fields, files=files)
return res
@dataclass
class Job(Base):
"""
Minimal payload for creating a job.
Schema: https://api.pollination.cloud/redoc#operation/create_job
"""
source: str
# A job can be started with many argument lists to run the same simulation
# with different parameters.
# Hence, the arguments must be a list of lists
arguments: List[List[JobPathArgument]]
def to_dict(self):
d = asdict(self)
args = []
for group in self.arguments:
g = []
for arg in group:
g.append(arg.to_dict())
args.append(g)
d['arguments'] = args
return d
class PollinationClient(httpx.Client):
"""
An HTTP client specific to the Pollination REST API.
"""
class Routes:
"""
Simple container class for Pollination API routes.
"""
var_name = '/{name}'
var_owner = '/{owner}'
var_job_id = '/{job_id}'
var_run_id = '/{run_id}'
var_output_name = '/{output_name}'
artifacts = '/artifacts'
downloads = '/downloads'
filters = '/filters'
jobs = '/jobs'
outputs = '/outputs'
recipes = '/recipes'
runs = '/runs'
user = '/user'
accounts = '/accounts'
accounts_name = accounts + var_name
projects = '/projects'
project_create = projects + var_owner
project_add_recipe = (
projects + var_owner + var_name + recipes + filters
)
project_add_artifact = projects + var_owner + var_name + artifacts
project_jobs = projects + var_owner + var_name + jobs
project_job_by_id = project_jobs + var_job_id
project_job_artifacts = project_job_by_id + artifacts
project_job_artifacts_download = project_job_artifacts + downloads
project_runs = projects + var_owner + var_name + runs
project_run_output = (
project_runs + var_run_id + outputs + var_output_name
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.base_url = 'https://api.pollination.cloud'
self.headers['FECF49DE.E5AF405587754456916B22F9'] = os.environ['POLLINATION_API_KEY']
self.organization = os.environ['POLLINATION_ORG']
def _org_endpoint(self):
return (
self
.Routes
.accounts_name
.format_map(dict(name=self.organization))
)
def get_organization(self) -> dict:
res = self.get(self._org_endpoint())
return res
def create_project(self, body: Payload.Create) -> dict:
endpoint = self.Routes.project_create.format_map(
dict(owner=self.organization))
res = self.post(endpoint, json=body.to_dict())
return res
def add_recipe_to_project(self, project_name: str, body: Payload.RecipeFilter):
"""
Add a recipe to a project via a filter that will select the recipe
from the set of recipes which are visible to the current account.
"""
endpoint = (
self
.Routes
.project_add_recipe
.format_map(
dict(
owner=self.organization,
name=project_name
)
)
)
res = self.post(endpoint, json=body.to_dict())
return res
def add_file_to_project(self, project_name: str, body: Payload.Artifact) -> dict:
"""
Add a file to a project for use in simulations.
Because the files are stored in a cloud storage provider, we must first
ask the Pollination API to generate a storage link for us, then put
the file's content to that location.
"""
endpoint = (
self
.Routes
.project_add_artifact
.format_map(
dict(
owner=self.organization,
name=project_name
)
)
)
res = self.post(endpoint, json=body.to_dict())
res_body = res.json()
res = body.upload(res_body['url'], res_body['fields'])
return res if res.status_code == 204 else res.raise_for_status()
def create_job(self, project_name: str, body: Payload.Job):
endpoint = (
self
.Routes
.project_jobs
.format_map(
dict(
owner=self.organization,
name=project_name
)
)
)
res = self.post(endpoint, json=body.to_dict())
return res
def get_job(self, project_name: str, job_id: str):
endpoint = (
self
.Routes
.project_job_by_id.format_map(
dict(
owner=self.organization,
name=project_name,
job_id=job_id
)
)
)
res = self.get(endpoint)
return res
def list_jobs(self, project_name: str):
endpoint = self.Routes.project_jobs.format_map(
dict(owner=self.organization, name=project_name))
res = self.get(endpoint)
return res
def list_job_artifacts(self, project_name: str, job_id: str):
endpoint = (
self
.Routes
.project_job_artifacts
.format_map(
dict(
owner=self.organization,
name=project_name,
job_id=job_id
)
)
)
res = self.get(endpoint)
return res
def get_job_artifact_link(self, project_name: str, job_id: str, artifact_path: str):
"""
Schema: https://api.pollination.cloud/redoc#operation/download_job_artifact
"""
endpoint = (
self
.Routes
.project_job_artifacts_download
.format_map(
dict(
owner=self.organization,
name=project_name,
job_id=job_id
)
)
)
endpoint += f'?path={artifact_path}'
res = self.get(endpoint)
return res
def get_runs(self, project_name: str, job_id: str):
"""
Schema: https://api.pollination.cloud/redoc#operation/list_runs
"""
endpoint = (
self
.Routes
.project_runs
.format_map(
dict(
owner=self.organization,
name=project_name
)
)
)
endpoint += f'?job_id={job_id}'
res = self.get(endpoint)
return res
def get_run_output(self, project_name: str, run_id: str, output_name: str):
endpoint = (
self
.Routes
.project_run_output
.format_map(
dict(
owner=self.organization,
name=project_name,
run_id=run_id,
output_name=output_name
)
)
)
res = self.get(endpoint)
return res