-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Defined Schemas - Data Format for API Requests/Response
- Loading branch information
1 parent
6612a0f
commit 293561a
Showing
9 changed files
with
205 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,66 @@ | ||
"""Schemas using Pydantic for Request and Response Model""" | ||
"""Schemas: Define data representations for API requests/responses. | ||
This separation improves data validation and control over what data | ||
is exposed through the API. | ||
""" | ||
|
||
from typing import List, Dict, Any | ||
from pydantic import BaseModel, HttpUrl | ||
import datetime as _dt | ||
import pydantic as _pydantic | ||
|
||
class _PostBase(_pydantic.BaseModel): | ||
"""Base schema for 'Post' data, including title and content.""" | ||
title:str | ||
content:str | ||
|
||
# Define a response model for the search results | ||
class SearchResult(BaseModel): | ||
"""Response Model for the Search Result""" | ||
class CreatePost(_PostBase): | ||
"""Schema for creating a new Post. | ||
Requires title and content, used for data validation and creation. | ||
""" | ||
|
||
title: str | ||
pdf_link: HttpUrl | ||
class Post(_PostBase): | ||
"""Schema for representing a Post including additional details. | ||
Inherits from '_PostBase' and adds fields like ID, owner ID, | ||
creation and update dates. | ||
""" | ||
id:int | ||
owner_id:int | ||
date_created: _dt.datetime | ||
date_last_updated: _dt.datetime | ||
|
||
class Config: | ||
orm_mode = True | ||
|
||
class _UserBase(_pydantic.BaseModel): | ||
"""Base schema for User data, including email.""" | ||
email: str | ||
|
||
class UserCreate(_UserBase): | ||
"""Schema for creating a new User. | ||
Requires email and password, used for data validation and creation. | ||
""" | ||
password:str | ||
|
||
# Define a response model for the recommendations | ||
class Recommendation(BaseModel): | ||
"""Response Model for the Recommendation""" | ||
class User(_UserBase): | ||
"""Schema for representing a User including additional details and posts. | ||
Inherits from _UserBase and adds fields like ID, active status, and a list of posts. | ||
""" | ||
id:int | ||
is_active:bool | ||
posts: list[Post] = [] | ||
|
||
class Config: | ||
orm_mode = True | ||
|
||
class _RecommendedPaperBase(_pydantic.BaseModel): | ||
"""Base schema for RecommendedPaper data, including basic paper details.""" | ||
id: str | ||
title: str | ||
published_date: _dt.datetime | ||
pdf_link: _pydantic.HttpUrl | ||
summary: str | ||
pdf_text: str | ||
|
||
items: List[Dict[str, Any]] | ||
class RecommendedPaper(_RecommendedPaperBase): | ||
"""Schema for representing a RecommendedPaper with nested results. | ||
Inherits from _RecommendedPaperBase and can include a list of nested results (other papers). | ||
""" | ||
results: list[_RecommendedPaperBase] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.