Module main_api_startpoint

FastAPI-based module for handling meal tracking API endpoints.

This module provides the core functionality for managing user authentication, registration, and meal tracking operations such as adding, editing, and deleting meals. It uses Pydantic models to validate incoming requests and interacts with a MySQL database using custom repositories.

Module includes: - Authentication (token validation) - User registration and login - Meal operations (add, edit, delete, and retrieve meals) - Fetching available meal types

Dependencies

  • FastAPI
  • Pydantic
  • Custom imports (databaseWrapper, logger, models like MealItem and GetMealsItem)

Usage example:

# Import the module
from src.utils.databaseWrapper import DatabaseWrapper
from src.utils.logger import Logger

# Initialize components
db_wrapper = DatabaseWrapper()
logger = Logger()

# Run the FastAPI app
uvicorn.run(app, host="0.0.0.0", port=8000)

Functions

async def add_meal(meal_item: MealItemPydantic, response: starlette.responses.Response)

POST /v1/addMeal endpoint. Adds a new meal entry.

def convert_pydantic_to_authentication_item(auth_pydantic: AuthenticationItemPydantic)

Converts a Pydantic AuthenticationItem model to the internal AuthenticationItem.

def convert_pydantic_to_credentials_item(credentials_pydantic: CredentialsItemPydantic)

Converts a Pydantic CredentialsItem model to the internal CredentialsItem.

def convert_pydantic_to_delete_meal_item(delete_meal_pydantic: DeleteMealItemPydantic)

Converts a Pydantic DeleteMealItem model to the internal DeleteMealItem.

def convert_pydantic_to_get_meals_item(get_meals_pydantic: GetMealsItemPydantic)

Converts a Pydantic GetMealsItem model to the internal GetMealsItem.

def convert_pydantic_to_meal_item(meal_pydantic: MealItemPydantic)

Converts a Pydantic MealItem model to the internal MealItem.

async def delete_meal(delete_meal_item: DeleteMealItemPydantic, response: starlette.responses.Response)

POST /v1/deleteMeal endpoint. Deletes a meal entry.

async def edit_meal(meal_item: MealItemPydantic, response: starlette.responses.Response)

POST /v1/editMeal endpoint. Edits an existing meal entry.

async def get_meal_types(credentials: CredentialsItemPydantic, response: starlette.responses.Response)

POST /v1/getMealTypes endpoint. Fetches all available meal types.

async def get_meals(get_meals_item: GetMealsItemPydantic, response: starlette.responses.Response)

POST /v1/getMeals endpoint. Fetches the meal entries for a user on a specific day.

async def login(credentials_item: CredentialsItemPydantic, response: starlette.responses.Response)

POST /v1/login endpoint. Verifies user login credentials.

def login_local(credentials_item: CredentialsItemPydantic, response: starlette.responses.Response, attempted_update=False)

Handles local login logic.

async def register(credentials_item: CredentialsItemPydantic, response: starlette.responses.Response)

POST /v1/register endpoint. Registers a new user if token validation passes.

async def root_get()

GET / endpoint. Returns a simple message with the project repository URL. Logs the request and sends a 200 OK status with a message.

async def root_post()

POST / endpoint. Returns a simple message with the project repository URL. Logs the request and sends a 200 OK status with a message.

async def token(authentication_item: AuthenticationItemPydantic, response: starlette.responses.Response)

POST /v1/token endpoint. Validates the provided token and returns a response.

Classes

class AuthenticationItemPydantic (**data: Any)

Represents authentication data for validation.

Json model of a valid AuthenticationItem to send to the API: { "token": "" }

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Expand source code
class AuthenticationItemPydantic(BaseModel):
    """
    Represents authentication data for validation.

    Json model of a valid AuthenticationItem to send to the API:
    {
        "token": "<your_actual_token_here>"
    }
    """
    token: str

Ancestors

  • pydantic.main.BaseModel

Class variables

var model_computed_fields
var model_config
var model_fields
var token : str
class CredentialsItemPydantic (**data: Any)

Represents credentials for a user.

Json model of a valid CredentialsItem to send to the API: { "token": "", "userName": "", "hashedPassword": "" }

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Expand source code
class CredentialsItemPydantic(BaseModel):
    """
    Represents credentials for a user.

    Json model of a valid CredentialsItem to send to the API:
    {
        "token": "<your_actual_token_here>",
        "userName": "<your_actual_username_here>",
        "hashedPassword": "<your_actual_hashed_password_here>"
    }
    """
    token: str
    userName: str
    hashedPassword: str

Ancestors

  • pydantic.main.BaseModel

Class variables

var hashedPassword : str
var model_computed_fields
var model_config
var model_fields
var token : str
var userName : str
class DeleteMealItemPydantic (**data: Any)

Represents data required to delete a meal.

Json model of a valid DeleteMealItem to send to the API: { "credentials": { "token": "", "userName": "", "hashedPassword": "" }, "year": 2024, "month": 10, "day": 12, "mealType": "Dinner" }

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Expand source code
class DeleteMealItemPydantic(BaseModel):
    """
    Represents data required to delete a meal.

    Json model of a valid DeleteMealItem to send to the API:
    {
        "credentials": {
            "token": "<your_actual_token_here>",
            "userName": "<your_actual_username_here>",
            "hashedPassword": "<your_actual_hashed_password_here>"
        },
        "year": 2024,
        "month": 10,
        "day": 12,
        "mealType": "Dinner"
    }
    """
    credentials: CredentialsItemPydantic
    year: int
    month: int
    day: int
    mealType: str

Ancestors

  • pydantic.main.BaseModel

Class variables

var credentialsCredentialsItemPydantic
var day : int
var mealType : str
var model_computed_fields
var model_config
var model_fields
var month : int
var year : int
class GetMealsItemPydantic (**data: Any)

Represents the details for fetching meals for a user on a specific day.

Json model of a valid GetMealsItem to send to the API: { "credentials": { "token": "", "userName": "", "hashedPassword": "" }, "year": 2024, "month": 10, "day": 12 }

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Expand source code
class GetMealsItemPydantic(BaseModel):
    """
    Represents the details for fetching meals for a user on a specific day.

    Json model of a valid GetMealsItem to send to the API:
    {
        "credentials": {
            "token": "<your_actual_token_here>",
            "userName": "<your_actual_username_here>",
            "hashedPassword": "<your_actual_hashed_password_here>"
        },
        "year": 2024,
        "month": 10,
        "day": 12
    }
    """
    credentials: CredentialsItemPydantic
    year: int
    month: int
    day: int

Ancestors

  • pydantic.main.BaseModel

Class variables

var credentialsCredentialsItemPydantic
var day : int
var model_computed_fields
var model_config
var model_fields
var month : int
var year : int
class MealItemPydantic (**data: Any)

Represents a meal entry for a specific day and meal type.

Json model of a valid MealItem to send to the API: { "credentials": { "token": "", "userName": "", "hashedPassword": "" }, "year": 2024, "month": 10, "day": 12, "mealType": "Lunch", "fat_level": 1, "sugar_level": 2 }

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Expand source code
class MealItemPydantic(BaseModel):
    """
    Represents a meal entry for a specific day and meal type.

    Json model of a valid MealItem to send to the API:
    {
        "credentials": {
            "token": "<your_actual_token_here>",
            "userName": "<your_actual_username_here>",
            "hashedPassword": "<your_actual_hashed_password_here>"
        },
        "year": 2024,
        "month": 10,
        "day": 12,
        "mealType": "Lunch",
        "fat_level": 1,
        "sugar_level": 2
    }
    """
    credentials: CredentialsItemPydantic
    year: int
    month: int
    day: int
    mealType: str
    fat_level: int  # 0: Low, 1: Medium, 2: High
    sugar_level: int  # 0: Low, 1: Medium, 2: High

Ancestors

  • pydantic.main.BaseModel

Class variables

var credentialsCredentialsItemPydantic
var day : int
var fat_level : int
var mealType : str
var model_computed_fields
var model_config
var model_fields
var month : int
var sugar_level : int
var year : int