forked from anvilco/python-anvil
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
447 lines (383 loc) · 13.4 KB
/
api.py
File metadata and controls
447 lines (383 loc) · 13.4 KB
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import logging
from gql import gql
from graphql import DocumentNode
from typing import Any, AnyStr, Callable, Dict, List, Optional, Tuple, Union
from .api_resources.mutations import (
BaseQuery,
CreateEtchPacket,
ForgeSubmit,
GenerateEtchSigningURL,
)
from .api_resources.payload import (
CreateEtchPacketPayload,
FillPDFPayload,
ForgeSubmitPayload,
GeneratePDFPayload,
)
from .api_resources.requests import FullyQualifiedRequest, PlainRequest, RestRequest
from .http import GQLClient, HTTPClient
logger = logging.getLogger(__name__)
def _get_return(res: Dict, get_data: Callable[[Dict], Union[Dict, List]]):
"""Process response and get data from path if provided."""
_res = res
if "response" in res and "headers" in res:
_res = res["response"]
return {"response": get_data(_res), "headers": res["headers"]}
return get_data(_res)
class Anvil:
"""Main Anvil API class.
Handles all GraphQL and REST queries.
Usage:
>> anvil = Anvil(api_key="my_key")
>> payload = {}
>> pdf_data = anvil.fill_pdf("the_template_id", payload)
"""
# Version number to use for latest versions (usually drafts)
VERSION_LATEST = -1
# Version number to use for the latest published version.
# This is the default when a version is not provided.
VERSION_LATEST_PUBLISHED = -2
def __init__(
self,
api_key: Optional[str] = None,
environment="dev",
endpoint_url=None,
):
if not api_key:
raise ValueError('`api_key` must be a valid string')
self.client = HTTPClient(api_key=api_key, environment=environment)
self.gql_client = GQLClient.get_client(
api_key=api_key,
environment=environment,
endpoint_url=endpoint_url,
)
def query(
self,
query: Union[str, DocumentNode],
variables: Optional[Dict[str, Any]] = None,
**kwargs,
):
"""Execute a GraphQL query.
:param query:
:type query: Union[str, DocumentNode]
:param variables:
:type variables: Optional[Dict[str, Any]]
:param kwargs:
:return:
"""
# Remove `debug` for now.
kwargs.pop("debug", None)
if isinstance(query, str):
query = gql(query)
return self.gql_client.execute(query, variable_values=variables, **kwargs)
def mutate(
self, query: Union[str, BaseQuery], variables: Dict[str, Any], **kwargs
) -> Dict[str, Any]:
"""
Execute a GraphQL mutation.
NOTE: Any files attached provided in `variables` will be sent via the
multipart spec:
https://github.com/jaydenseric/graphql-multipart-request-spec
:param query:
:type query: Union[str, BaseQuery]
:param variables:
:type variables: Dict[str, Any]
:param kwargs:
:return:
"""
# Remove `debug` for now.
kwargs.pop("debug", None)
if isinstance(query, str):
use_query = gql(query)
else:
mutation = query.get_mutation()
use_query = gql(mutation)
return self.gql_client.execute(use_query, variable_values=variables, **kwargs)
def request_rest(self, options: Optional[dict] = None):
api = RestRequest(self.client, options=options)
return api
def request_fully_qualified(self, options: Optional[dict] = None):
api = FullyQualifiedRequest(self.client, options=options)
return api
def fill_pdf(
self, template_id: str, payload: Union[dict, AnyStr, FillPDFPayload], **kwargs
):
"""Fill an existing template with provided payload data.
Use the casts graphql query to get a list of available templates you
can use for this request.
:param template_id: eid of an existing template/cast
:type template_id: str
:param payload: payload in the form of a dict or JSON data
:type payload: dict|str
:param kwargs.version_number: specific template version number to use. If
not provided, the latest _published_ version will be used.
:type kwargs.version_number: int
"""
try:
if isinstance(payload, dict):
data = FillPDFPayload(**payload)
elif isinstance(payload, str):
data = FillPDFPayload.model_validate_json(payload)
elif isinstance(payload, FillPDFPayload):
data = payload
else:
raise ValueError("`payload` must be a valid JSON string or a dict")
except KeyError as e:
logger.exception(e)
raise ValueError(
"`payload` validation failed. Please make sure all required "
"fields are set. "
) from e
version_number = kwargs.pop("version_number", None)
if version_number:
kwargs["params"] = dict(versionNumber=version_number)
api = RestRequest(client=self.client)
return api.post(
f"fill/{template_id}.pdf",
data.model_dump(by_alias=True, exclude_none=True) if data else {},
**kwargs,
)
def generate_pdf(self, payload: Union[AnyStr, Dict, GeneratePDFPayload], **kwargs):
if not payload:
raise ValueError("`payload` must be a valid JSON string or a dict")
if isinstance(payload, dict):
data = GeneratePDFPayload(**payload)
elif isinstance(payload, str):
data = GeneratePDFPayload.model_validate_json(payload)
elif isinstance(payload, GeneratePDFPayload):
data = payload
else:
raise ValueError("`payload` must be a valid JSON string or a dict")
# Any data errors would come from here
api = RestRequest(client=self.client)
return api.post(
"generate-pdf",
data=data.model_dump(by_alias=True, exclude_none=True),
**kwargs,
)
def get_cast(
self,
eid: str,
fields: Optional[List[str]] = None,
version_number: Optional[int] = None,
cast_args: Optional[List[Tuple[str, str]]] = None,
**kwargs,
) -> Dict[str, Any]:
if not fields:
# Use default fields
fields = ["eid", "title", "fieldInfo"]
if not cast_args:
cast_args = []
cast_args.append(("eid", f'"{eid}"'))
# If `version_number` isn't provided, the API will default to the
# latest published version.
if version_number:
cast_args.append(("versionNumber", str(version_number)))
arg_str = ""
if len(cast_args):
joined_args = [(":".join(arg)) for arg in cast_args]
arg_str = f"({','.join(joined_args)})"
res = self.query(
gql(
f"""{{
cast {arg_str} {{
{" ".join(fields)}
}}
}}"""
),
**kwargs,
)
def get_data(r: dict) -> Dict[str, Any]:
return r["cast"]
return _get_return(res, get_data=get_data)
def get_casts(
self, fields: Optional[List[str]] = None, show_all: bool = False, **kwargs
) -> List[Dict[str, Any]]:
"""Retrieve all Cast objects for the current user across all organizations.
:param fields: List of fields to retrieve for each cast object
:type fields: Optional[List[str]]
:param show_all: Boolean to show all Cast objects.
Defaults to showing only templates.
:type show_all: bool
:param kwargs:
:return:
"""
if not fields:
# Use default fields
fields = ["eid", "title", "fieldInfo"]
cast_args = "" if show_all else "(isTemplate: true)"
res = self.query(
gql(
f"""{{
currentUser {{
organizations {{
casts {cast_args} {{
{" ".join(fields)}
}}
}}
}}
}}"""
),
**kwargs,
)
def get_data(r: dict):
orgs = r["currentUser"]["organizations"]
return [item for org in orgs for item in org["casts"]]
return _get_return(res, get_data=get_data)
def get_current_user(self, **kwargs):
"""Retrieve current user data.
:param kwargs:
:return:
"""
res = self.query(
gql(
"""{
currentUser {
name
email
eid
role
organizations {
eid
name
slug
casts {
eid
name
}
}
}
}"""
),
**kwargs,
)
return _get_return(res, get_data=lambda r: r["currentUser"])
def get_welds(self, **kwargs) -> Union[List, Tuple[List, Dict]]:
res = self.query(
gql(
"""{
currentUser {
organizations {
welds {
eid
slug
name
forges {
eid
name
}
}
}
}
}"""
),
**kwargs,
)
def get_data(r: dict):
orgs = r["currentUser"]["organizations"]
return [item for org in orgs for item in org["welds"]]
return _get_return(res, get_data=get_data)
def get_weld(self, eid: str, **kwargs):
res = self.query(
gql(
"""
query WeldQuery(
#$organizationSlug: String!,
#$slug: String!
$eid: String!
) {
weld(
#organizationSlug: $organizationSlug,
#slug: $slug
eid: $eid
) {
eid
slug
name
forges {
eid
name
slug
}
}
}"""
),
variables=dict(eid=eid),
**kwargs,
)
def get_data(r: dict):
return r["weld"]
return _get_return(res, get_data=get_data)
def create_etch_packet(
self,
payload: Optional[
Union[
dict,
CreateEtchPacketPayload,
CreateEtchPacket,
AnyStr,
]
] = None,
json=None,
**kwargs,
):
"""Create etch packet via a graphql mutation."""
# Create an etch packet payload instance excluding signers and files
# (if any). We'll need to add those separately. below.
if not any([payload, json]):
raise TypeError('One of the arguments `payload` or `json` must exist')
if json:
payload = CreateEtchPacketPayload.model_validate_json(json)
if isinstance(payload, dict):
mutation = CreateEtchPacket.create_from_dict(payload)
elif isinstance(payload, CreateEtchPacketPayload):
mutation = CreateEtchPacket(payload=payload)
elif isinstance(payload, CreateEtchPacket):
mutation = payload
else:
raise ValueError(
"`payload` must be a valid CreateEtchPacket instance or dict"
)
payload = mutation.create_payload()
variables = payload.model_dump(by_alias=True, exclude_none=True)
return self.mutate(mutation, variables=variables, upload_files=True, **kwargs)
def generate_etch_signing_url(self, signer_eid: str, client_user_id: str, **kwargs):
"""Generate a signing URL for a given user."""
mutation = GenerateEtchSigningURL(
signer_eid=signer_eid,
client_user_id=client_user_id,
)
payload = mutation.create_payload()
return self.mutate(
mutation, variables=payload.model_dump(by_alias=True), **kwargs
)
def download_documents(self, document_group_eid: str, **kwargs):
"""Retrieve all completed documents in zip form."""
api = PlainRequest(client=self.client)
return api.get(f"document-group/{document_group_eid}.zip", **kwargs)
def forge_submit(
self,
payload: Optional[Union[Dict[str, Any], ForgeSubmitPayload]] = None,
json=None,
**kwargs,
) -> Dict[str, Any]:
"""Create a Webform (forge) submission via a graphql mutation."""
if not any([json, payload]):
raise TypeError('One of arguments `json` or `payload` are required')
if json:
payload = ForgeSubmitPayload.model_validate_json(json)
if isinstance(payload, dict):
mutation = ForgeSubmit.create_from_dict(payload)
elif isinstance(payload, ForgeSubmitPayload):
mutation = ForgeSubmit(payload=payload)
else:
raise ValueError(
"`payload` must be a valid ForgeSubmitPayload instance or dict"
)
return self.mutate(
mutation,
variables=mutation.create_payload().model_dump(
by_alias=True, exclude_none=True
),
**kwargs,
)