Skip to content

Commit 516b3f1

Browse files
committed
basic kg API service setup w participant and biosample
1 parent 7869b7e commit 516b3f1

File tree

9 files changed

+127
-0
lines changed

9 files changed

+127
-0
lines changed

.gitignore

Whitespace-only changes.

__init__.py

Whitespace-only changes.

dependencies.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from fastapi import Header, HTTPException
2+
3+
4+
async def get_token_header(x_token: str = Header()):
5+
if x_token != "test-token":
6+
raise HTTPException(status_code=400, detail="X-Token header invalid")
7+
8+
9+
async def get_query_token(token: str):
10+
if token != "sooyoung":
11+
raise HTTPException(status_code=400, detail="No Sooyoung token provided")

internal/__init__.py

Whitespace-only changes.

internal/admin.py

Whitespace-only changes.

main.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from fastapi import Depends, FastAPI
2+
3+
from dependencies import get_query_token, get_token_header
4+
# from internal import admin
5+
from routers import biosamples, participants
6+
7+
app = FastAPI(dependencies=[Depends(get_query_token)])
8+
9+
10+
app.include_router(participants.router)
11+
app.include_router(biosamples.router)
12+
# app.include_router(
13+
# admin.router,
14+
# prefix="/admin",
15+
# tags=["admin"],
16+
# dependencies=[Depends(get_token_header)],
17+
# responses={418: {"description": "I'm a teapot"}},
18+
# )
19+
20+
21+
@app.get("/")
22+
async def root():
23+
return {"message": "Knowledge Graph API"}

routers/__init__.py

Whitespace-only changes.

routers/biosamples.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from fastapi import APIRouter, Depends, HTTPException
2+
from pydantic import BaseModel
3+
4+
from dependencies import get_token_header
5+
6+
router = APIRouter(
7+
prefix="/biosamples",
8+
tags=["biosamples"],
9+
dependencies=[Depends(get_token_header)],
10+
responses={404: {"description": "Not found"}},
11+
)
12+
13+
14+
class Biosample(BaseModel):
15+
name: str
16+
17+
18+
fake_biosamples_db = {"first": {"name": "Sample 1"}, "second": {"name": "Sample 2"}}
19+
20+
21+
@router.get("/")
22+
async def read_biosamples():
23+
return fake_biosamples_db
24+
25+
26+
@router.post("/")
27+
async def create_biosample(biosample: Biosample):
28+
if biosample.name in fake_biosamples_db:
29+
raise HTTPException(status_code=404, detail="Biosample already created")
30+
return {"name": fake_biosamples_db[biosample.name]["name"], "biosample_id": biosample.name}
31+
32+
33+
@router.get("/{biosample_id}")
34+
async def read_biosample(biosample_id: str):
35+
if biosample_id not in fake_biosamples_db:
36+
raise HTTPException(status_code=404, detail="Biosample not found")
37+
return {"name": fake_biosamples_db[biosample_id]["name"], "biosample_id": biosample_id}
38+
39+
40+
@router.put(
41+
"/{biosample_id}",
42+
tags=["custom"],
43+
responses={403: {"description": "Operation forbidden"}},
44+
)
45+
async def update_biosample(biosample_id: str):
46+
if biosample_id != "first":
47+
raise HTTPException(
48+
status_code=403, detail="You can only update the biosample: first"
49+
)
50+
return {"biosample_id": biosample_id, "name": "Sample 1"}
51+
52+
53+
@router.delete("/{biosample_id}")
54+
async def delete_biosample(biosample_id: str):
55+
return None

routers/participants.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from typing import Union
2+
3+
from fastapi import APIRouter, Depends, HTTPException
4+
from pydantic import BaseModel
5+
6+
from dependencies import get_token_header
7+
8+
router = APIRouter(
9+
prefix="/participants",
10+
tags=["participants"],
11+
dependencies=[Depends(get_token_header)],
12+
responses={404: {"description": "Not found"}},
13+
)
14+
15+
16+
class Participant(BaseModel):
17+
name: str
18+
id: int
19+
20+
21+
@router.post("/", response_model=Participant)
22+
async def create_participant(participant: Participant):
23+
return {"participant_name": participant.name, "participant_id": participant_id}
24+
25+
26+
@router.get("/{participant_id}")
27+
async def read_participant(participant_id: int, q: Union[str, None] = None):
28+
return {"participant_id": participant_id, "q": q}
29+
30+
31+
@router.put("/{participant_id}")
32+
async def update_participant(participant_id: int, participant: Participant):
33+
return {"participant_name": participant.name, "participant_id": participant_id}
34+
35+
36+
@router.delete("/{participant_id}")
37+
async def delete_participant(participant_id: int):
38+
return None

0 commit comments

Comments
 (0)