-
Notifications
You must be signed in to change notification settings - Fork 0
/
views_api.py
132 lines (109 loc) · 4.44 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
from http import HTTPStatus
from fastapi import APIRouter, Depends, HTTPException, UploadFile
from fastapi.responses import FileResponse
from lnbits.core.crud import get_wallet
from lnbits.core.models import WalletTypeInfo
from lnbits.core.services import create_invoice
from lnbits.decorators import check_user_exists, require_admin_key, require_invoice_key
from .crud import (
create_print,
create_printer,
delete_printer,
get_print,
get_printer,
get_printers,
get_prints,
update_printer,
)
from .helpers import print_file_path, safe_file_name
from .models import CreatePrinter, Print, Printer
pay2print_ext_api = APIRouter(
prefix="/api/v1",
tags=["pay2print"],
)
async def _validate_input_wallet(user_id: str, wallet_id: str) -> None:
wallet = await get_wallet(wallet_id)
if not wallet:
raise HTTPException(HTTPStatus.BAD_REQUEST, "Wallet not found.")
if wallet.user != user_id:
raise HTTPException(HTTPStatus.BAD_REQUEST, "Wallet does not belong to user.")
@pay2print_ext_api.get("/printer")
async def api_get_printers(
key_info: WalletTypeInfo = Depends(require_invoice_key),
) -> list[Printer]:
return await get_printers(key_info.wallet.user)
@pay2print_ext_api.post("/printer")
async def api_create_printer(
data: CreatePrinter,
key_info: WalletTypeInfo = Depends(require_admin_key),
) -> Printer:
if data.wallet != key_info.wallet.id:
await _validate_input_wallet(key_info.wallet.user, data.wallet)
return await create_printer(key_info.wallet.user, data)
@pay2print_ext_api.post(
"/upload/{printer_id}",
description="Public upload endpoint, returns a payment request.",
)
async def api_upload_print(printer_id: str, file: UploadFile) -> str:
printer = await get_printer(printer_id)
if not printer:
raise HTTPException(HTTPStatus.NOT_FOUND, "Printer not found.")
if not file or not file.filename or not file.content_type:
raise HTTPException(HTTPStatus.BAD_REQUEST, "No file uploaded.")
if "application/pdf" not in file.content_type:
raise HTTPException(HTTPStatus.BAD_REQUEST, "Only PDF files are supported.")
upload_file_path = safe_file_name(file.filename)
open(upload_file_path, "wb").write(await file.read())
payment = await create_invoice(
wallet_id=printer.wallet,
amount=100,
memo=f"Print payment for file: {file.filename}",
extra={"tag": "pay2print"},
)
await create_print(payment.payment_hash, printer_id, upload_file_path.name)
return payment.bolt11
@pay2print_ext_api.get("/file/{print_id}", dependencies=[Depends(check_user_exists)])
async def api_show_file(print_id: str) -> FileResponse:
_print = await get_print(print_id)
if not _print:
raise HTTPException(HTTPStatus.NOT_FOUND, "Print not found.")
return FileResponse(
print_file_path(_print.file), media_type="application/pdf", filename=_print.file
)
@pay2print_ext_api.put("/printer/{printer_id}")
async def api_update_printer(
printer_id: str,
data: CreatePrinter,
key_info: WalletTypeInfo = Depends(require_admin_key),
) -> Printer:
if data.wallet != key_info.wallet.id:
await _validate_input_wallet(key_info.wallet.user, data.wallet)
printer = await get_printer(printer_id)
if not printer:
raise HTTPException(HTTPStatus.NOT_FOUND, "Printer not found.")
if key_info.wallet.user != printer.user_id:
raise HTTPException(HTTPStatus.FORBIDDEN, "User not allowed to update printer.")
printer.wallet = data.wallet
printer.host = data.host
if data.name:
printer.name = data.name
return await update_printer(printer)
@pay2print_ext_api.delete("/printer/{printer_id}")
async def api_delete_printer(
printer_id: str,
key_info: WalletTypeInfo = Depends(require_admin_key),
) -> None:
printer = await get_printer(printer_id)
if not printer:
raise HTTPException(HTTPStatus.NOT_FOUND, "Printer not found.")
if key_info.wallet.user != printer.user_id:
raise HTTPException(HTTPStatus.FORBIDDEN, "User not allowed to delete printer.")
return await delete_printer(printer_id)
@pay2print_ext_api.get(
"/print/{printer_id}", dependencies=[Depends(require_invoice_key)]
)
async def api_get_prints(printer_id: str) -> list[Print]:
printer = await get_printer(printer_id)
if not printer:
raise HTTPException(HTTPStatus.NOT_FOUND, "Printer not found.")
return await get_prints(printer_id)