-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsplat_cli.py
More file actions
executable file
·95 lines (77 loc) · 2.98 KB
/
splat_cli.py
File metadata and controls
executable file
·95 lines (77 loc) · 2.98 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
#!/usr/bin/env -S uv run -s
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "boto3",
# "requests",
# "typer",
# ]
# ///
import argparse
import base64
import enum
import json
import subprocess
import boto3
import requests
import typer
DEFAULT_LAMBDA_URL = "http://localhost:8080/2015-03-31/functions/function/invocations"
parser = argparse.ArgumentParser(
description="Run against splat locally. Sample usage: ./splat_cli.py --open -o /tmp/google.pdf -b https://google.com"
)
class Renderer(enum.StrEnum):
princexml = "princexml"
browser = "browser"
def invoke_function(
document_content: str | None = typer.Option(None, "--content", "-c"),
document_url: str | None = typer.Option(None, "--url", "-u"),
browser_url: str | None = typer.Option(None, "--browser-url", "-b"),
renderer: Renderer = typer.Option(Renderer.princexml, "--renderer", "-r"),
output_path: str = typer.Option("/tmp/output.pdf", "--output", "-o"), # noqa
lambda_url: str = typer.Option(DEFAULT_LAMBDA_URL, "--lambda-url", "-l"),
function_name: str | None = typer.Option(None, "--function-name", "-f"),
) -> bytes:
"""
Usage:
Invoke using a local function url
./splat_cli.py -o /tmp/google.pdf -b https://google.com
Invoke using a deployed lambda against an embedded document
./splat_cli.py -o /tmp/google.pdf -c "<h1> hi </h1>" --function-name splat-staging
"""
if not (document_content or document_url or browser_url):
print("Please provide document content or document url or browser url")
raise typer.Exit(code=1)
body = {"renderer": renderer}
if document_content:
body["document_content"] = document_content
elif document_url:
body["document_url"] = document_url
elif browser_url:
body["browser_url"] = browser_url
if function_name:
lambda_client = boto3.client("lambda")
response = lambda_client.invoke(FunctionName=function_name, Payload=json.dumps({"body": json.dumps(body)}))
status_code = response.get("StatusCode")
if status_code not in {200, 201}:
print("Something went wrong!")
raise Exception(response)
data = json.loads(response["Payload"].read().decode("utf-8"))
status_code = data["statusCode"]
is_base64_encoded = data["isBase64Encoded"]
else:
response = requests.post(lambda_url, json={"body": json.dumps(body)}, timeout=60)
response.raise_for_status()
data = response.json()
status_code = data["statusCode"]
is_base64_encoded = data["isBase64Encoded"]
if is_base64_encoded:
body = base64.b64decode(data["body"])
else:
body = json.loads(data.get("body")) if data.get("body") else {}
if output_path:
print(f"Writing to: {output_path}")
with open(output_path, "wb") as f:
f.write(body) # noqa
subprocess.run(["open", output_path]) # noqa
if __name__ == "__main__":
typer.run(invoke_function)