-
Notifications
You must be signed in to change notification settings - Fork 1
/
views_api.py
276 lines (237 loc) · 9.38 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from http import HTTPStatus
from fastapi import APIRouter, Depends, HTTPException, Query, Request
from fastapi.responses import RedirectResponse
from lnbits.core.crud import get_user, get_wallet
from lnbits.core.models import WalletTypeInfo
from lnbits.decorators import require_admin_key, require_invoice_key
from lnbits.utils.exchange_rates import btc_price
from .crud import (
authenticate_service,
create_donation,
create_service,
delete_donation,
delete_service,
get_donation,
get_donations,
get_service,
get_service_redirect_uri,
get_services,
post_donation,
update_donation,
update_service,
)
from .helpers import create_charge, delete_charge, get_charge_status
from .models import CreateDonation, CreateService, Donation, Service, ValidateDonation
streamalerts_api_router = APIRouter()
@streamalerts_api_router.post(
"/api/v1/services", dependencies=[Depends(require_admin_key)]
)
async def api_create_service(data: CreateService) -> Service:
"""Create a service, which holds data about how/where to post donations"""
service = await create_service(data)
return service
@streamalerts_api_router.get("/api/v1/getaccess/{service_id}")
async def api_get_access(service_id: str, request: Request):
"""Redirect to Streamlabs' Approve/Decline page for API access for Service
with service_id
"""
service = await get_service(service_id)
if not service:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="Service does not exist!"
)
redirect_uri = await get_service_redirect_uri(request, service_id)
params = {
"response_type": "code",
"client_id": service.client_id,
"redirect_uri": redirect_uri,
"scope": "donations.create",
"state": service.state,
}
endpoint_url = "https://streamlabs.com/api/v1.0/authorize/?"
querystring = "&".join([f"{key}={value}" for key, value in params.items()])
redirect_url = endpoint_url + querystring
return RedirectResponse(redirect_url)
@streamalerts_api_router.get("/api/v1/authenticate/{service_id}")
async def api_authenticate_service(
service_id: str, request: Request, code: str = Query(...), state: str = Query(...)
):
"""Endpoint visited via redirect during third party API authentication
If successful, an API access token will be added to the service, and
the user will be redirected to index.html.
"""
service = await get_service(service_id)
assert service
if service.state != state:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="State doesn't match!"
)
redirect_uri = request.url.scheme + "://" + request.headers["Host"]
redirect_uri += f"/streamalerts/api/v1/authenticate/{service_id}"
url, success = await authenticate_service(service_id, code, redirect_uri)
if success:
return RedirectResponse(url)
else:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="Service already authenticated!"
)
@streamalerts_api_router.post("/api/v1/donations")
async def api_create_donation(data: CreateDonation, request: Request):
"""Take data from donation form and return satspay charge"""
service = await get_service(data.service)
if not service:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Service not found!"
)
wallet = await get_wallet(service.wallet)
if not wallet:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Wallet not found!"
)
# Currency is hardcoded while frotnend is limited
# Fiat amount is calculated here while frontend is limited
price = await btc_price(data.cur_code)
amount = data.sats * (10 ** (-8)) * price
webhook_base = request.url.scheme + "://" + request.headers["Host"]
description = f"{data.sats} sats donation from {data.name} to {service.twitchuser}"
create_charge_data = {
"amount": data.sats,
"completelink": f"https://twitch.tv/{service.twitchuser}",
"completelinktext": "Back to Stream!",
"webhook": webhook_base + "/streamalerts/api/v1/postdonation",
"description": description,
"time": 1440,
"lnbitswallet": service.wallet,
"onchainwallet": service.onchain,
"user": wallet.user,
}
charge_id = await create_charge(data=create_charge_data, api_key=wallet.inkey)
await create_donation(
data=data,
wallet=service.wallet,
amount=amount,
donation_id=charge_id,
)
return {"redirect_url": f"/satspay/{charge_id}"}
@streamalerts_api_router.post("/api/v1/postdonation")
async def api_post_donation(data: ValidateDonation):
"""Post a paid donation to Stremalabs/StreamElements.
This endpoint acts as a webhook for the SatsPayServer extension."""
donation_id = data.id
donation = await get_donation(donation_id)
if not donation:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail=f"Donation '{donation_id}' not found!",
)
wallet = await get_wallet(donation.wallet)
assert wallet, f"Could not fetch wallet: {donation.wallet}"
charge = await get_charge_status(donation_id, wallet.inkey)
if charge and charge.paid:
return await post_donation(donation_id)
else:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="Not a paid charge!"
)
@streamalerts_api_router.get("/api/v1/services")
async def api_get_services(
key_info: WalletTypeInfo = Depends(require_invoice_key),
) -> list[Service]:
"""Return list of all services assigned to wallet with given invoice key"""
user = await get_user(key_info.wallet.user)
wallet_ids = user.wallet_ids if user else []
services = []
for wallet_id in wallet_ids:
new_services = await get_services(wallet_id)
services += new_services if new_services else []
return services
@streamalerts_api_router.get("/api/v1/donations")
async def api_get_donations(
key_info: WalletTypeInfo = Depends(require_invoice_key),
) -> list[Donation]:
"""Return list of all donations assigned to wallet with given invoice
key
"""
user = await get_user(key_info.wallet.user)
wallet_ids = user.wallet_ids if user else []
donations = []
for wallet_id in wallet_ids:
new_donations = await get_donations(wallet_id)
donations += new_donations if new_donations else []
return donations
@streamalerts_api_router.put("/api/v1/donations/{donation_id}")
async def api_update_donation(
data: CreateDonation,
donation_id: str,
key_info: WalletTypeInfo = Depends(require_admin_key),
) -> Donation:
"""Update a donation with the data given in the request"""
donation = await get_donation(donation_id)
if not donation:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Donation does not exist."
)
if donation.wallet != key_info.wallet.id:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="Not your donation."
)
for k, v in data.dict().items():
setattr(donation, k, v)
await update_donation(donation)
return donation
@streamalerts_api_router.put("/api/v1/services/{service_id}")
async def api_update_service(
service_id: str,
data: CreateService,
key_info: WalletTypeInfo = Depends(require_admin_key),
) -> Service:
"""Update a service with the data given in the request"""
service = await get_service(service_id)
if not service:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Service does not exist."
)
if service.wallet != key_info.wallet.id:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="Not your service."
)
for k, v in data.dict().items():
setattr(service, k, v)
service = await update_service(service)
return service
@streamalerts_api_router.delete("/api/v1/donations/{donation_id}")
async def api_delete_donation(
donation_id: str, key_info: WalletTypeInfo = Depends(require_admin_key)
):
"""Delete the donation with the given donation_id"""
donation = await get_donation(donation_id)
if not donation:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="No donation with this ID!"
)
if donation.wallet != key_info.wallet.id:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN,
detail="Not authorized to delete this donation!",
)
await delete_donation(donation_id)
await delete_charge(donation_id, key_info.wallet.adminkey)
return "", HTTPStatus.NO_CONTENT
@streamalerts_api_router.delete("/api/v1/services/{service_id}")
async def api_delete_service(
service_id: str, key_info: WalletTypeInfo = Depends(require_admin_key)
):
"""Delete the service with the given service_id"""
service = await get_service(service_id)
if not service:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="No service with this ID!"
)
if service.wallet != key_info.wallet.id:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN,
detail="Not authorized to delete this service!",
)
donations = await delete_service(service_id)
for d in donations:
await delete_charge(d, key_info.wallet.adminkey)