-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathviews_api.py
175 lines (129 loc) · 5.33 KB
/
views_api.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Description: This file contains the extensions API endpoints.
from http import HTTPStatus
from fastapi import APIRouter, Depends, Request
from lnbits.core.crud import get_user
from lnbits.core.models import WalletTypeInfo
from lnbits.core.services import create_invoice
from lnbits.decorators import require_admin_key, require_invoice_key
from starlette.exceptions import HTTPException
from .crud import (
create_myextension,
delete_myextension,
get_myextension,
get_myextensions,
update_myextension,
)
from .helpers import lnurler
from .models import CreateMyExtensionData, CreatePayment, MyExtension
myextension_api_router = APIRouter()
# Note: we add the lnurl params to returns so the links
# are generated in the MyExtension model in models.py
## Get all the records belonging to the user
@myextension_api_router.get("/api/v1/myex")
async def api_myextensions(
req: Request, # Withoutthe lnurl stuff this wouldnt be needed
wallet: WalletTypeInfo = Depends(require_invoice_key),
) -> list[MyExtension]:
wallet_ids = [wallet.wallet.id]
user = await get_user(wallet.wallet.user)
wallet_ids = user.wallet_ids if user else []
myextensions = await get_myextensions(wallet_ids)
# Populate lnurlpay and lnurlwithdraw for each instance.
# Without the lnurl stuff this wouldnt be needed.
for myex in myextensions:
myex.lnurlpay = lnurler(myex.id, "myextension.api_lnurl_pay", req)
myex.lnurlwithdraw = lnurler(myex.id, "myextension.api_lnurl_withdraw", req)
return myextensions
## Get a single record
@myextension_api_router.get(
"/api/v1/myex/{myextension_id}",
dependencies=[Depends(require_invoice_key)],
)
async def api_myextension(myextension_id: str, req: Request) -> MyExtension:
myex = await get_myextension(myextension_id)
if not myex:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="MyExtension does not exist."
)
# Populate lnurlpay and lnurlwithdraw.
# Without the lnurl stuff this wouldnt be needed.
myex.lnurlpay = lnurler(myex.id, "myextension.api_lnurl_pay", req)
myex.lnurlwithdraw = lnurler(myex.id, "myextension.api_lnurl_withdraw", req)
return myex
## Create a new record
@myextension_api_router.post("/api/v1/myex", status_code=HTTPStatus.CREATED)
async def api_myextension_create(
req: Request, # Withoutthe lnurl stuff this wouldnt be needed
data: CreateMyExtensionData,
wallet: WalletTypeInfo = Depends(require_admin_key),
) -> MyExtension:
myex = await create_myextension(data)
# Populate lnurlpay and lnurlwithdraw.
# Withoutthe lnurl stuff this wouldnt be needed.
myex.lnurlpay = lnurler(myex.id, "myextension.api_lnurl_pay", req)
myex.lnurlwithdraw = lnurler(myex.id, "myextension.api_lnurl_withdraw", req)
return myex
## update a record
@myextension_api_router.put("/api/v1/myex/{myextension_id}")
async def api_myextension_update(
req: Request, # Withoutthe lnurl stuff this wouldnt be needed
data: CreateMyExtensionData,
myextension_id: str,
wallet: WalletTypeInfo = Depends(require_admin_key),
) -> MyExtension:
myex = await get_myextension(myextension_id)
if not myex:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="MyExtension does not exist."
)
if wallet.wallet.id != myex.wallet:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="Not your MyExtension."
)
for key, value in data.dict().items():
setattr(myex, key, value)
myex = await update_myextension(data)
# Populate lnurlpay and lnurlwithdraw.
# Without the lnurl stuff this wouldnt be needed.
myex.lnurlpay = lnurler(myex.id, "myextension.api_lnurl_pay", req)
myex.lnurlwithdraw = lnurler(myex.id, "myextension.api_lnurl_withdraw", req)
return myex
## Delete a record
@myextension_api_router.delete("/api/v1/myex/{myextension_id}")
async def api_myextension_delete(
myextension_id: str, wallet: WalletTypeInfo = Depends(require_admin_key)
):
myex = await get_myextension(myextension_id)
if not myex:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="MyExtension does not exist."
)
if myex.wallet != wallet.wallet.id:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="Not your MyExtension."
)
await delete_myextension(myextension_id)
return
# ANY OTHER ENDPOINTS YOU NEED
## This endpoint creates a payment
@myextension_api_router.post("/api/v1/myex/payment", status_code=HTTPStatus.CREATED)
async def api_myextension_create_invoice(data: CreatePayment) -> dict:
myextension = await get_myextension(data.myextension_id)
if not myextension:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="MyExtension does not exist."
)
# we create a payment and add some tags,
# so tasks.py can grab the payment once its paid
payment = await create_invoice(
wallet_id=myextension.wallet,
amount=data.amount,
memo=(
f"{data.memo} to {myextension.name}" if data.memo else f"{myextension.name}"
),
extra={
"tag": "myextension",
"amount": data.amount,
},
)
return {"payment_hash": payment.payment_hash, "payment_request": payment.bolt11}