Skip to content
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
41 changes: 41 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: CI Pipeline for FastAPI

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
lint-and-test:
runs-on: ubuntu-latest
steps:
# Step 1: Checkout Code
- name: Checkout Code
uses: actions/checkout@v4

# Step 2: Setup Python
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.9'

# Step 3: Install Dependencies
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt

# Step 4: Run Pylint
- name: Run Pylint
run: |
pip install pylint
pylint app/ tests/

# Step 5: Run Tests
- name: Run Tests
run: |
pip install pytest
pytest tests/
150 changes: 148 additions & 2 deletions app/clients/router.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,161 @@
from fastapi import APIRouter
from fastapi.responses import HTMLResponse
"""
This module defines API routes for client-related operations such as creating,
updating, retrieving, and deleting client data. It also includes prediction
functionality.
"""

from fastapi import APIRouter, HTTPException
from pydantic import BaseModel

from app.clients.service.logic import interpret_and_calculate
from app.clients.service.logic import create_client_data
from app.clients.service.logic import get_client_data
from app.clients.service.logic import update_client_data, delete_client_data
from app.clients.schema import PredictionInput


class ClientData(BaseModel):
"""
Represents the schema for client data with various attributes such as
demographics, work experience, and other personal information.
"""
# pylint: disable=duplicate-code

age: int
gender: int
work_experience: int
canada_workex: int
dep_num: int
canada_born: int
citizen_status: int
level_of_schooling: int
fluent_english: int
reading_english_scale: int
speaking_english_scale: int
writing_english_scale: int
numeracy_scale: int
computer_scale: int
transportation_bool: int
caregiver_bool: int
housing: int
income_source: int
felony_bool: int
attending_school: int
currently_employed: int
substance_use: int
time_unemployed: int
need_mental_health_support_bool: int
employment_assistance: int
life_stabilization: int
retention_services: int
specialized_services: int
employment_related_financial_supports: int
employer_financial_supports: int
enhanced_referrals: int
success_rate: int


router = APIRouter(prefix="/clients", tags=["clients"])


@router.post("/predictions")
async def predict(data: PredictionInput):
"""
Perform predictions based on the provided input data.

Args:
data (PredictionInput): The input data for the prediction model.

Returns:
dict: The calculated prediction results.
"""
print("HERE")
print(data.model_dump())
return interpret_and_calculate(data.model_dump())


@router.post("/", response_model=ClientData)
async def create_client(client_data: ClientData):
"""
Create a new client record in the system.

Args:
client_data (ClientData): The data for the new client to be created.

Returns:
ClientData: The newly created client data.

Raises:
HTTPException: Returns a 400 error if the client creation fails.
"""
created_client = create_client_data(client_data.dict())
if created_client:
return created_client
raise HTTPException(status_code=400, detail="Unable to create client")


@router.get("/", response_model=ClientData)
async def get_client(age: int, gender: int, work_experience: int):
"""
Retrieve a single client's data by their unique attributes.

Args:
age (int): The age of the client.
gender (int): The gender of the client.
work_experience (int): The work experience of the client.

Returns:
ClientData: The client data corresponding to the specified attributes.

Raises:
HTTPException: Returns a 404 error if no client is found with the
provided attributes.
"""
client = get_client_data(age, gender, work_experience)
if client:
return client
raise HTTPException(status_code=404, detail="Client not found")


@router.put("/", response_model=ClientData)
async def update_client(client_update: ClientData):
"""
Update an existing client's data.

Args:
client_update (ClientData): A model containing the client's updated
data.

Returns:
ClientData: The updated client data.

Raises:
HTTPException: Returns a 404 error if no client is found with the
provided attributes, or if the update fails.
"""
updated_client = update_client_data(client_update.dict())
if updated_client:
return updated_client
raise HTTPException(status_code=404, detail="Unable to update client")


@router.delete("/", response_model=dict)
async def delete_client(age: int, gender: int, work_experience: int):
"""
Delete a client's data from the system.

Args:
age (int): The age of the client.
gender (int): The gender of the client.
work_experience (int): The work experience of the client.

Returns:
dict: A message indicating successful deletion.

Raises:
HTTPException: Returns a 404 error if no client is found with the
provided attributes, or if the deletion fails.
"""
if delete_client_data(age, gender, work_experience):
return {"message": "Client deleted successfully"}
raise HTTPException(status_code=404, detail="Client not found")
9 changes: 9 additions & 0 deletions app/clients/schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
"""
This module defines Pydantic models used for client-related API requests and data validation.
"""
from pydantic import BaseModel


class PredictionInput(BaseModel):
"""
Represents the input schema for client prediction requests.
Includes demographic, work, and personal data for validation.
"""
age: int
gender: str
work_experience: int
Expand All @@ -25,3 +33,4 @@ class PredictionInput(BaseModel):
substance_use: str
time_unemployed: int
need_mental_health_support_bool: str
# pylint: disable=duplicate-code
Loading