Created Queries and Execute endpoint
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
# dependencies.py
|
||||
from fastapi import Depends, HTTPException, status, Security
|
||||
|
||||
from fastapi.security import APIKeyHeader
|
||||
@@ -6,10 +5,6 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.future import select
|
||||
from data.db import SessionLocal
|
||||
from data.models import User, UserRole
|
||||
from pydantic import BaseModel
|
||||
|
||||
# class UserInDB(User):
|
||||
# hashed_password: str
|
||||
|
||||
async def get_db():
|
||||
async with SessionLocal() as session:
|
||||
@@ -18,7 +13,7 @@ async def get_db():
|
||||
API_KEY_NAME = "Authorization"
|
||||
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
|
||||
|
||||
async def get_current_user(db: AsyncSession = Depends(get_db), api_key:str = Security(api_key_header)):
|
||||
async def get_current_user(db: AsyncSession = Depends(get_db), api_key:str = Security(api_key_header)) -> User:
|
||||
if api_key_header is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN, detail="API key missing"
|
||||
@@ -34,7 +29,7 @@ async def get_current_user(db: AsyncSession = Depends(get_db), api_key:str = Sec
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid credentials")
|
||||
return user
|
||||
|
||||
async def get_admin_user(current_user: User = Depends(get_current_user)):
|
||||
async def get_admin_user(current_user: User = Depends(get_current_user)) -> User:
|
||||
if current_user.role != UserRole.admin:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Not enough permissions")
|
||||
return current_user
|
||||
|
||||
@@ -6,4 +6,22 @@ class ConnectionTypes(str, enum.Enum):
|
||||
|
||||
class UserRole(enum.Enum):
|
||||
admin = "admin"
|
||||
user = "user"
|
||||
user = "user"
|
||||
|
||||
|
||||
class FilterOperator(str, enum.Enum):
|
||||
eq = "="
|
||||
neq = "!="
|
||||
gt = ">"
|
||||
lt = "<"
|
||||
gte = ">="
|
||||
lte = "<="
|
||||
like = "LIKE"
|
||||
ilike = "ILIKE"
|
||||
in_ = "IN"
|
||||
is_null = "IS NULL"
|
||||
is_not_null = "IS NOT NULL"
|
||||
|
||||
class SortOrder(str, enum.Enum):
|
||||
asc = "ASC"
|
||||
desc = "DESC"
|
||||
@@ -17,3 +17,33 @@ class UserNotFound(HTTPException):
|
||||
headers=None,
|
||||
):
|
||||
super().__init__(status_code, detail, headers)
|
||||
|
||||
|
||||
class QueryValidationError(ValueError):
|
||||
def __init__(self, loc: list[str], msg: str):
|
||||
self.loc = loc
|
||||
self.msg = msg
|
||||
super().__init__(msg)
|
||||
|
||||
class QueryNotFound(HTTPException):
|
||||
def __init__(self, status_code=404, detail = {
|
||||
'message': "The referenced query was not found.",
|
||||
"code": 'query-not-found'
|
||||
}, headers = None):
|
||||
super().__init__(status_code, detail, headers)
|
||||
|
||||
|
||||
class ConnectionNotFound(HTTPException):
|
||||
def __init__(self, status_code=404, detail = {
|
||||
'message': "The referenced connection was not found.",
|
||||
"code": 'connection-not-found'
|
||||
}, headers = None):
|
||||
super().__init__(status_code, detail, headers)
|
||||
|
||||
|
||||
class PoolNotFound(HTTPException):
|
||||
def __init__(self, status_code=404, detail = {
|
||||
'message': "We didn't find a running Pool for the referenced connection.",
|
||||
"code": 'pool-not-found'
|
||||
}, headers = None):
|
||||
super().__init__(status_code, detail, headers)
|
||||
@@ -1,44 +0,0 @@
|
||||
# add_user.py
|
||||
import asyncio
|
||||
import secrets
|
||||
from sqlalchemy.future import select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from getpass import getpass
|
||||
from data.db import engine, SessionLocal
|
||||
from data.models import Base, User, UserRole
|
||||
|
||||
def create_secret():
|
||||
return secrets.token_hex(32)
|
||||
|
||||
async def create_user():
|
||||
|
||||
async with SessionLocal() as session:
|
||||
username = input("Enter username: ").strip()
|
||||
role_input = input("Enter role (admin/user): ").strip().lower()
|
||||
print('\n')
|
||||
if role_input not in UserRole._value2member_map_:
|
||||
print("> Invalid role. Please enter 'admin' or 'user'.")
|
||||
return
|
||||
|
||||
role = UserRole(role_input)
|
||||
|
||||
# Check if username already exists
|
||||
result = await session.execute(select(User).filter_by(username=username))
|
||||
existing_user = result.scalars().first()
|
||||
if existing_user:
|
||||
print(f"> Username '{username}' is already taken.")
|
||||
return
|
||||
|
||||
# Create new user
|
||||
api_key = create_secret()
|
||||
new_user = User(username=username, role=role, api_key=api_key)
|
||||
session.add(new_user)
|
||||
await session.commit()
|
||||
await session.refresh(new_user)
|
||||
|
||||
|
||||
print(f"> User '{username}' with role '{role.value}' created successfully.")
|
||||
print(f"> API Key: {api_key}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(create_user())
|
||||
Reference in New Issue
Block a user