File size: 2,339 Bytes
2cc5f0e
eeba9fe
 
2cc5f0e
 
 
5bf0f62
2cc5f0e
5bf0f62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cc5f0e
5bf0f62
 
 
 
 
 
 
 
 
2cc5f0e
 
5bf0f62
 
 
 
 
 
 
2cc5f0e
5bf0f62
 
2cc5f0e
eeba9fe
2cc5f0e
5bf0f62
 
 
 
 
 
 
2cc5f0e
eeba9fe
2cc5f0e
5bf0f62
 
 
 
 
eeba9fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bf0f62
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
# PATH: bot/integrations/cf_worker1.py
from __future__ import annotations

from bot.config import Workers
from bot.integrations.http import post_json


def _h():
    return {
        "Authorization": f"Bearer {Workers.BOT_BACKEND_KEY}",
        "Content-Type": "application/json",
    }


def _unwrap(r: dict) -> dict:
    """
    post_json() may return:
      A) direct backend json -> {"ok": true, ...}
      B) wrapper -> {"ok": true, "status": 200, "data": {...}}
    handlers.py expects (A), so convert (B) -> (A)
    """
    if isinstance(r, dict) and isinstance(r.get("data"), dict):
        data = r["data"]
        if "ok" not in data and "ok" in r:
            data["ok"] = r["ok"]
        return data
    return r


async def profile_add(
    tg_id: int,
    client_id: str,
    client_secret: str,
    label: str = "main",
    ttl_sec: int = 600,
) -> dict:
    r = await post_json(
        f"{Workers.WORKER1_URL}/api/profile/add",
        _h(),
        {
            "tg_id": str(tg_id),
            "client_id": client_id,
            "client_secret": client_secret,
            "label": label,
            "ttl_sec": ttl_sec,
        },
    )
    return _unwrap(r)


# existing (kept)
async def profile_list(tg_id: int) -> dict:
    r = await post_json(
        f"{Workers.WORKER1_URL}/api/profile/list",
        _h(),
        {"tg_id": str(tg_id)},
    )
    return _unwrap(r)


# existing (kept)
async def profile_set_default(tg_id: int, profile_id: str) -> dict:
    r = await post_json(
        f"{Workers.WORKER1_URL}/api/profile/set_default",
        _h(),
        {"tg_id": str(tg_id), "profile_id": profile_id},
    )
    return _unwrap(r)


# ✅ Added: handlers.py expects this name
async def profile_check_auth(tg_id: int, profile_id: str, ttl_sec: int = 600, force: bool = False) -> dict:
    r = await post_json(
        f"{Workers.WORKER1_URL}/api/profile/login_link",
        _h(),
        {"tg_id": str(tg_id), "profile_id": str(profile_id), "ttl_sec": int(ttl_sec), "force": bool(force)},
    )
    return _unwrap(r)


# ✅ Added: handlers.py expects this name
async def profile_delete(tg_id: int, profile_id: str) -> dict:
    r = await post_json(
        f"{Workers.WORKER1_URL}/api/profile/remove",
        _h(),
        {"tg_id": str(tg_id), "profile_id": str(profile_id)},
    )
    return _unwrap(r)