Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method for generating signed Smart CDN URLs #33

Merged
merged 10 commits into from
Nov 28, 2024
12 changes: 6 additions & 6 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def test_get_signed_smart_cdn_url(self):
params['template'],
params['input'],
{},
3600 * 1000 # 1 hour
expires_at_ms=self.expire_at_ms
)

expected_url = 'https://workspace.tlcdn.com/template/file.jpg?auth_key=test-key&exp=1732550672867&sig=sha256%3Ad994b8a737db1c43d6e04a07018dc33e8e28b23b27854bd6383d828a212cfffb'
Expand All @@ -176,7 +176,7 @@ def test_get_signed_smart_cdn_url(self):
params['template'],
params['input'],
{},
3600 * 1000
expires_at_ms=self.expire_at_ms
)

expected_url = 'https://workspace.tlcdn.com/template/input.jpg?auth_key=test-key&exp=1732550672867&sig=sha256%3A75991f02828d194792c9c99f8fea65761bcc4c62dbb287a84f642033128297c0'
Expand All @@ -192,7 +192,7 @@ def test_get_signed_smart_cdn_url(self):
params['template'],
params['input'],
params['url_params'],
3600 * 1000
expires_at_ms=self.expire_at_ms
)

expected_url = 'https://workspace.tlcdn.com/template/file.jpg?auth_key=test-key&exp=1732550672867&width=100&sig=sha256%3Ae5271d8fb6482d9351ebe4285b6fc75539c4d311ff125c4d76d690ad71c258ef'
Expand All @@ -207,7 +207,7 @@ def test_get_signed_smart_cdn_url(self):
params['template'],
params['input'],
params['url_params'],
3600 * 1000
expires_at_ms=self.expire_at_ms
)

expected_url = 'https://workspace.tlcdn.com/template/file.jpg?auth_key=test-key&exp=1732550672867&height=200&width=&sig=sha256%3A1a26733c859f070bc3d83eb3174650d7a0155642e44a5ac448a43bc728bc0f85'
Expand All @@ -222,7 +222,7 @@ def test_get_signed_smart_cdn_url(self):
params['template'],
params['input'],
params['url_params'],
3600 * 1000
expires_at_ms=self.expire_at_ms
)

expected_url = 'https://workspace.tlcdn.com/template/file.jpg?auth_key=test-key&exp=1732550672867&height=200&sig=sha256%3Adb740ebdfad6e766ebf6516ed5ff6543174709f8916a254f8d069c1701cef517'
Expand All @@ -237,7 +237,7 @@ def test_get_signed_smart_cdn_url(self):
params['template'],
params['input'],
params['url_params'],
3600 * 1000
expires_at_ms=self.expire_at_ms
)

expected_url = 'https://workspace.tlcdn.com/template/file.jpg?auth_key=test-key&exp=1732550672867&width=&sig=sha256%3A840426f9ac72dde02fd080f09b2304d659fdd41e630b1036927ec1336c312e9d'
Expand Down
8 changes: 5 additions & 3 deletions transloadit/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def get_signed_smart_cdn_url(
template: str,
input: str,
url_params: Optional[dict[str, Union[str, int, float, bool, List[Union[str, int, float, bool]], None]]] = None,
expires_in: Optional[int] = 60 * 60 * 1000 # 1 hour
expires_at_ms: Optional[int] = None
) -> str:
"""
Construct a signed Smart CDN URL.
Expand All @@ -190,7 +190,7 @@ def get_signed_smart_cdn_url(
- template (str): Template slug or template ID
- input (str): Input value that is provided as ${fields.input} in the template
- url_params (Optional[dict]): Additional parameters for the URL query string. Values can be strings, numbers, booleans, arrays thereof, or None.
- expires_in (Optional[int]): Expiration time of signature in milliseconds. Defaults to 1 hour.
- expires_at_ms (Optional[int]): Timestamp in milliseconds since epoch when the signature is no longer valid. Defaults to 1 hour from now.
Acconut marked this conversation as resolved.
Show resolved Hide resolved

:Returns:
str: The signed Smart CDN URL
Expand All @@ -202,6 +202,8 @@ def get_signed_smart_cdn_url(
template_slug = quote_plus(template)
input_field = quote_plus(input)

expiry = expires_at_ms if expires_at_ms is not None else int(time.time() * 1000) + 60 * 60 * 1000 # 1 hour default

params = []
if url_params:
for k, v in url_params.items():
Expand All @@ -215,7 +217,7 @@ def get_signed_smart_cdn_url(
raise ValueError(f"URL parameter values must be strings, numbers, booleans, arrays, or None. Got {type(v)} for {k}")

params.append(("auth_key", self.auth_key))
params.append(("exp", str(int(time.time() * 1000) + expires_in)))
params.append(("exp", str(expiry)))

# Sort params alphabetically by key
sorted_params = sorted(params, key=lambda x: x[0])
Expand Down