From 0e9e9db17a9c50e878e74cb6a19edc2da5250eea Mon Sep 17 00:00:00 2001 From: Krrupa <“sudonthineni@gmail.com@users.noreply.github.com”> Date: Wed, 30 Nov 2022 15:33:29 +0530 Subject: [PATCH 1/9] added student api --- router/student.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 router/student.py diff --git a/router/student.py b/router/student.py new file mode 100644 index 0000000..deaf8db --- /dev/null +++ b/router/student.py @@ -0,0 +1,16 @@ +from fastapi import APIRouter, HTTPException +import requests +from settings import settings + +router = APIRouter(prefix="/student", tags=["Student"]) +student_db_url = settings.db_url + "student/" + +@router.post("/student-id-exists") +def student_id_exists(student_id: str): + """ + API to check if a student ID exists + """ + response = requests.get(student_db_url + student_id) + if response.status_code == 200: + return response.json() + return HTTPException(status_code=response.status_code, detail=response.errors) \ No newline at end of file From 11847d4df7d76a5b504c21dd69e4df1a451adee6 Mon Sep 17 00:00:00 2001 From: Krrupa <“sudonthineni@gmail.com@users.noreply.github.com”> Date: Wed, 30 Nov 2022 15:34:21 +0530 Subject: [PATCH 2/9] added router --- main.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index a5c095c..a1f32ec 100644 --- a/main.py +++ b/main.py @@ -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 @@ -14,4 +14,5 @@ def authjwt_exception_handler(request: Request, exc: AuthJWTException): app.include_router(auth.router) app.include_router(session.router) -app.include_router(group.router) \ No newline at end of file +app.include_router(group.router) +app.include_router(student.router) \ No newline at end of file From bdb589564a5487d195b3bae2910b7943b31059dc Mon Sep 17 00:00:00 2001 From: Krrupa <“sudonthineni@gmail.com@users.noreply.github.com”> Date: Wed, 30 Nov 2022 19:48:50 +0530 Subject: [PATCH 3/9] added query params --- router/student.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/router/student.py b/router/student.py index deaf8db..fd8beab 100644 --- a/router/student.py +++ b/router/student.py @@ -10,7 +10,19 @@ def student_id_exists(student_id: str): """ API to check if a student ID exists """ - response = requests.get(student_db_url + student_id) + query_params = {'student_id':student_id} + response = requests.get(student_db_url, params=query_params) + if response.status_code == 200: + return response.json() + return HTTPException(status_code=response.status_code, detail=response.errors) + +@router.post("/birthdate-matches") +def birthdate_matches(student_id:str, birth_date:str): + """ + API to check if the given birthdate matches to the student ID + """ + query_params = {'student_id':student_id, 'date_of_birth': birth_date} + response = requests.get(student_db_url, params=query_params) if response.status_code == 200: return response.json() return HTTPException(status_code=response.status_code, detail=response.errors) \ No newline at end of file From e1cfd453a580edce1c89955fc1aaa5af4f72b9c2 Mon Sep 17 00:00:00 2001 From: Krrupa <“sudonthineni@gmail.com@users.noreply.github.com”> Date: Wed, 30 Nov 2022 19:53:23 +0530 Subject: [PATCH 4/9] added id check --- router/student.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/router/student.py b/router/student.py index fd8beab..7823efa 100644 --- a/router/student.py +++ b/router/student.py @@ -21,8 +21,11 @@ def birthdate_matches(student_id:str, birth_date:str): """ API to check if the given birthdate matches to the student ID """ - query_params = {'student_id':student_id, 'date_of_birth': birth_date} - response = requests.get(student_db_url, params=query_params) - if response.status_code == 200: - return response.json() - return HTTPException(status_code=response.status_code, detail=response.errors) \ No newline at end of file + student_id_exists = student_id_exists(student_id) + if student_id_exists: + query_params = {'student_id':student_id, 'date_of_birth': birth_date} + response = requests.get(student_db_url, params=query_params) + if response.status_code == 200: + return response.json() + return HTTPException(status_code=response.status_code, detail=response.errors) + return HTTPException(status_code=404, detail="Student ID does not exist!") \ No newline at end of file From 3b05948116ae6958beb0a8b54a90a0a863728785 Mon Sep 17 00:00:00 2001 From: Krrupa <“sudonthineni@gmail.com@users.noreply.github.com”> Date: Thu, 2 Feb 2023 12:50:29 +0530 Subject: [PATCH 5/9] made a universal API --- app/router/student.py | 70 ++++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/app/router/student.py b/app/router/student.py index 551ee8e..c175d7e 100644 --- a/app/router/student.py +++ b/app/router/student.py @@ -1,33 +1,63 @@ -from fastapi import APIRouter, HTTPException +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/" +student_db_url = settings.db_url + "/student/" -@router.post("/student-id-exists") -def student_id_exists(student_id: str): +@router.get("/{student_id}") +def index(student_id: str, request: Request): """ - API to check if a student ID exists + 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) query_params = {"student_id": student_id} response = requests.get(student_db_url, params=query_params) + if response.status_code == 200: - return response.json() - return HTTPException(status_code=response.status_code, detail=response.errors) + if len(response.json()) == 0: + return HTTPException(status_code=404, detail="Student ID does not exist!") + elif len(extra_parameters) != 0: + 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 + ) -@router.post("/birthdate-matches") -def birthdate_matches(student_id: str, birth_date: str): - """ - API to check if the given birthdate matches to the student ID - """ - does_student_id_exist = student_id_exists(student_id) - if does_student_id_exist: - query_params = {"student_id": student_id, "date_of_birth": birth_date} - response = requests.get(student_db_url, params=query_params) - if response.status_code == 200: - return response.json() - return HTTPException(status_code=response.status_code, detail=response.errors) - return HTTPException(status_code=404, detail="Student ID does not exist!") + return response.json() + return HTTPException(status_code=response.status_code, detail=response.errors) From bee8f3d9a6a09f00951b7e8e91d810484a361306 Mon Sep 17 00:00:00 2001 From: Krrupa <“sudonthineni@gmail.com@users.noreply.github.com”> Date: Tue, 7 Feb 2023 16:07:14 +0530 Subject: [PATCH 6/9] added root method --- app/models.py | 24 ++++++++++++++++++++++-- app/router/student.py | 32 +++++++++++++------------------- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/app/models.py b/app/models.py index 7837b0d..caa82f2 100644 --- a/app/models.py +++ b/app/models.py @@ -8,6 +8,26 @@ class User(BaseModel): data: Union[Dict[str, str], None] +class Student(BaseModel): + student_id: Optional[str] = "" + father_name: Optional[str] = "" + father_phone_number: Optional[str] = "" + mother_name: Optional[str] = "" + mother_phone_number: Optional[str] = "" + category: Optional[str] = "" + stream: Optional[str] = "" + physically_handicapped: Optional[str] = "" + family_income: Optional[int] = "" + father_profession: Optional[str] = "" + father_educational_level: Optional[str] = "" + mother_profession: Optional[str] = "" + mother_educational_level: Optional[str] = "" + time_of_device_availability: Optional[str] = "" + has_internet_access: Optional[str] = "" + contact_hours_per_week: Optional[str] = "" + is_dropper: Optional[bool] = "" + + class SessionResponse(BaseModel): id: str session_id: str @@ -27,5 +47,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] = "" diff --git a/app/router/student.py b/app/router/student.py index c175d7e..4030c2d 100644 --- a/app/router/student.py +++ b/app/router/student.py @@ -1,14 +1,24 @@ from fastapi import APIRouter, HTTPException, Request import requests from settings import settings - +from models import Student router = APIRouter(prefix="/student", tags=["Student"]) student_db_url = settings.db_url + "/student/" +@router.get("/") +def index(student: Student): + response = requests.get(student_db_url, params=dict(student)) + 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("/{student_id}") -def index(student_id: str, request: Request): +def get_student(student_id: str): """ 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. @@ -38,26 +48,10 @@ def index(student_id: str, request: Request): returns [{student_data}] """ - extra_parameters = dict(request.query_params) - query_params = {"student_id": student_id} - response = requests.get(student_db_url, params=query_params) + response = requests.get(student_db_url, params={"student_id": student_id}) 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: - 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) From 5941287d8010e93325368957b619a311e644fc8f Mon Sep 17 00:00:00 2001 From: Krrupa <“sudonthineni@gmail.com@users.noreply.github.com”> Date: Wed, 15 Feb 2023 12:40:45 +0530 Subject: [PATCH 7/9] student apis --- app/models.py | 33 +++++++++-------- app/router/student.py | 84 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 83 insertions(+), 34 deletions(-) diff --git a/app/models.py b/app/models.py index caa82f2..888dd20 100644 --- a/app/models.py +++ b/app/models.py @@ -9,23 +9,22 @@ class User(BaseModel): class Student(BaseModel): - student_id: Optional[str] = "" - father_name: Optional[str] = "" - father_phone_number: Optional[str] = "" - mother_name: Optional[str] = "" - mother_phone_number: Optional[str] = "" - category: Optional[str] = "" - stream: Optional[str] = "" - physically_handicapped: Optional[str] = "" - family_income: Optional[int] = "" - father_profession: Optional[str] = "" - father_educational_level: Optional[str] = "" - mother_profession: Optional[str] = "" - mother_educational_level: Optional[str] = "" - time_of_device_availability: Optional[str] = "" - has_internet_access: Optional[str] = "" - contact_hours_per_week: Optional[str] = "" - is_dropper: Optional[bool] = "" + father_name: Optional[str] = None + father_phone_number: Optional[str] = None + mother_name: Optional[str] = None + mother_phone_number: Optional[str] = None + category: Optional[str] = None + stream: Optional[str] = None + physically_handicapped: Optional[str] = None + family_income: Optional[int] = None + father_profession: Optional[str] = None + father_educational_level: Optional[str] = None + mother_profession: Optional[str] = None + mother_educational_level: Optional[str] = None + time_of_device_availability: Optional[str] = None + has_internet_access: Optional[str] = None + contact_hours_per_week: Optional[str] = None + is_dropper: Optional[bool] = None class SessionResponse(BaseModel): diff --git a/app/router/student.py b/app/router/student.py index 4030c2d..42c52ef 100644 --- a/app/router/student.py +++ b/app/router/student.py @@ -1,15 +1,53 @@ -from fastapi import APIRouter, HTTPException, Request +from fastapi import APIRouter, HTTPException import requests from settings import settings from models import Student router = APIRouter(prefix="/student", tags=["Student"]) -student_db_url = settings.db_url + "/student/" +student_db_url = settings.db_url + "/student" @router.get("/") -def index(student: Student): - response = requests.get(student_db_url, params=dict(student)) +def get_students(student: Student, student_id: str = None): + """ + 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/ + body: {"stream":"PCB"} + returns [data_of_all_students_with_stream_PCB] + + > $BASE_URL/student/?student_id=student_id_with_stream_PCM + body: {"stream":"PCB"} + + returns { + "status_code": 404, + "detail": "No student found!", + "headers": null + } + + """ + query_params = {} + student_attributes = student.dict(exclude_unset=True) + for key in student_attributes.keys(): + query_params[key] = student_attributes[key] + if student_id: + 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="No student found!") @@ -17,41 +55,53 @@ def index(student: Student): return HTTPException(status_code=response.status_code, detail=response.errors) -@router.get("/{student_id}") -def get_student(student_id: str): +@router.get("/verify") +def verify_student(student_id: str, student: Student): """ - 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. + 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. - Optional parameters: + 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: - list: Returns student details if the student ID exists in the database (and optional parameters match, if they are provided), 404 otherwise. + bool: True if the details match against the student ID, otherwise False Example: - > $BASE_URL/student/1234 - returns [{student_data}] + > $BASE_URL/student/verify/?student_id=1234 + body: {"stream":"PCB"} + returns True - > $BASE_URL/student/{invalid_id} + > $BASE_URL/student/verify/?student_id={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}] + > $BASE_URL/student/1234/?student_id=1234 + body: {"stream":"PCM"} + returns { + "status_code": 404, + "detail": "Student ID does not exist!", + "headers": null + } """ - response = requests.get(student_db_url, params={"student_id": student_id}) + query_params = {} + student_attributes = student.dict(exclude_unset=True) + for key in student_attributes.keys(): + query_params[key] = student_attributes[key] + if student_id: + 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 response.json() + return True return HTTPException(status_code=response.status_code, detail=response.errors) From d20cd0dcdd4a0d22615b4b1dc1c133de439739c4 Mon Sep 17 00:00:00 2001 From: Krrupa <“sudonthineni@gmail.com@users.noreply.github.com”> Date: Wed, 15 Feb 2023 14:38:44 +0530 Subject: [PATCH 8/9] removed model --- app/models.py | 19 --------------- app/router/student.py | 57 ++++++++++++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/app/models.py b/app/models.py index 888dd20..3944daa 100644 --- a/app/models.py +++ b/app/models.py @@ -8,25 +8,6 @@ class User(BaseModel): data: Union[Dict[str, str], None] -class Student(BaseModel): - father_name: Optional[str] = None - father_phone_number: Optional[str] = None - mother_name: Optional[str] = None - mother_phone_number: Optional[str] = None - category: Optional[str] = None - stream: Optional[str] = None - physically_handicapped: Optional[str] = None - family_income: Optional[int] = None - father_profession: Optional[str] = None - father_educational_level: Optional[str] = None - mother_profession: Optional[str] = None - mother_educational_level: Optional[str] = None - time_of_device_availability: Optional[str] = None - has_internet_access: Optional[str] = None - contact_hours_per_week: Optional[str] = None - is_dropper: Optional[bool] = None - - class SessionResponse(BaseModel): id: str session_id: str diff --git a/app/router/student.py b/app/router/student.py index 42c52ef..dd6a638 100644 --- a/app/router/student.py +++ b/app/router/student.py @@ -1,14 +1,33 @@ -from fastapi import APIRouter, HTTPException +from fastapi import APIRouter, HTTPException, Request, Query import requests from settings import settings -from models import Student 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(student: Student, student_id: str = None): +def get_students(request: Request): """ This API returns a student or a list of students who match the criteria(s) given in the request. @@ -26,12 +45,10 @@ def get_students(student: Student, student_id: str = None): > $BASE_URL/student/?student_id=1234 returns [{student_data}] - > $BASE_URL/student/ - body: {"stream":"PCB"} + > $BASE_URL/student/?stream=PCB returns [data_of_all_students_with_stream_PCB] - > $BASE_URL/student/?student_id=student_id_with_stream_PCM - body: {"stream":"PCB"} + > $BASE_URL/student/?student_id=student_id_with_stream_PCM&stream=PCB returns { "status_code": 404, @@ -41,11 +58,12 @@ def get_students(student: Student, student_id: str = None): """ query_params = {} - student_attributes = student.dict(exclude_unset=True) - for key in student_attributes.keys(): - query_params[key] = student_attributes[key] - if student_id: - query_params["student_id"] = student_id + 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: @@ -56,7 +74,7 @@ def get_students(student: Student, student_id: str = None): @router.get("/verify") -def verify_student(student_id: str, student: Student): +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. @@ -92,14 +110,15 @@ def verify_student(student_id: str, student: Student): """ query_params = {} - student_attributes = student.dict(exclude_unset=True) - for key in student_attributes.keys(): - query_params[key] = student_attributes[key] - if student_id: - query_params["student_id"] = student_id + 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!") From 05bc7e1732a9eb77148971d1720382c9ebb32331 Mon Sep 17 00:00:00 2001 From: Krrupa <“sudonthineni@gmail.com@users.noreply.github.com”> Date: Wed, 15 Feb 2023 14:40:11 +0530 Subject: [PATCH 9/9] comment --- app/router/student.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/router/student.py b/app/router/student.py index dd6a638..5359743 100644 --- a/app/router/student.py +++ b/app/router/student.py @@ -90,7 +90,9 @@ async def verify_student(request: Request, student_id: str): Example: > $BASE_URL/student/verify/?student_id=1234 - body: {"stream":"PCB"} + returns True + + > $BASE_URL/student/verify/?student_id=1234&stream=PCB returns True > $BASE_URL/student/verify/?student_id={invalid_id} @@ -100,8 +102,7 @@ async def verify_student(request: Request, student_id: str): "headers": null } - > $BASE_URL/student/1234/?student_id=1234 - body: {"stream":"PCM"} + > $BASE_URL/student/1234/?student_id=1234&stream=PCM returns { "status_code": 404, "detail": "Student ID does not exist!",