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 all 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
4 changes: 2 additions & 2 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ class GroupResponse(BaseModel):
name: str
group_input_schema: Optional[Dict] = {}
group_locale: Optional[str] = ""
group_locale_data = Optional[Dict] = {}
auth_type = Optional[str] = ""
group_locale_data: Optional[Dict] = {}
auth_type: Optional[str] = ""
127 changes: 127 additions & 0 deletions app/router/student.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
from fastapi import APIRouter, HTTPException, Request, Query
import requests
from settings import settings

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

QUERY_PARAMS = [
"student_id",
"father_name",
"father_phone_number",
"mother_name",
"mother_phone_number",
"category",
"stream",
"physically_handicapped",
"family_income",
"father_profession",
"father_educational_level",
"mother_profession",
"mother_educational_level",
"time_of_device_availability",
"has_internet_access",
"contact_hours_per_week",
"is_dropper",
]


@router.get("/")
def get_students(request: Request):
"""
This API returns a student or a list of students who match the criteria(s) given in the request.

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

Returns:
list: student data if student(s) whose details match, otherwise 404

Example:
> $BASE_URL/student/
returns [data_of_all_students]

> $BASE_URL/student/?student_id=1234
returns [{student_data}]

> $BASE_URL/student/?stream=PCB
returns [data_of_all_students_with_stream_PCB]

> $BASE_URL/student/?student_id=student_id_with_stream_PCM&stream=PCB

returns {
"status_code": 404,
"detail": "No student found!",
"headers": null
}

"""
query_params = {}
for key in request.query_params.keys():
if key not in QUERY_PARAMS:
return HTTPException(
status_code=400, detail="Query Parameter {} is not allowed!".format(key)
)
query_params[key] = request.query_params[key]

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="No student found!")
return response.json()
return HTTPException(status_code=response.status_code, detail=response.errors)


@router.get("/verify")
async def verify_student(request: Request, student_id: str):
"""
This API checks if the provided student ID and the additional details match in the database.

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

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

Returns:
bool: True if the details match against the student ID, otherwise False

Example:
> $BASE_URL/student/verify/?student_id=1234
returns True

> $BASE_URL/student/verify/?student_id=1234&stream=PCB
returns True

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

> $BASE_URL/student/1234/?student_id=1234&stream=PCM
returns {
"status_code": 404,
"detail": "Student ID does not exist!",
"headers": null
}

"""
query_params = {}
for key in request.query_params.keys():
if key not in QUERY_PARAMS:
return HTTPException(
status_code=400, detail="Query Parameter {} is not allowed!".format(key)
)
query_params[key] = request.query_params[key]
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!")
return True
return HTTPException(status_code=response.status_code, detail=response.errors)