Initial commit with the basics
This commit is contained in:
0
app/routers/__init__.py
Normal file
0
app/routers/__init__.py
Normal file
43
app/routers/items.py
Normal file
43
app/routers/items.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
from pydantic import BaseModel, EmailStr, Field
|
||||
from typing import Dict, List, Optional, Set, Union
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
class Image(BaseModel):
|
||||
url: str
|
||||
name: str
|
||||
|
||||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: Optional[str] = Field(None, example='A very nice Item')
|
||||
price: float
|
||||
tax: Optional[float] = None
|
||||
is_offer: Optional[bool] = None
|
||||
#tags: List[str] = [] # not unique tags
|
||||
tags: Set[str] = set() # unique tags
|
||||
image: Optional[Image] = None # one image
|
||||
images: Optional[List[Image]] = None # or as a list of images
|
||||
|
||||
|
||||
@router.get('/')
|
||||
async def read_items():
|
||||
return [{'name': 'Item Foo'}, {'name': 'item Bar'}]
|
||||
|
||||
|
||||
@router.get('/{item_id}')
|
||||
async def read_item(item_id: str):
|
||||
return {'name': 'Fake Specific Item', 'item_id': item_id}
|
||||
|
||||
|
||||
@router.put(
|
||||
'/{item_id}',
|
||||
tags=['Extra Tag'],
|
||||
responses={403: {'description': 'Operation forbidden'}},
|
||||
)
|
||||
async def update_item(item_id: str):
|
||||
if item_id != 'foo':
|
||||
raise HTTPException(status_code=403, detail='You can only update the item: foo')
|
||||
return {'item_id': item_id, 'name': 'The Fighters'}
|
||||
24
app/routers/items_basic.py
Normal file
24
app/routers/items_basic.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/")
|
||||
async def read_items():
|
||||
return [{"name": "Item Foo"}, {"name": "item Bar"}]
|
||||
|
||||
|
||||
@router.get("/{item_id}")
|
||||
async def read_item(item_id: str):
|
||||
return {"name": "Fake Specific Item", "item_id": item_id}
|
||||
|
||||
|
||||
@router.put(
|
||||
"/{item_id}",
|
||||
tags=["custom"],
|
||||
responses={403: {"description": "Operation forbidden"}},
|
||||
)
|
||||
async def update_item(item_id: str):
|
||||
if item_id != "foo":
|
||||
raise HTTPException(status_code=403, detail="You can only update the item: foo")
|
||||
return {"item_id": item_id, "name": "The Fighters"}
|
||||
153
app/routers/users.py
Normal file
153
app/routers/users.py
Normal file
@@ -0,0 +1,153 @@
|
||||
from datetime import datetime, time, timedelta
|
||||
from fastapi import APIRouter, Depends, Header, HTTPException, status
|
||||
from pydantic import BaseModel, EmailStr, Field
|
||||
from typing import Dict, List, Optional, Set, Union
|
||||
|
||||
from ..lib_general import *
|
||||
from app.config import settings
|
||||
|
||||
from app.db import *
|
||||
|
||||
|
||||
#import logging
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
class UserBase(BaseModel):
|
||||
id_random: str = None # This should not be None. It is required.
|
||||
account_id_random: str = None # This should not be None. It is required.
|
||||
username: str = Field(None, example='New.User', min_length=3, max_length=100)
|
||||
name: Optional[str] = None
|
||||
email: EmailStr
|
||||
email_verified: Optional[bool] = None
|
||||
enable: Optional[bool] = None
|
||||
enable_from: Optional[datetime] = None
|
||||
enable_to: Optional[datetime] = None
|
||||
super: Optional[bool] = None
|
||||
manager: Optional[bool] = None
|
||||
administrator: Optional[bool] = None
|
||||
verified: Optional[bool] = None
|
||||
|
||||
|
||||
class UserIn(UserBase):
|
||||
password: str = Field(None, example='My Difficult Password!', min_length=10)
|
||||
|
||||
|
||||
class UserOut(UserBase):
|
||||
password_set_on: Optional[datetime] = None
|
||||
password_reset_token: Optional[str] = None
|
||||
password_reset_expire_on: Optional[datetime] = None
|
||||
logged_in_on: Optional[datetime] = None
|
||||
last_activity_on: Optional[datetime] = None
|
||||
created_on: datetime
|
||||
update_on: Optional[datetime] = None
|
||||
|
||||
|
||||
class UserInDB(UserBase):
|
||||
hashed_password: str
|
||||
password_set_on: Optional[datetime] = None
|
||||
password_reset_token: Optional[str] = None
|
||||
password_reset_expire_on: Optional[datetime] = None
|
||||
logged_in_on: Optional[datetime] = None
|
||||
last_activity_on: Optional[datetime] = None
|
||||
|
||||
|
||||
@router.post(
|
||||
"/",
|
||||
response_model=UserOut,
|
||||
summary='Create a new user account',
|
||||
status_code=status.HTTP_201_CREATED
|
||||
)
|
||||
async def create_user(user: UserIn, x_account_id: str = Header(...)):
|
||||
"""
|
||||
Create a new user account
|
||||
"""
|
||||
|
||||
user = {}
|
||||
user['account_id_random'] = x_account_id
|
||||
user['username'] = 'Scott.Idem'
|
||||
user['name'] = 'Scott Idem'
|
||||
user['email'] = 'Scott.Idem@oneskyit.com'
|
||||
|
||||
return user
|
||||
|
||||
|
||||
#@router.patch('/{id_random}', response_model=UserOut, dependencies=[Depends(get_account_header)])
|
||||
#async def update_user(id_random: str, user: UserIn, x_account_id: str = Header(...)):
|
||||
#async def update_user(id_random: str, user: UserIn):
|
||||
@router.patch(
|
||||
'/{id_random}',
|
||||
response_model=UserOut,
|
||||
summary='Update a user account'
|
||||
)
|
||||
async def update_user(id_random: str, user: UserIn, x_account_id: str = Depends(get_account_header)):
|
||||
"""
|
||||
Update a user account
|
||||
"""
|
||||
|
||||
user = {}
|
||||
user['id_random'] = id_random
|
||||
user['account_id_random'] = x_account_id
|
||||
user['username'] = 'Scott.Idem'
|
||||
user['name'] = 'Scott Idem'
|
||||
user['email'] = 'Scott.Idem@oneskyit.com'
|
||||
user['created_on'] = datetime.now()
|
||||
user['super'] = True
|
||||
|
||||
return user
|
||||
|
||||
|
||||
@router.delete('/{id_random}', response_model=bool)
|
||||
async def delete_user(id_random: str, x_account_id: str = Depends(get_account_header)):
|
||||
"""
|
||||
Delete a user account
|
||||
"""
|
||||
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@router.get('/', response_model=List[UserOut])
|
||||
@router.get('/list_all', response_model=List[UserOut])
|
||||
async def list_users():
|
||||
"""
|
||||
Get a list of users
|
||||
"""
|
||||
|
||||
print(settings.APP_NAME)
|
||||
|
||||
users = [{'username': 'test.user.1'}, {'username': 'test.user.2'}, {'username': 'Scott.Idem'}]
|
||||
|
||||
|
||||
print('Getting all users...')
|
||||
|
||||
sql = """
|
||||
SELECT *
|
||||
FROM `user`
|
||||
WHERE id=1
|
||||
"""
|
||||
|
||||
records = sql_select(sql=sql, as_list=True)
|
||||
|
||||
#records = sql_select(table_name='user')
|
||||
|
||||
|
||||
if records:
|
||||
print('Got the user list')
|
||||
return records
|
||||
else:
|
||||
print('No user records found')
|
||||
raise HTTPException(status_code=404)
|
||||
|
||||
|
||||
@router.get('/{username}')
|
||||
async def get_user_username(username: str, x_account_id: str = Header(...)):
|
||||
return {'username': username}
|
||||
|
||||
|
||||
#@router.get('/me')
|
||||
#async def get_user_current():
|
||||
#user_out: UserOut
|
||||
|
||||
#return {'username': 'test.user'}
|
||||
18
app/routers/users_basic.py
Normal file
18
app/routers/users_basic.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/users/", tags=["users"])
|
||||
async def read_users():
|
||||
return [{"username": "Foo"}, {"username": "Bar"}]
|
||||
|
||||
|
||||
@router.get("/users/me", tags=["users"])
|
||||
async def read_user_me():
|
||||
return {"username": "fakecurrentuser"}
|
||||
|
||||
|
||||
@router.get("/users/{username}", tags=["users"])
|
||||
async def read_user(username: str):
|
||||
return {"username": username}
|
||||
83
app/routers/websockets.py
Normal file
83
app/routers/websockets.py
Normal file
@@ -0,0 +1,83 @@
|
||||
from fastapi import APIRouter, FastAPI, WebSocket, WebSocketDisconnect
|
||||
from fastapi.responses import HTMLResponse
|
||||
from typing import List
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
html = """
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Chat</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>WebSocket Chat</h1>
|
||||
<h2>Your ID: <span id="ws-id"></span></h2>
|
||||
<form action="" onsubmit="sendMessage(event)">
|
||||
<input type="text" id="messageText" autocomplete="off"/>
|
||||
<button>Send</button>
|
||||
</form>
|
||||
<ul id='messages'>
|
||||
</ul>
|
||||
<script>
|
||||
var client_id = Date.now()
|
||||
document.querySelector("#ws-id").textContent = client_id;
|
||||
var ws = new WebSocket(`ws://localhost:5005/ws/${client_id}`);
|
||||
ws.onmessage = function(event) {
|
||||
var messages = document.getElementById('messages')
|
||||
var message = document.createElement('li')
|
||||
var content = document.createTextNode(event.data)
|
||||
message.appendChild(content)
|
||||
messages.appendChild(message)
|
||||
};
|
||||
function sendMessage(event) {
|
||||
var input = document.getElementById("messageText")
|
||||
ws.send(input.value)
|
||||
input.value = ''
|
||||
event.preventDefault()
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
|
||||
class ConnectionManager:
|
||||
def __init__(self):
|
||||
self.active_connections: List[WebSocket] = []
|
||||
|
||||
async def connect(self, websocket: WebSocket):
|
||||
await websocket.accept()
|
||||
self.active_connections.append(websocket)
|
||||
|
||||
def disconnect(self, websocket: WebSocket):
|
||||
self.active_connections.remove(websocket)
|
||||
|
||||
async def send_personal_message(self, message: str, websocket: WebSocket):
|
||||
await websocket.send_text(message)
|
||||
|
||||
async def broadcast(self, message: str):
|
||||
for connection in self.active_connections:
|
||||
await connection.send_text(message)
|
||||
|
||||
|
||||
manager = ConnectionManager()
|
||||
|
||||
|
||||
@router.get("/ws_test")
|
||||
async def websocket_root():
|
||||
return HTMLResponse(html)
|
||||
|
||||
|
||||
@router.websocket("/ws/{client_id}")
|
||||
async def websocket_endpoint(websocket: WebSocket, client_id: int):
|
||||
await manager.connect(websocket)
|
||||
try:
|
||||
while True:
|
||||
data = await websocket.receive_text()
|
||||
await manager.send_personal_message(f"You wrote: {data}", websocket)
|
||||
await manager.broadcast(f"Client #{client_id} says: {data}")
|
||||
except WebSocketDisconnect:
|
||||
manager.disconnect(websocket)
|
||||
await manager.broadcast(f"Client #{client_id} left the chat")
|
||||
Reference in New Issue
Block a user