Update API, refactor and fix bugs

Co-authored-by: Ezequiel Bellver <ebellver@itba.edu.ar>
Co-authored-by: Juan Barmasch <jbarmasch@itba.edu.ar>
This commit is contained in:
Santiago Lo Coco 2022-11-29 11:14:08 -03:00
parent 1e1576679a
commit ec80c1a96c
21 changed files with 246 additions and 205 deletions

View File

@ -1,14 +1,17 @@
from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi import APIRouter
from fastapi.responses import JSONResponse
from bsition.api.components.document import documents_routes
from bsition.api.components.table import tables_routes
from bsition.api.components.user import users_routes
from bsition.api.routes.auth import auth_routes
from bsition.api.endpoints import documents, tables, users, login
app = FastAPI()
app.include_router(auth_routes, prefix="/api")
app.include_router(documents_routes, prefix="/api")
app.include_router(users_routes, prefix="/api")
app.include_router(tables_routes, prefix="/api")
load_dotenv()
router = APIRouter()
@router.get("")
def info():
return JSONResponse(content={"message": "BSition API v1"}, status_code=200)
router.include_router(login.router)
router.include_router(documents.router, prefix="/documents")
router.include_router(users.router, prefix="/users")
router.include_router(tables.router, prefix="/tables")

View File

@ -1,52 +0,0 @@
import json
from fastapi import APIRouter, Response, status
from pydantic import BaseModel
from pydantic.types import Optional
from bsition.api.middleware.verify_token import VerifyTokenRoute
from bsition.backend import elastic, mongo
documents_routes = APIRouter(route_class=VerifyTokenRoute)
class Document(BaseModel):
name: str
access: list
data: str
owner: str
class DocumentUpdate(BaseModel):
name: Optional[str]
access: Optional[list]
data: Optional[str]
@documents_routes.post("/documents")
def create(aux: Document, response: Response):
mongo.create_document(json.loads(json.dumps(aux.__dict__)))
response.status_code = status.HTTP_201_CREATED
@documents_routes.get("/documents")
def get_documents(query: str = None):
if query is not None:
return elastic.search("test-index", query)
return mongo.get_documents()
@documents_routes.get("/documents/{id}")
def get_by_id(id: str):
return mongo.get_document_by_id(id)
@documents_routes.put("/documents/{id}")
def edit_data(aux: DocumentUpdate, id: str, response: Response):
if aux.data is not None:
mongo.edit_data(id, aux.data)
if aux.access is not None:
mongo.edit_access(id, aux.access)
if aux.name is not None:
mongo.edit_name(id, aux.name)
response.status_code = status.HTTP_202_ACCEPTED

View File

@ -1,79 +0,0 @@
from fastapi import APIRouter, Response, status
from pydantic import BaseModel
from pydantic.utils import Optional
from bsition.api.middleware.verify_token import VerifyTokenRoute
from bsition.backend import postgres
tables_routes = APIRouter(route_class=VerifyTokenRoute)
class Table(BaseModel):
name: Optional[str]
column: Optional[str]
type: Optional[str]
column_data: Optional[list]
row_number: Optional[str]
columns: Optional[list]
columns_data: Optional[list]
class Sort(BaseModel):
property: str
order: str
priority: str
class Filter(BaseModel):
property: str
value: str
function: str
@tables_routes.post("/tables")
def create_table(aux: Table, response: Response):
postgres.create_table(aux.name)
response.status_code = status.HTTP_201_CREATED
@tables_routes.put("/tables/{name}")
def edit_table(aux: Table, name: str):
if aux.column is not None and aux.type is not None:
postgres.add_column(name, aux.column, aux.type)
if aux.column_data is not None:
postgres.insert_columns(name, aux.column_data)
if aux.row_number is not None:
postgres.edit_columns(name, aux.columns, aux.columns_data, aux.row_number)
@tables_routes.post("/tables/{name}/sort")
def create_sort(name: str, response: Response):
postgres.create_sort(name)
response.status_code = status.HTTP_201_CREATED
@tables_routes.put("/tables/{name}/sort")
def add_sort(aux: Sort, name: str):
postgres.add_sort(name, aux.property, aux.order, aux.priority)
@tables_routes.get("/tables/{name}/sort")
def sort(name: str):
return postgres.sort(name)
@tables_routes.post("/tables/{name}/filter")
def create_filter(name: str, response: Response):
postgres.create_filter(name)
postgres.add_filter_trigger(name)
response.status_code = status.HTTP_201_CREATED
@tables_routes.put("/tables/{name}/filter")
def add_filter(aux: Filter, name: str):
postgres.add_filter(name, aux.property, aux.value, aux.function)
@tables_routes.get("/tables/{name}/filter")
def filter(name: str):
return postgres.filter(name)

View File

@ -1,37 +0,0 @@
from fastapi import APIRouter, Response, status
from pydantic import BaseModel
from bsition.api.middleware.verify_token import VerifyTokenRoute
from bsition.backend import postgres
users_routes = APIRouter()
class User(BaseModel):
username: str
password: str
@users_routes.post("/users")
def create_user(aux: User, response: Response):
postgres.add_user(aux.username, aux.password)
response.status_code = status.HTTP_201_CREATED
users_routes.route_class = VerifyTokenRoute
@users_routes.get("/users")
def get_users():
return postgres.get_users()
@users_routes.get("/users/{id}")
def get_by_id(id: str):
return postgres.get_user_by_id(id)
@users_routes.put("/users/{id}")
def edit_user(aux: User, id: str, response: Response):
postgres.edit_user(id, aux.username, aux.password)
response.status_code = status.HTTP_202_ACCEPTED

View File

@ -0,0 +1,39 @@
import json
from fastapi import APIRouter
from fastapi.responses import JSONResponse
from bsition.api.middleware.verify_token import VerifyTokenRoute
from bsition.api.models.document import Document, DocumentUpdate
from bsition.backend import elastic, mongo
router = APIRouter(route_class=VerifyTokenRoute)
@router.post("")
def create(aux: Document):
mongo.create_document(json.loads(json.dumps(aux.__dict__)))
return JSONResponse(content={"detail": "Document created"}, status_code=201)
@router.get("")
def get_documents(query: str = None):
if query is not None:
return elastic.search("test-index", query)
return mongo.get_documents()
@router.get("/{id}")
def get_by_id(id: str):
return mongo.get_document_by_id(id)
@router.put("/{id}")
def edit_data(aux: DocumentUpdate, id: str):
if aux.data is not None:
mongo.edit_data(id, aux.data)
if aux.access is not None:
mongo.edit_access(id, aux.access)
if aux.name is not None:
mongo.edit_name(id, aux.name)
return JSONResponse(content={"detail": "Document updated."}, status_code=202)

View File

@ -0,0 +1,24 @@
from fastapi import APIRouter, HTTPException
from fastapi.responses import JSONResponse
from bsition.api.utils.jwt import write_token
from bsition.api.models.user import User
from bsition.backend.postgres import get_user_by_username_and_password
router = APIRouter()
@router.post("/login")
def login(user: User):
if get_user_by_username_and_password(user.username, user.password) is not None:
return JSONResponse(
content={
"access_token": write_token(user.dict()),
"token_type": "bearer"
},
status_code=202)
else:
raise HTTPException(
status_code=400,
detail="User not found.",
)

View File

@ -0,0 +1,60 @@
from fastapi import APIRouter
from fastapi.responses import JSONResponse
from bsition.api.middleware.verify_token import VerifyTokenRoute
from bsition.api.models.table import Table, Sort, Filter
from bsition.backend import postgres
router = APIRouter(route_class=VerifyTokenRoute)
@router.post("")
def create_table(aux: Table):
postgres.create_table(aux.name)
return JSONResponse(content={"detail": "Table created."}, status_code=201)
@router.put("/{name}")
def edit_table(aux: Table, name: str):
if aux.column is not None and aux.type is not None:
postgres.add_column(name, aux.column, aux.type)
if aux.column_data is not None:
postgres.insert_columns(name, aux.column_data)
if aux.row_number is not None:
postgres.edit_columns(name, aux.columns, aux.columns_data, aux.row_number)
return JSONResponse(content={"detail": "Table updated."}, status_code=202)
@router.post("/{name}/sort")
def create_sort(name: str):
postgres.create_sort(name)
return JSONResponse(content={"detail": "Sort created."}, status_code=201)
@router.put("/{name}/sort")
def add_sort(aux: Sort, name: str):
postgres.add_sort(name, aux.property, aux.order, aux.priority)
return JSONResponse(content={"detail": "Sort updated."}, status_code=202)
@router.get("/{name}/sort")
def sort(name: str):
return postgres.sort(name)
@router.post("/{name}/filter")
def create_filter(name: str):
postgres.create_filter(name)
postgres.add_filter_trigger(name)
return JSONResponse(content={"detail": "Filter created."}, status_code=201)
@router.put("/{name}/filter")
def add_filter(aux: Filter, name: str):
postgres.add_filter(name, aux.property, aux.value, aux.function)
return JSONResponse(content={"detail": "Filter updated."}, status_code=202)
@router.get("/{name}/filter")
def filter(name: str):
return postgres.filter(name)

View File

@ -0,0 +1,39 @@
from fastapi import APIRouter, HTTPException
from fastapi.responses import JSONResponse
from bsition.api.middleware.verify_token import VerifyTokenRoute
from bsition.api.models.user import User
from bsition.backend import postgres
router = APIRouter()
@router.post("")
def create_user(user: User):
if postgres.get_user_by_username_and_password(user.username, user.password) is not None:
raise HTTPException(
status_code=400,
detail="User already exists.",
)
postgres.add_user(user.username, user.password)
return JSONResponse(content={"detail": "User created."}, status_code=201)
router.route_class = VerifyTokenRoute
@router.get("")
def get_users():
return postgres.get_users()
@router.get("/{id}")
def get_by_id(id: str):
return postgres.get_user_by_id(id)
@router.put("/{id}")
def edit_user(aux: User, id: str):
postgres.edit_user(id, aux.username, aux.password)
return JSONResponse(content={"detail": "User updated."}, status_code=202)

8
bsition/api/main.py Normal file
View File

@ -0,0 +1,8 @@
from fastapi import FastAPI
from dotenv import load_dotenv
from bsition.api.api import router
app = FastAPI()
app.include_router(router, prefix="/api")
load_dotenv()

View File

@ -1,7 +1,7 @@
from fastapi import Request
from fastapi.routing import APIRoute
from bsition.api.jwt import validate_token
from bsition.api.utils.jwt import validate_token
class VerifyTokenRoute(APIRoute):

View File

@ -0,0 +1,15 @@
from pydantic import BaseModel
from pydantic.types import Optional
class Document(BaseModel):
name: str
access: list
data: str
owner: str
class DocumentUpdate(BaseModel):
name: Optional[str]
access: Optional[list]
data: Optional[str]

View File

@ -0,0 +1,24 @@
from pydantic import BaseModel
from pydantic.utils import Optional
class Table(BaseModel):
name: Optional[str]
column: Optional[str]
type: Optional[str]
column_data: Optional[list]
row_number: Optional[str]
columns: Optional[list]
columns_data: Optional[list]
class Sort(BaseModel):
property: str
order: str
priority: str
class Filter(BaseModel):
property: str
value: str
function: str

View File

@ -0,0 +1,7 @@
from pydantic import BaseModel
from pydantic.types import Optional
class User(BaseModel):
username: str
password: Optional[str]

View File

@ -1,20 +0,0 @@
from fastapi import APIRouter
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from bsition.api.jwt import write_token
from bsition.backend.postgres import get_user_by_username
auth_routes = APIRouter()
class User(BaseModel):
username: str
@auth_routes.post("/login")
def login(user: User):
if get_user_by_username(user.username) is not None:
return write_token(user.dict())
else:
return JSONResponse(content={"message": "User not found"}, status_code=404)

View File

View File

@ -11,7 +11,7 @@ def expire_date(days: int):
def write_token(data: dict):
return encode(
payload={**data, "exp": expire_date(2)}, key=getenv("SECRET"), algorithm="HS256"
payload={**data, "exp": expire_date(1)}, key=getenv("SECRET"), algorithm="HS256"
)

View File

@ -282,6 +282,17 @@ def get_user_by_username(username):
return cur.fetchone()
def get_user_by_username_and_password(username, password):
conn = get_connection()
cur = conn.cursor()
cur.execute(
sql.SQL("SELECT * FROM users WHERE username = {username} AND password = {password}").format(
username=sql.Literal(username), password=sql.Literal(password)
)
)
return cur.fetchone()
def edit_user(id, username, password):
conn = get_connection()
cur = conn.cursor()
@ -293,7 +304,6 @@ def edit_user(id, username, password):
if data[i] is None:
continue
print(id)
cur.execute(
sql.SQL("UPDATE users SET {col} = {value} WHERE id = {id}").format(
col=sql.Identifier(column),

View File

@ -33,7 +33,7 @@ services:
environment:
- xpack.security.enabled=false
- discovery.type=single-node
- "ES_JAVA_OPTS=-Xms1G -Xmx1G"
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
volumes:
- ./data/elasticsearch:/usr/share/elasticsearch/data
ports:

2
run.sh
View File

@ -26,7 +26,7 @@ done
if [ "$RUN" = 'install' ]; then
poetry install
elif [ "$RUN" = 'api' ]; then
poetry run uvicorn bsition.api.api:app --host 0.0.0.0
poetry run uvicorn bsition.api.main:app --host 0.0.0.0
elif [ "$RUN" = 'configure' ]; then
poetry run configure
else