2023-08-22 23:51:39 +00:00
|
|
|
"""Python FastAPI Auth0 integration example
|
|
|
|
"""
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
|
|
from fastapi import FastAPI, Security
|
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
|
|
|
|
from .utils import VerifyToken
|
|
|
|
from .database import TodoDatabase
|
|
|
|
from .thermal_print import ThermalPrinter
|
|
|
|
|
|
|
|
# from wsgiref import simple_server
|
|
|
|
|
|
|
|
# Creates app instance
|
|
|
|
app = FastAPI()
|
|
|
|
auth = VerifyToken()
|
|
|
|
data = TodoDatabase()
|
2023-08-23 02:32:59 +00:00
|
|
|
printer = ThermalPrinter(data)
|
2023-08-24 18:34:08 +00:00
|
|
|
# printer.print_default() #temp debug
|
2023-08-24 22:18:10 +00:00
|
|
|
printer.print_qrcode()
|
2023-08-22 23:51:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Todo(BaseModel):
|
|
|
|
time: str
|
|
|
|
task: str
|
|
|
|
recurring: bool
|
|
|
|
|
|
|
|
class TodoList(BaseModel):
|
2023-08-24 22:18:10 +00:00
|
|
|
date: str #Always selected date OLD TEXT: #Either current date in %Y-%m-%d format or "recurring" for a recurring task
|
2023-08-22 23:51:39 +00:00
|
|
|
todos: list[Todo]
|
|
|
|
|
|
|
|
|
|
|
|
class TodoDate(BaseModel):
|
|
|
|
date: str
|
|
|
|
# date: str = datetime.today().strftime('%Y-%m-%d')
|
|
|
|
|
|
|
|
|
2023-08-24 00:02:31 +00:00
|
|
|
class PrintAction(BaseModel):
|
|
|
|
date: str = datetime.today().strftime('%Y-%m-%d')
|
|
|
|
action: str #Options:
|
2023-08-24 18:34:08 +00:00
|
|
|
# all (pulls current todos from date, needs date)
|
2023-08-24 00:02:31 +00:00
|
|
|
# sudoku (prints a random sudoku)
|
|
|
|
# todos (prints only dates todos)
|
|
|
|
# wordsearch (prints a random wordsearch)
|
|
|
|
# quote (prints a random quote)
|
|
|
|
# greeting (prints a greeting)
|
2023-08-24 18:34:08 +00:00
|
|
|
# sentence (prints a custom message)
|
|
|
|
sentence: str = ""
|
2023-08-24 00:02:31 +00:00
|
|
|
|
2023-08-22 23:51:39 +00:00
|
|
|
# @app.get("/api/public")
|
|
|
|
# def public():
|
|
|
|
# """No access token required to access this route"""
|
|
|
|
|
|
|
|
# result = {
|
|
|
|
# "status": "success",
|
|
|
|
# "msg": ("Hello from a public endpoint! You don't need to be "
|
|
|
|
# "authenticated to see this.")
|
|
|
|
# }
|
|
|
|
# return result
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/api/todos/get")
|
|
|
|
def get_todos(date: str = datetime.today().strftime('%Y-%m-%d'), auth_result: str = Security(auth.verify)):
|
|
|
|
"""A valid access token is required to access this route"""
|
|
|
|
|
|
|
|
return {
|
|
|
|
"todos": data.read_todos(date)
|
|
|
|
}
|
|
|
|
|
|
|
|
# result = {
|
|
|
|
# "todos": [
|
|
|
|
# {"time": "1:00pm to 2:00pm", "text": "Read a book"},
|
|
|
|
# {"time": "2:00pm to 3:00pm", "text": "Go for a walk"}
|
|
|
|
# ]
|
|
|
|
# } #TODO: replace with database access
|
|
|
|
|
|
|
|
# return result
|
|
|
|
|
|
|
|
|
|
|
|
# return auth_result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/todos/write")
|
|
|
|
def write_todos(todos: TodoList, auth_result: str = Security(auth.verify)):
|
|
|
|
"""A valid access token is required to access this route"""
|
|
|
|
|
|
|
|
# result = {
|
|
|
|
# "todos": [
|
|
|
|
# {"time": "1:00pm to 2:00pm", "text": "Read a book"},
|
|
|
|
# {"time": "2:00pm to 3:00pm", "text": "Go for a walk"}
|
|
|
|
# ]
|
|
|
|
# } #TODO: replace with database write
|
|
|
|
|
|
|
|
# return result
|
|
|
|
|
|
|
|
# data.save_todo()
|
|
|
|
|
|
|
|
print(todos)
|
|
|
|
|
|
|
|
# if todos.date != "recurring":
|
|
|
|
# data.remove_todos(todos.date)
|
|
|
|
data.remove_todos(todos.date)
|
|
|
|
data.save_todos(todos.date, todos.todos)
|
|
|
|
|
|
|
|
return {"result": todos.date}
|
|
|
|
|
|
|
|
# return todos
|
|
|
|
|
|
|
|
|
|
|
|
# @app.post("/api/todos/write_recurring")
|
|
|
|
# def write_todos_recurring(todos: TodoList, auth_result: str = Security(auth.verify)):
|
|
|
|
|
|
|
|
# data.save_todos("recurring", todos.todos)
|
|
|
|
|
|
|
|
# return {"result": "success"}
|
|
|
|
|
|
|
|
|
2023-08-24 20:59:35 +00:00
|
|
|
@app.post("/api/todos/print")
|
2023-08-24 18:34:08 +00:00
|
|
|
def print_todos(action: PrintAction, auth_result: str = Security(auth.verify)):
|
2023-08-22 23:51:39 +00:00
|
|
|
"""A valid access token is required to access this route"""
|
|
|
|
|
|
|
|
# result = {
|
|
|
|
# "todos": [
|
|
|
|
# {"time": "1:00pm to 2:00pm", "text": "Read a book"},
|
|
|
|
# {"time": "2:00pm to 3:00pm", "text": "Go for a walk"}
|
|
|
|
# ]
|
|
|
|
# } #TODO: replace with access to database if no todos provided from request, otherwise print request data
|
2023-08-24 18:34:08 +00:00
|
|
|
|
|
|
|
#Options:
|
|
|
|
# all (pulls current todos from date, needs date)
|
|
|
|
# sudoku (prints a random sudoku)
|
|
|
|
# todos (prints only dates todos)
|
|
|
|
# wordsearch (prints a random wordsearch)
|
|
|
|
# quote (prints a random quote)
|
|
|
|
# greeting (prints a greeting)
|
|
|
|
# sentence (prints a custom message)
|
|
|
|
if action.action == "all":
|
|
|
|
printer.print_default()
|
2023-08-22 23:51:39 +00:00
|
|
|
|
2023-08-24 18:34:08 +00:00
|
|
|
if action.action == "sudoku":
|
|
|
|
printer.print_sudoku()
|
|
|
|
|
|
|
|
if action.action == "todos":
|
2023-08-24 18:36:44 +00:00
|
|
|
todos = data.read_todos(action.date)
|
|
|
|
printer.print_todos(todos)
|
2023-08-24 18:34:08 +00:00
|
|
|
|
|
|
|
if action.action == "wordsearch":
|
|
|
|
printer.print_wordsearch()
|
|
|
|
|
|
|
|
if action.action == "quote":
|
|
|
|
printer.print_random_quote()
|
|
|
|
|
|
|
|
if action.action == "greeting":
|
|
|
|
printer.print_greeting()
|
|
|
|
|
|
|
|
if action.action == "sentence":
|
|
|
|
printer.print_custom(action.sentence)
|
|
|
|
|
|
|
|
|
|
|
|
printer.finished_printing()
|
|
|
|
|
|
|
|
|
|
|
|
# todos = data.get_todos(action.date)
|
|
|
|
# printer.print_todos()
|
2023-08-22 23:51:39 +00:00
|
|
|
|
|
|
|
return {"result": "success"}
|
|
|
|
|
|
|
|
# class SaveTodos:
|
|
|
|
# def on_post(self, req, resp):
|
|
|
|
# pass #Handle web interface sending updated todo list
|
|
|
|
|
|
|
|
# class PrintTodos:
|
|
|
|
# def on_post(self, req, resp):
|
|
|
|
# pass #Handle web interface requesting a print action
|
|
|
|
|
|
|
|
|
|
|
|
# @app.get("/api/get_todos-scoped")
|
|
|
|
# def private_scoped(auth_result: str = Security(auth.verify, scopes=['todos:all'])):
|
|
|
|
# """A valid access token and an appropriate scope are required to access
|
|
|
|
# this route
|
|
|
|
# """
|
|
|
|
|
|
|
|
# return auth_result
|
|
|
|
|
|
|
|
|
|
|
|
app.mount("/", StaticFiles(directory="application/static",html = True), name="static")
|
|
|
|
|
|
|
|
|
2023-08-23 02:18:06 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
printer.print_default()
|
|
|
|
|
2023-08-22 23:51:39 +00:00
|
|
|
# if __name__ == '__main__':
|
|
|
|
# print('ay')
|
|
|
|
# httpd = simple_server.make_server('127.0.0.1', 8000, app)
|
|
|
|
# httpd.serve_forever()
|