-
Notifications
You must be signed in to change notification settings - Fork 3
/
crud.py
94 lines (77 loc) · 2.77 KB
/
crud.py
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
89
90
91
92
93
94
import secrets
import time
from typing import List, Optional, Union
from uuid import uuid4
from .db import db
from .helpers import generate_bleskomat_lnurl_hash
from .models import Bleskomat, BleskomatLnurl, CreateBleskomat
async def create_bleskomat(data: CreateBleskomat, wallet_id: str) -> Bleskomat:
bleskomat_id = uuid4().hex
api_key_id = secrets.token_hex(8)
api_key_secret = secrets.token_hex(32)
api_key_encoding = "hex"
bleskomat = Bleskomat(
id=bleskomat_id,
wallet=wallet_id,
api_key_id=api_key_id,
api_key_secret=api_key_secret,
api_key_encoding=api_key_encoding,
**data.dict(),
)
await db.insert("bleskomat.bleskomats", bleskomat)
return bleskomat
async def get_bleskomat(bleskomat_id: str) -> Optional[Bleskomat]:
return await db.fetchone(
"SELECT * FROM bleskomat.bleskomats WHERE id = :id",
{"id": bleskomat_id},
Bleskomat,
)
async def get_bleskomat_by_api_key_id(api_key_id: str) -> Optional[Bleskomat]:
return await db.fetchone(
"SELECT * FROM bleskomat.bleskomats WHERE api_key_id = :id",
{"id": api_key_id},
Bleskomat,
)
async def get_bleskomats(wallet_ids: Union[str, List[str]]) -> List[Bleskomat]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join([f"'{wallet_id}'" for wallet_id in wallet_ids])
return await db.fetchall(
f"SELECT * FROM bleskomat.bleskomats WHERE wallet IN ({q})",
model=Bleskomat,
)
async def update_bleskomat(bleskomat: Bleskomat) -> Bleskomat:
await db.update("bleskomat.bleskomats", bleskomat)
return bleskomat
async def delete_bleskomat(bleskomat_id: str) -> None:
await db.execute(
"DELETE FROM bleskomat.bleskomats WHERE id = :id", {"id": bleskomat_id}
)
async def create_bleskomat_lnurl(
*, bleskomat: Bleskomat, secret: str, tag: str, params: str, uses: int = 1
) -> BleskomatLnurl:
bleskomat_lnurl_id = uuid4().hex
lnurl_hash = generate_bleskomat_lnurl_hash(secret)
now = int(time.time())
bleskomat_lnurl = BleskomatLnurl(
id=bleskomat_lnurl_id,
bleskomat=bleskomat.id,
wallet=bleskomat.wallet,
hash=lnurl_hash,
tag=tag,
params=params,
api_key_id=bleskomat.api_key_id,
initial_uses=uses,
remaining_uses=uses,
created_time=now,
updated_time=now,
)
await db.insert("bleskomat.bleskomat_lnurls", bleskomat_lnurl)
return bleskomat_lnurl
async def get_bleskomat_lnurl(secret: str) -> Optional[BleskomatLnurl]:
lnurl_hash = generate_bleskomat_lnurl_hash(secret)
return await db.fetchone(
"SELECT * FROM bleskomat.bleskomat_lnurls WHERE hash = :hash",
{"hash": lnurl_hash},
BleskomatLnurl,
)