File size: 1,655 Bytes
51a9974
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Client for the shared shell gallery (talks to the Modal gallery endpoints).

Set the three endpoint URLs via env (printed when you `modal deploy
modal_gallery.py`), or they default to the conventional Modal URL shape for the
'slug-gallery' app under the known account.
"""

import os
import json

import httpx

_ACCOUNT = "anubhavbharadwaaj"
SAVE_URL = os.environ.get(
    "GALLERY_SAVE_URL", f"https://{_ACCOUNT}--slug-gallery-save.modal.run")
LIST_URL = os.environ.get(
    "GALLERY_LIST_URL", f"https://{_ACCOUNT}--slug-gallery-list-shells.modal.run")
SHELL_URL = os.environ.get(
    "GALLERY_SHELL_URL", f"https://{_ACCOUNT}--slug-gallery-shell.modal.run")

_TIMEOUT = 20


def save_shell(svg: str, meta: dict) -> str | None:
    """Save a shell to the shared gallery. Returns the new id, or None on failure."""
    try:
        r = httpx.post(SAVE_URL, json={"svg": svg, "meta": meta}, timeout=_TIMEOUT)
        r.raise_for_status()
        return r.json().get("id")
    except Exception:
        return None


def list_shells(limit: int = 60) -> list[dict]:
    """Newest-first metadata index. Empty list on failure."""
    try:
        r = httpx.get(LIST_URL, params={"limit": limit}, timeout=_TIMEOUT)
        r.raise_for_status()
        return r.json().get("shells", [])
    except Exception:
        return []


def get_shell(shell_id: str) -> dict | None:
    """One shell {svg, meta} by id. None on failure."""
    try:
        r = httpx.get(SHELL_URL, params={"id": shell_id}, timeout=_TIMEOUT)
        r.raise_for_status()
        data = r.json()
        return data if "svg" in data else None
    except Exception:
        return None