forked from anvilco/python-anvil
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
363 lines (296 loc) · 10.1 KB
/
cli.py
File metadata and controls
363 lines (296 loc) · 10.1 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
import click
import os
from csv import DictReader
from logging import getLogger
from tabulate import tabulate
from time import sleep
from typing import List
from python_anvil import utils
from .api import Anvil
from .api_resources.payload import FillPDFPayload
logger = getLogger(__name__)
def get_api_key():
return os.environ.get("ANVIL_API_KEY")
def contains_headers(res):
return isinstance(res, dict) and "headers" in res
def process_response(res):
return res["response"], res["headers"]
@click.group()
@click.option("--debug/--no-debug", default=False)
@click.pass_context
def cli(ctx: click.Context, debug=False):
ctx.ensure_object(dict)
key = get_api_key()
if not key:
raise ValueError("$ANVIL_API_KEY must be defined in your environment variables")
anvil = Anvil(key)
ctx.obj["anvil"] = anvil
ctx.obj["debug"] = debug
@cli.command("current-user", help="Show details about your API user")
@click.pass_context
def current_user(ctx):
anvil = ctx.obj["anvil"]
debug = ctx.obj["debug"]
res = anvil.get_current_user(debug=debug)
if contains_headers(res):
res, headers = process_response(res)
if debug:
click.echo(headers)
click.echo(f"User data: \n\n {res}")
@cli.command()
@click.option(
"-i", "-in", "input_filename", help="Filename of input payload", required=True
)
@click.option(
"-o",
"--out",
"out_filename",
help="Filename of output PDF",
required=True,
)
@click.pass_context
def generate_pdf(ctx, input_filename, out_filename):
"""Generate a PDF."""
anvil = ctx.obj["anvil"]
debug = ctx.obj["debug"]
with click.open_file(input_filename, "r") as infile:
res = anvil.generate_pdf(infile.read(), debug=debug)
if contains_headers(res):
res, headers = process_response(res)
if debug:
click.echo(headers)
with click.open_file(out_filename, "wb") as file:
file.write(res)
@cli.command()
@click.option("-l", "--list", "list_all", help="List all available welds", is_flag=True)
@click.argument("eid", default="")
@click.pass_context
def weld(ctx, eid, list_all):
"""Fetch weld info or list of welds."""
anvil = ctx.obj["anvil"]
debug = ctx.obj["debug"]
if list_all:
res = anvil.get_welds(debug=debug)
if contains_headers(res):
res, headers = process_response(res)
if debug:
click.echo(headers)
data = [(w["eid"], w.get("slug"), w.get("name"), w.get("forges")) for w in res]
click.echo(tabulate(data, tablefmt="pretty", headers=["eid", "slug", "title"]))
return
if not eid:
# pylint: disable=broad-exception-raised
raise Exception("You need to pass in a weld eid")
res = anvil.get_weld(eid)
print(res)
@cli.command()
@click.option(
"-l",
"--list",
"list_templates",
help="List available casts marked as templates",
is_flag=True,
)
@click.option(
"-a", "--all", "list_all", help="List all casts, even non-templates", is_flag=True
)
@click.option("--version_number", help="Get the specified version of this cast")
@click.argument("eid", default="")
@click.pass_context
def cast(ctx, eid, version_number, list_all, list_templates):
"""Fetch Cast data given a Cast eid."""
anvil = ctx.obj["anvil"] # type: Anvil
debug = ctx.obj["debug"]
if not eid and not (list_templates or list_all):
raise AssertionError("Cast eid or --list/--all option required")
if list_all or list_templates:
res = anvil.get_casts(show_all=list_all, debug=debug)
if contains_headers(res):
res, headers = process_response(res)
if debug:
click.echo(headers)
data = [[c["eid"], c["title"]] for c in res]
click.echo(tabulate(data, headers=["eid", "title"]))
return
if eid:
click.echo(f"Getting cast with eid '{eid}' \n")
_res = anvil.get_cast(eid, version_number=version_number, debug=debug)
if contains_headers(_res):
res, headers = process_response(res)
if debug:
click.echo(headers)
def get_field_info(cc):
return tabulate(cc.get("fields", []))
if not _res:
click.echo(f"Cast with eid: {eid} not found")
return
table_data = [[_res["eid"], _res["title"], get_field_info(_res["fieldInfo"])]]
click.echo(tabulate(table_data, tablefmt="pretty", headers=list(_res.keys())))
@cli.command("fill-pdf")
@click.argument("template_id")
@click.option(
"-o",
"--out",
"out_filename",
required=True,
help="Filename of output PDF",
)
@click.option(
"-i",
"--input",
"payload_csv",
required=True,
help="Filename of input CSV that provides data",
)
@click.pass_context
def fill_pdf(ctx, template_id, out_filename, payload_csv):
"""Fill PDF template with data."""
anvil = ctx.obj["anvil"]
debug = ctx.obj["debug"]
if all([template_id, out_filename, payload_csv]):
payloads = [] # type: List[FillPDFPayload]
with click.open_file(payload_csv, "r") as csv_file:
reader = DictReader(csv_file)
# NOTE: This is potentially problematic for larger datasets and/or
# very long csv files, but not sure if the use-case is there yet..
#
# Once memory/execution times are a problem for this command, the
# `progressbar()` can be removed below and we could just work on
# each csv line individually without loading it all into memory
# as we are doing here (or with `list()`). But then that removes
# the nice progress bar, so..trade-offs!
for row in reader:
payloads.append(FillPDFPayload(data=dict(row)))
with click.progressbar(payloads, label="Filling PDFs and saving") as ps:
indexed_files = utils.build_batch_filenames(out_filename)
for payload in ps:
res = anvil.fill_pdf(template_id, payload.model_dump(), debug=debug)
if contains_headers(res):
res, headers = process_response(res)
if debug:
click.echo(headers)
next_file = next(indexed_files)
click.echo(f"\nWriting {next_file}")
with click.open_file(next_file, "wb") as file:
file.write(res)
sleep(1)
@cli.command("create-etch")
@click.option(
"-p",
"--payload",
"payload",
type=click.File('rb'),
required=True,
help="File that contains JSON payload",
)
@click.pass_context
def create_etch(ctx, payload):
"""Create an etch packet with a JSON file.
Example usage:
# For existing files
> $ ANVIL_API_KEY=mykey anvil create-etch --payload=my_payload_file.json
# You can also get data from STDIN
> $ ANVIL_API_KEY=mykey anvil create-etch --payload -
"""
anvil = ctx.obj["anvil"]
debug = ctx.obj["debug"]
res = anvil.create_etch_packet(json=payload.read(), debug=debug)
if contains_headers(res):
res, headers = process_response(res)
if debug:
click.echo(headers)
if "data" in res:
click.echo(
f"Etch packet created with id: {res['data']['createEtchPacket']['eid']}"
)
else:
click.echo(res)
@cli.command("generate-etch-url", help="Generate an etch url for a signer")
@click.option(
"-c",
"--client",
"client_user_id",
required=True,
help="The signer's user id in your system belongs here",
)
@click.option(
"-s",
"--signer",
"signer_eid",
required=True,
help="The eid of the next signer belongs here. The signer's eid can be "
"found in the response of the `createEtchPacket` mutation",
)
@click.pass_context
def generate_etch_url(ctx, signer_eid, client_user_id):
anvil = ctx.obj["anvil"]
debug = ctx.obj["debug"]
res = anvil.generate_etch_signing_url(
signer_eid=signer_eid, client_user_id=client_user_id, debug=debug
)
if contains_headers(res):
res, headers = process_response(res)
if debug:
click.echo(headers)
url = res.get("data", {}).get("generateEtchSignURL")
click.echo(f"Signing URL is: {url}")
@cli.command("download-documents", help="Download etch documents")
@click.option(
"-d",
"--document-group",
"document_group_eid",
required=True,
help="The documentGroupEid can be found in the response of the "
"createEtchPacket or sendEtchPacket mutations.",
)
@click.option(
"-f", "--filename", "filename", help="Optional filename for the downloaded zip file"
)
@click.option(
"--stdout/--no-stdout",
help="Instead of writing to a file, output data to STDOUT",
default=False,
)
@click.pass_context
def download_documents(ctx, document_group_eid, filename, stdout):
anvil = ctx.obj["anvil"]
debug = ctx.obj["debug"]
res = anvil.download_documents(document_group_eid, debug=debug)
if contains_headers(res):
res, headers = process_response(res)
if debug:
click.echo(headers)
if not stdout:
if not filename:
filename = f"{document_group_eid}.zip"
with click.open_file(filename, 'wb') as out_file:
out_file.write(res)
click.echo(f"Saved as '{click.format_filename(filename)}'")
else:
click.echo(res)
@cli.command('gql-query', help="Run a raw graphql query")
@click.option(
"-q",
"--query",
"query",
required=True,
help="The query body. This is the 'query' part of the JSON payload",
)
@click.option(
"-v",
"--variables",
"variables",
help="The query variables. This is the 'variables' part of the JSON payload",
)
@click.pass_context
def gql_query(ctx, query, variables):
anvil = ctx.obj["anvil"]
debug = ctx.obj["debug"]
res = anvil.query(query, variables=variables, debug=debug)
if contains_headers(res):
res, headers = process_response(res)
if debug:
click.echo(headers)
click.echo(res)
if __name__ == "__main__": # pragma: no cover
cli() # pylint: disable=no-value-for-parameter