Functions/tapas/funko.py

42 lines
878 B
Python
Raw Normal View History

2024-08-21 20:54:09 +00:00
from flask import Flask, request
2023-05-16 15:35:02 +00:00
from tapita import Cover
from io import BytesIO
import base64
2024-08-21 20:54:09 +00:00
app = Flask("tapas")
2023-05-16 15:35:02 +00:00
2024-08-21 20:54:09 +00:00
@app.route('/', methods=['POST'])
def handle():
2023-05-16 15:35:02 +00:00
"""handle a request to the function
Args:
req (str): request body
{
"title": "foo",
"subtitle": "bar",
"author": "bat",
}
"""
2024-08-21 20:54:09 +00:00
if not request:
return "Foo", 200, {"Content-Type": "text/plain"}
2023-05-16 15:35:02 +00:00
try:
2024-08-21 20:54:09 +00:00
args = request.json
2023-05-16 15:35:02 +00:00
except Exception:
return "Bad Request", 400
c = Cover(**args)
byte_arr = BytesIO()
c.image.save(byte_arr, format="JPEG")
return (
f'<img src="data:image/jpeg;base64, {base64.b64encode(byte_arr.getvalue()).decode("utf-8")}" style="width: 100%; border: solid 1px #aaa;">',
2023-05-16 15:35:02 +00:00
200,
{"Content-Type": "text/html"},
)
2024-08-21 20:54:09 +00:00
@app.route('/ping')
def ping():
return "OK"