Skip to content

Commit

Permalink
Merge pull request #12 from avantifellows/user
Browse files Browse the repository at this point in the history
Student API
  • Loading branch information
sudheshna-donthineni authored Feb 23, 2023
2 parents f3999dc + 05bc7e1 commit 66b38be
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 1 deletion.
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
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)

0 comments on commit 66b38be

Please sign in to comment.