Skip to content

Commit b8626e9

Browse files
authored
Merge pull request #162 from nanotaboada/feature/docstrings
chore: add API docs section
2 parents 685dce6 + 5fb9e52 commit b8626e9

File tree

8 files changed

+63
-35
lines changed

8 files changed

+63
-35
lines changed

.vscode/launch.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"version": "0.2.0",
3-
"configurations": [
4-
{
5-
"name": "Python Debugger: FastAPI",
6-
"type": "debugpy",
7-
"request": "launch",
8-
"module": "uvicorn",
9-
"args": ["main:app", "--reload", "--port", "9000"],
10-
"jinja": true
11-
}
12-
]
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Python: Launch FastAPI (Debug)",
6+
"type": "debugpy",
7+
"request": "launch",
8+
"module": "uvicorn",
9+
"args": ["main:app", "--reload", "--port", "9000"],
10+
"jinja": true
11+
}
12+
]
1313
}

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ pip install --requirement requirements.txt
3535
uvicorn main:app --reload --port 9000
3636
```
3737

38+
## Documentation
39+
40+
```console
41+
http://localhost:9000/docs
42+
```
43+
44+
![API Documentation](python-samples-fastapi-restful-docs.png)
45+
3846
## Credits
3947

4048
The solution has been coded using [Visual Studio Code](https://code.visualstudio.com/) with the official [Python](https://marketplace.visualstudio.com/items?itemName=ms-python.python) extension.

main.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@ async def lifespan(app: FastAPI):
1414
FastAPICache.init(InMemoryBackend())
1515
yield
1616

17-
app = FastAPI(lifespan=lifespan)
17+
app = FastAPI(lifespan=lifespan,
18+
title="python-samples-fastapi-restful",
19+
description="🧪 Proof of Concept for a RESTful API made with Python 3 and FastAPI",
20+
version="1.0.0",)
21+
1822
app.include_router(player_route.api_router)

models/player_model.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ class PlayerModel(MainModel):
2828
Pydantic model representing a football Player.
2929
3030
Attributes:
31-
id (int): The unique identifier for the player.
32-
first_name (str): The first name of the player.
33-
middle_name (Optional[str]): The middle name of the player, if any.
34-
last_name (str): The last name of the player.
35-
date_of_birth (Optional[str]): The date of birth of the player, if provided.
36-
squad_number (int): The unique squad number assigned to the player.
37-
position (str): The playing position of the player.
38-
abbr_position (Optional[str]): The abbreviated form of the player's position, if any.
39-
team (Optional[str]): The team to which the player belongs, if any.
31+
id (int): The unique identifier for the Player.
32+
first_name (str): The first name of the Player.
33+
middle_name (Optional[str]): The middle name of the Player, if any.
34+
last_name (str): The last name of the Player.
35+
date_of_birth (Optional[str]): The date of birth of the Player, if provided.
36+
squad_number (int): The unique squad number assigned to the Player.
37+
position (str): The playing position of the Player.
38+
abbr_position (Optional[str]): The abbreviated form of the Player's position, if any.
39+
team (Optional[str]): The team to which the Player belongs, if any.
4040
league (Optional[str]): The league where the team plays, if any.
41-
starting11 (Optional[bool]): Indicates if the player is in the starting 11, if provided.
41+
starting11 (Optional[bool]): Indicates if the Player is in the starting 11, if provided.
4242
"""
4343
id: int
4444
first_name: str
312 KB
Loading

routes/player_route.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def get_orm_session():
3737
@api_router.post(
3838
"/players/",
3939
status_code=status.HTTP_201_CREATED,
40-
summary="Creates a new Player"
40+
summary="Creates a new Player",
41+
tags=["Players"]
4142
)
4243
def post(
4344
player_model: PlayerModel = Body(...),
@@ -69,7 +70,8 @@ def post(
6970
"/players/",
7071
response_model=List[PlayerModel],
7172
status_code=status.HTTP_200_OK,
72-
summary="Retrieves a collection of Players"
73+
summary="Retrieves a collection of Players",
74+
tags=["Players"]
7375
)
7476
@cache(expire=CACHING_TIME_IN_SECONDS)
7577
def get_all(
@@ -93,7 +95,8 @@ def get_all(
9395
"/players/{player_id}",
9496
response_model=PlayerModel,
9597
status_code=status.HTTP_200_OK,
96-
summary="Retrieves a Player by its Id"
98+
summary="Retrieves a Player by its Id",
99+
tags=["Players"]
97100
)
98101
@cache(expire=CACHING_TIME_IN_SECONDS)
99102
def get_by_id(
@@ -125,7 +128,8 @@ def get_by_id(
125128
"/players/squadnumber/{squad_number}",
126129
response_model=PlayerModel,
127130
status_code=status.HTTP_200_OK,
128-
summary="Retrieves a Player by its Squad Number"
131+
summary="Retrieves a Player by its Squad Number",
132+
tags=["Players"]
129133
)
130134
@cache(expire=CACHING_TIME_IN_SECONDS)
131135
def get_by_squad_number(
@@ -158,7 +162,8 @@ def get_by_squad_number(
158162
@api_router.put(
159163
"/players/{player_id}",
160164
status_code=status.HTTP_204_NO_CONTENT,
161-
summary="Updates an existing Player"
165+
summary="Updates an existing Player",
166+
tags=["Players"]
162167
)
163168
def put(
164169
player_id: int = Path(..., title="The ID of the Player"),
@@ -174,7 +179,7 @@ def put(
174179
orm_session (Session): The SQLAlchemy ORM session.
175180
176181
Raises:
177-
HTTPException: HTTP 404 Not Found error if the player with the specified ID does not exist.
182+
HTTPException: HTTP 404 Not Found error if the Player with the specified ID does not exist.
178183
"""
179184
player = player_service.retrieve_by_id(orm_session, player_id)
180185

@@ -191,7 +196,8 @@ def put(
191196
@api_router.delete(
192197
"/players/{player_id}",
193198
status_code=status.HTTP_204_NO_CONTENT,
194-
summary="Deletes an existing Player"
199+
summary="Deletes an existing Player",
200+
tags=["Players"]
195201
)
196202
def delete(
197203
player_id: int = Path(..., title="The ID of the Player"),

services/player_service.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313

1414
def create(orm_session: Session, player_model: PlayerModel):
15-
"""Creates a new Player in the database.
15+
"""
16+
Creates a new Player in the database.
1617
1718
Args:
1819
orm_session: The SQLAlchemy ORM session instance.
@@ -36,7 +37,8 @@ def create(orm_session: Session, player_model: PlayerModel):
3637

3738

3839
def retrieve_all(orm_session: Session):
39-
"""Retrieves all the players from the database.
40+
"""
41+
Retrieves all the players from the database.
4042
4143
Args:
4244
orm_session: The SQLAlchemy ORM session instance.
@@ -51,7 +53,8 @@ def retrieve_all(orm_session: Session):
5153

5254

5355
def retrieve_by_id(orm_session: Session, player_id: int):
54-
"""Retrieves a Player by its ID from the database.
56+
"""
57+
Retrieves a Player by its ID from the database.
5558
5659
Args:
5760
orm_session: The SQLAlchemy ORM session instance.
@@ -65,7 +68,8 @@ def retrieve_by_id(orm_session: Session, player_id: int):
6568

6669

6770
def retrieve_by_squad_number(orm_session: Session, squad_number: int):
68-
"""Retrieves a Player by its Squad Number from the database.
71+
"""
72+
Retrieves a Player by its Squad Number from the database.
6973
7074
Args:
7175
orm_session: The SQLAlchemy ORM session instance.
@@ -82,7 +86,8 @@ def retrieve_by_squad_number(orm_session: Session, squad_number: int):
8286

8387

8488
def update(orm_session: Session, player_model: PlayerModel):
85-
"""Updates (entirely) an existing Player in the database.
89+
"""
90+
Updates (entirely) an existing Player in the database.
8691
8792
Args:
8893
orm_session: The SQLAlchemy ORM session instance.
@@ -115,7 +120,8 @@ def update(orm_session: Session, player_model: PlayerModel):
115120

116121

117122
def delete(orm_session: Session, player_id: int):
118-
"""Deletes an existing Player from the database.
123+
"""
124+
Deletes an existing Player from the database.
119125
120126
Args:
121127
orm_session: The SQLAlchemy ORM session instance.

tests/player_stub.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
class Player:
2+
"""
3+
Test stub representing a Player.
4+
"""
5+
26
def __init__(
37
self,
48
id=None,

0 commit comments

Comments
 (0)