Skip to content

Commit

Permalink
fix python lint issue
Browse files Browse the repository at this point in the history
Signed-off-by: Lu Ken <[email protected]>
  • Loading branch information
kenplusplus committed Dec 27, 2024
1 parent 0c20703 commit afe61a0
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 7 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
```shell
export PYTHONPATH=<repo_dir>/src
uvicorn gentrade-server.main:app --reload
```
or
python -m gentrade-server.main
```
File renamed without changes.
12 changes: 9 additions & 3 deletions src/gentrade-server/auth.py → src/gentrade_server/auth.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
"""
Authorization module
"""
from fastapi import Security, HTTPException, status
from fastapi.security import APIKeyHeader
from .db import check_api_key, get_user_from_api_key

api_key_header = APIKeyHeader(name="X-API-Key")

def get_user(api_key_header: str = Security(api_key_header)):
if check_api_key(api_key_header):
user = get_user_from_api_key(api_key_header)
def get_user(header: str = Security(api_key_header)):
"""
Get user from the API key
"""
if check_api_key(header):
user = get_user_from_api_key(header)
return user
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
Expand Down
9 changes: 9 additions & 0 deletions src/gentrade-server/db.py → src/gentrade_server/db.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""
Database
"""
api_keys = {
"e54d4431-5dab-474e-b71a-0db1fcb9e659": "7oDYjo3d9r58EJKYi5x4E8",
"5f0c7127-3be9-4488-b801-c7b6415b45e9": "mUP7PpTHmFAkxcQLWKMY8t"
Expand All @@ -13,7 +16,13 @@
}

def check_api_key(api_key: str):
"""
Check whether API key valid
"""
return api_key in api_keys

def get_user_from_api_key(api_key: str):
"""
Get the user from the given API key
"""
return users[api_keys[api_key]]
5 changes: 5 additions & 0 deletions src/gentrade-server/main.py → src/gentrade_server/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import uvicorn

from fastapi import FastAPI, Depends
from .routers import secure, public
from .auth import get_user
Expand All @@ -13,3 +15,6 @@
prefix="/api/v1/secure",
dependencies=[Depends(get_user)]
)

if __name__ == '__main__':
uvicorn.run(app, host="0.0.0.0", port=8000)
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
"""
Public result API interfaces
"""
from fastapi import APIRouter


router = APIRouter()

@router.get("/")
async def get_testroute():
return "OK"
"""
Test public interface
"""
return "OK"
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
"""
Secure API interface
"""
from fastapi import APIRouter, Depends
from ..auth import get_user

router = APIRouter()

@router.get("/")
async def get_testroute(user: dict = Depends(get_user)):
return user
"""
Test secure interface
"""
return user

0 comments on commit afe61a0

Please sign in to comment.