Real-time task manager for my FastAPI course https://stepik.org/course/179694/
Task Manager FastAPI is a simple task management API built using FastAPI. It provides basic CRUD (Create, Read, Update, Delete) operations for tasks and includes real-time updates for task status changes via WebSocket.
This task manager is deliberately made not completely asynchronous in order for students to complete the Final project on their own. It also uses at least 4 deprecated methods for the same purposes. To improve, it is necessary to make asynchronous work with databases and update the endpoints code.
- Features
- Project Structure
- Getting Started
- Usage
- Testing with SwaggerUI
- Testing with pytest
- Potential Improvements
- Contributing
- License
- User registration and authentication.
- CRUD operations for tasks (Create, Read, Update, Delete).
- Real-time updates for task status changes using WebSocket.
- OAuth2 authentication for API access.
- Simple and straightforward project structure.
The project follows the following directory structure:
task_manager_fastapi/
├── app/
│ ├── alembic/
│ ├── api/
│ │ ├── endpoints/
│ │ ├── middleware/
│ │ ├── models/
│ ├── core/
│ ├── db/
├── tests/
├── .env
├── .gitignore
├── alembic.ini
├── README.md
├── main.py
├── requirements.txt
app
: Contains the main application code.alembic
: Database migration files (if used).api
: Contains API endpoints for tasks and users.endpoints
: Task and user API endpoints.middleware
: Custom middleware (simple logging for example).models
: Pydantic models for request and response.
core
: Core utilities and configurations.db
: Database configuration and models.
tests
: Unit tests..env
: Store environment variables (e.g., database credentials)..gitignore
: Lists files and directories to be ignored by version control.alembic.ini
: Alembic configuration (if used).README.md
: Documentation about the project.requirements.txt
: List of project dependencies.
Before running the application, make sure you have the following prerequisites installed:
- Python 3.7+ (3.10+ recommended).
- PostgreSQL.
-
Clone the repository:
git clone https://github.com/Cheater121/task_manager_fastapi.git cd task_manager_fastapi
-
Create a virtual environment (recommended):
python3 -m venv venv source venv/bin/activate # On Windows, use venv\Scripts\activate.bat
-
Install project dependencies:
pip install -r requirements.txt
To run the FastAPI application locally, use the following command:
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
Replace 0.0.0.0
and 8000
with your desired host and port.
Or you can just run main.py
:
python3 main.py
The API exposes the following endpoints:
POST /api/v1/users/register/
: Register a new user.POST /api/v1/users/login/
: Authenticate and receive a JWT token.GET /api/v1/about_me/
: Retrieve user info (protected endpoint).GET /api/v1/tasks/
: Retrieve a list of tasks (protected endpoint).POST /api/v1/tasks/
: Create a new task (protected endpoint).GET /api/v1/tasks/{task_id}
: Retrieve a specific task (protected endpoint).PUT /api/v1/tasks/{task_id}
: Update a specific task (protected endpoint).DELETE /api/v1/tasks/{task_id}
: Delete a specific task (protected endpoint).
For real-time updates on task status changes, use WebSocket connections to /ws/tasks/{client_id}
.
Fast API comes with Swagger UI. This tool is automatically generated based on your API's route definitions and Pydantic models.
Once the API is running, Swagger UI can be accessed on the following URL:
http://localhost:8000/docs
You can use swagger UI to:
- Browse Endpoints
- Send Requests
- View Responses
- Test Validations
To Test with SwaggerUI, you can do the following for each endpoint explained above
-
Open your web browser and navigate to the /docs path as mentioned above.
-
Explore the available endpoints and select the one you want to test.
-
Click on the "Try it out" button to open an interactive form where you can input data.
-
Fill in the required parameters and request body (if applicable) according to the API documentation given above.
-
Click the "Execute" button to send the request to the API.
-
The response will be displayed below, showing the status code and response data.
-
You can also view example request and response payloads, which can be helpful for understanding the expected data format.
A suite of tests
using the pytest framework was used to help verify the functionality of the Task Manager FastAPI.
- Navigate to the
task_manager_fastapi
(root) directory using a terminal:
cd <your_path_to_project>/task_manager_fastapi
- Run the tests by executing the following command (don't forget to activate your virtual environment if used):
pytest
This command will automatically discover and run the test cases defined in the tests
directory.
Enhance your Task Manager FastAPI project with the following potential improvements:
-
Asynchronous Database Operations and Route Handlers:
- Utilize asynchronous database drivers like
asyncpg
for faster database access. - Rewrite route handlers as asynchronous to fully leverage FastAPI's asynchronous capabilities for better performance.
- Utilize asynchronous database drivers like
-
Task Prioritization and Deadlines:
- Allow users to set task priorities (e.g., high, medium, low).
- Implement deadlines and reminders for tasks, potentially using BackgroundTasks or Celery.
- Create sorting mechanisms for tasks based on priority and deadlines.
-
User Profile Management and User-Specific Tasks:
- Develop user profile management to enable users to update their information.
- Associate tasks with specific users, allowing each user to see only their tasks.
-
Task Assignment to Users or Groups:
- Enable task assignments to individual users or groups.
- Implement notifications for task assignments.
- Allow users to delegate tasks to others.
-
User Roles and Role-Based Access Control (RBAC):
- Introduce user roles (e.g., admin, manager, user) to control access.
- Implement Role-Based Access Control (RBAC) for fine-grained access control.
-
Test Expansion and Integration Tests:
- Expand unit tests to cover edge cases and scenarios.
- Develop integration tests to validate interactions between components.
- Create a test database for isolated and secure testing.
-
Advanced Logging and Error Handling:
- Enhance logging by categorizing log messages (info, warnings, errors).
- Implement structured logging for better analysis.
- Improve error handling for user-friendly error messages.
-
Database Layer Separation or Design Patterns:
- Consider separating database operations into a dedicated layer or applying a design pattern like the repository pattern.
- Modularize components into separate services for a microservices approach.
-
Security Enhancement with Cookies and Refresh Tokens:
- Implement secure cookies for browser-based sessions.
- Generate and validate refresh tokens for enhanced security.
- Implement token expiration and renewal mechanisms.
-
Scalability and Performance Optimization:
- Optimize database queries and indexes for improved performance.
- Introduce caching mechanisms (e.g., Redis) for frequently accessed data.
- Explore containerization and orchestration for scalable deployment.
-
User-Friendly Documentation:
- Improve API documentation using tools like Swagger UI or ReDoc.
- Provide detailed explanations and examples for API endpoints.
-
Internationalization and Localization:
- Enable multi-language support for the application.
- Allow users to select their preferred language.
-
User Feedback and Notifications:
- Implement a feedback system for users to report issues and suggestions.
- Add notification features such as email or in-app alerts for important updates.
These potential improvements can enhance the functionality, scalability, and user experience of your Task Manager FastAPI project. Prioritize them based on project goals and user needs.
Contributions are welcome! If you'd like to contribute to this project, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Make your changes and test thoroughly.
- Create a pull request with a clear description of your changes.
This project is licensed under the MIT License - see the LICENSE file for details.