Skip to content

Commit 1401af8

Browse files
committed
feat: allow html_key path to be configured
1 parent 1b063b6 commit 1401af8

File tree

3 files changed

+46
-21
lines changed

3 files changed

+46
-21
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "uptick-splat"
3-
version = "0.1.5"
3+
version = "0.1.8"
44
description = "Django library for invoking splat"
55
authors = ["william chu <[email protected]>"]
66
readme = "README.md"

uptick_splat/config.py

+41-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from dataclasses import dataclass
2-
from typing import Any, Callable
2+
from typing import Any, Callable, Optional
3+
from uuid import uuid4
34

45
import boto3
56

@@ -12,9 +13,15 @@ def get_session() -> Any:
1213
return session
1314

1415

16+
def get_tmp_html_key() -> str:
17+
"""This function can be overridden to provide a custom storage_location for temporary html files"""
18+
return f"tmp/{uuid4()}.html"
19+
20+
1521
def delete_key(bucket_name: str, path: str) -> None:
1622
"""This function can be overriden to provide a custom delete key function"""
17-
session = config.get_session()
23+
global config
24+
session = config.get_session_fn()
1825
s3_client = session.client("s3")
1926
try:
2027
s3_client.delete_object(Bucket=bucket_name, Key=path)
@@ -23,12 +30,13 @@ def delete_key(bucket_name: str, path: str) -> None:
2330

2431

2532
def configure_splat(
26-
function_region: str = "ap-southeast-2",
27-
function_name: str = "splat-prod",
28-
default_bucket_name: str = "",
29-
default_tagging: str = "ExpireAfter=1w",
30-
get_session_fn: Callable[[], Any] = get_session,
31-
delete_key_fn: Callable[[str, str], None] = delete_key,
33+
function_region: Optional[str] = None,
34+
function_name: Optional[str] = None,
35+
default_bucket_name: Optional[str] = None,
36+
default_tagging: Optional[str] = None,
37+
get_session_fn: Optional[Callable[[], Any]] = None,
38+
get_tmp_html_key_fn: Optional[Callable[[str], str]] = None,
39+
delete_key_fn: Optional[Callable[[str, str], None]] = None,
3240
):
3341
"""Configure the splat function.
3442
@@ -40,14 +48,20 @@ def configure_splat(
4048
:param default_key_delete_fn: a function that deletes a key from s3
4149
"""
4250
global config
43-
config = Config(
44-
function_region=function_region,
45-
function_name=function_name,
46-
default_bucket_name=default_bucket_name,
47-
default_tagging=default_tagging,
48-
get_session_fn=get_session_fn,
49-
delete_key_fn=delete_key_fn,
50-
)
51+
if function_region is not None:
52+
config.function_region = function_region
53+
if function_name is not None:
54+
config.function_name = function_name
55+
if default_bucket_name is not None:
56+
config.default_bucket_name = default_bucket_name
57+
if default_tagging is not None:
58+
config.default_tagging = default_tagging
59+
if get_session_fn is not None:
60+
config.get_session_fn = get_session_fn
61+
if get_tmp_html_key_fn is not None:
62+
config.get_tmp_html_key_fn = get_tmp_html_key_fn
63+
if delete_key_fn is not None:
64+
config.delete_key_fn = delete_key_fn
5165

5266

5367
@dataclass
@@ -58,7 +72,18 @@ class Config:
5872
default_tagging: str
5973

6074
get_session_fn: Callable[[], Any]
75+
get_tmp_html_key_fn: Callable[[str], str]
6176
delete_key_fn: Callable[[str, str], None]
6277

6378

6479
configure_splat()
80+
81+
config = Config(
82+
function_region="ap-southeast-2",
83+
function_name="splat-prod",
84+
default_bucket_name="",
85+
default_tagging="ExpireAfter=1w",
86+
get_session_fn=get_session,
87+
get_tmp_html_key_fn=get_tmp_html_key,
88+
delete_key_fn=delete_key,
89+
)

uptick_splat/utils.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,18 @@ def pdf_from_html(
6868
)
6969

7070
# Upload body HTML to s3 and get a link to hand to splat
71-
html_key = os.path.join(bucket_name, "tmp", f"{uuid4()}.html")
71+
tmp_html_key = config.get_tmp_html_key_fn()
7272

7373
s3_client.put_object(
7474
Body=body_html,
7575
Bucket=bucket_name,
76-
Key=html_key,
76+
Key=tmp_html_key,
7777
Tagging=config.default_tagging,
7878
)
7979

8080
document_url = s3_client.generate_presigned_url(
8181
"get_object",
82-
Params={"Bucket": bucket_name, "Key": html_key},
82+
Params={"Bucket": bucket_name, "Key": tmp_html_key},
8383
ExpiresIn=1800,
8484
)
8585

@@ -97,7 +97,7 @@ def pdf_from_html(
9797
)
9898

9999
# Remove the temporary html file from s3
100-
config.delete_key_fn(bucket_name, html_key)
100+
config.delete_key_fn(bucket_name, tmp_html_key)
101101

102102
# Check response of the invocation. Note that a successful invocation doesn't mean the PDF was generated.
103103
if response.get("StatusCode") != 200:

0 commit comments

Comments
 (0)