Moved code to subfolder
This commit is contained in:
14
backend/routes/admin.py
Normal file
14
backend/routes/admin.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from database.database import *
|
||||
from models.request import *
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.post("/cleardb", response_description="Clears database cache")
|
||||
async def cleardb():
|
||||
await clear_requests()
|
||||
|
||||
@router.get("/dbcache", response_description="Show database contents", response_model=List[Request])
|
||||
async def showcache():
|
||||
return await retrieve_requests()
|
37
backend/routes/request.py
Normal file
37
backend/routes/request.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from fastapi import APIRouter
|
||||
import uuid
|
||||
import time
|
||||
|
||||
from azure import translate as azure_translate
|
||||
from database.database import *
|
||||
from models.request import *
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/translate", response_description="Translate text", response_model=Request)
|
||||
async def translate(data: RequestData):
|
||||
# Check if this request is saved in the database
|
||||
check_db = await retrieve_request(data.requested_text, data.language_from, data.language_to)
|
||||
|
||||
if len(check_db) == 0: # Not in database, use azure api
|
||||
print("Not in database, use Azure api")
|
||||
# Make api request
|
||||
response = azure_translate(data)
|
||||
print(response)
|
||||
|
||||
# Create object
|
||||
item = Request(
|
||||
id=str(uuid.uuid4()),
|
||||
requested_text=data.requested_text,
|
||||
response_text=response[0]['translations'][0]['text'],
|
||||
language_from=data.language_from,
|
||||
language_to=data.language_to,
|
||||
unixdate=int(time.time())
|
||||
)
|
||||
|
||||
# Save it to the database and return it
|
||||
return await add_request(item=item)
|
||||
else:
|
||||
print("Already in database, serving the cached result")
|
||||
return check_db[0]
|
Reference in New Issue
Block a user