Skip to content
This repository has been archived by the owner on Jan 12, 2025. It is now read-only.

Commit

Permalink
feat: add tests for templates functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hughcrt committed Oct 29, 2024
1 parent 39ca8b5 commit 505ff2b
Show file tree
Hide file tree
Showing 6 changed files with 679 additions and 100 deletions.
18 changes: 12 additions & 6 deletions lunary/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import warnings, traceback, logging, copy, time, chevron, aiohttp, copy

from pkg_resources import parse_version
from packaging.version import parse as parse_version
from importlib.metadata import version, PackageNotFoundError
from contextvars import ContextVar
from datetime import datetime, timezone
Expand Down Expand Up @@ -1503,11 +1503,13 @@ def get_raw_template(slug: str, app_id: str | None = None, api_url: str | None =
Raises:
TemplateError: If fetching the template fails.
"""

try:
config = get_config()
token = app_id or config.app_id
api_url = api_url or config.api_url
base_url = api_url or config.api_url

if not token:
raise TemplateError("No authentication token provided")

global templateCache
now = time.time() * 1000
Expand All @@ -1518,19 +1520,23 @@ def get_raw_template(slug: str, app_id: str | None = None, api_url: str | None =

headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
response = requests.get(
f"{api_url}/v1/template_versions/latest?slug={slug}",
f"{base_url}/v1/template_versions/latest?slug={slug}",
headers=headers,
verify=config.ssl_verify,
)

if response.status_code == 401:
raise TemplateError("Invalid or unauthorized API credentials")

if not response.ok:
raise TemplateError(f"Error fetching template: {response.status_code} - {response.text}")

data = response.json()
templateCache[slug] = {"timestamp": now, "data": data}
return data
except TemplateError:
raise

except requests.exceptions.RequestException as e:
raise TemplateError(f"Network error while fetching template: {str(e)}")
except Exception as e:
raise TemplateError(f"Error fetching template: {str(e)}")

Expand Down
2 changes: 1 addition & 1 deletion lunary/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ def create_uuid_from_string(seed_string):
hash_hex = sha256_hash.hexdigest()
uuid_hex = hash_hex[:32]
uuid_obj = uuid.UUID(uuid_hex)
return uuid_obj
return uuid_obj
Loading

0 comments on commit 505ff2b

Please sign in to comment.