Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Student API #12

Merged
merged 11 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from router import auth, session, group
from router import auth, session, group, student
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi_jwt_auth.exceptions import AuthJWTException
Expand All @@ -16,6 +16,7 @@ def authjwt_exception_handler(request: Request, exc: AuthJWTException):
app.include_router(auth.router)
app.include_router(session.router)
app.include_router(group.router)
app.include_router(student.router)


@app.get("/")
Expand Down
63 changes: 63 additions & 0 deletions app/router/student.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from fastapi import APIRouter, HTTPException, Request
import requests
from settings import settings


router = APIRouter(prefix="/student", tags=["Student"])
student_db_url = settings.db_url + "/student/"


@router.get("/{student_id}")
def index(student_id: str, request: Request):
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved
"""
This API checks if the provided student ID exists in the database.
Additionally, if any other details need to be matched in the database, these details can be sent as query parameters.

Parameters:
student_id (str): The student ID to be checked.

Optional parameters:
Example: birth_date (str), category (str), stream (str).
For extensive list of optional parameters, refer to the DB schema note on Notion.

Returns:
list: Returns student details if the student ID exists in the database (and optional parameters match, if they are provided), 404 otherwise.

Example:
> $BASE_URL/student/1234
returns [{student_data}]

> $BASE_URL/student/{invalid_id}
returns {
"status_code": 404,
"detail": "Student ID does not exist!",
"headers": null
}

> $BASE_URL/student/1234/?stream=PCB&catgeory=Gen
returns [{student_data}]

"""
extra_parameters = dict(request.query_params)
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved
query_params = {"student_id": student_id}
response = requests.get(student_db_url, params=query_params)

if response.status_code == 200:
if len(response.json()) == 0:
return HTTPException(status_code=404, detail="Student ID does not exist!")

elif len(extra_parameters) != 0:
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved
query_params.update(extra_parameters)
response = requests.get(student_db_url, params=query_params)
if response.status_code == 200:
if len(response.json()) == 0:
return HTTPException(
status_code=404, detail="Paramters do not match!"
)
return response.json()
return HTTPException(
status_code=response.status_code, detail=response.errors
)

return response.json()
return HTTPException(status_code=response.status_code, detail=response.errors)