This is a sample FastAPI project demonstrating user authentication using FastAPI-Users. Borrowed heavily from https://thedmitry.pw/blog/2023/08/fastapi-async-sqlalchemy-pytest-and-alembic/ as a way to learn more quickly and easily. Here's a brief overview of how you can bring together Pydantic, Alembic, pytest, SQLAlchemy, and other tools in a Python project:
Pydantic: Use Pydantic for data validation and serialization. Define Pydantic models to represent your application's data structures and ensure their integrity.
SQLAlchemy: Use SQLAlchemy for database interactions. Define SQLAlchemy ORM models to represent your database tables and relationships. Use Alembic for database migrations to manage changes to your database schema over time.
Alembic: Use Alembic to generate and manage database migrations based on changes to your SQLAlchemy models. Alembic can help automate the process of creating and applying database schema changes as your application evolves.
pytest: Use pytest for testing your application. Write test cases to ensure the correctness and reliability of your codebase. pytest provides a powerful and flexible framework for writing and running tests in Python. I am an advocate of Test Driven Development (TDD) and Domain Driven Design (DDD).
Pydantic-Settings: Use Pydantic-Settings for managing application settings and configuration. Define Pydantic models to represent your application's settings, and use Pydantic-Settings to load and validate configuration values from various sources (e.g., environment variables, configuration files).
The standalone program authorize.py is a starting point that deals with some of the concepts and functionality present in this project.
The standalone programs [tutorial.py, tutorial_models.py] are part of the YouTube tutorial by AmigosCode, which is referenced in the documents/Week 1 Instructional Objectives. There is an example test in the tests folder. Just type "pytest" in te tests folder or change the path to suit your environment.
The standalone program main.py is based on https://fastapi.tiangolo.com/tutorial
The folder 01Assignment contains the coding. The tests are contained under tests. When running pytest go to the specific subfolder to ensure proper module recognition.
- Python 3.7+
- MySQL database
- Postgres database
-
Clone the repository:
git clone https://github.com/allend27unitec/reStart.git
-
Navigate to the project directory:
cd reStart
-
Install dependencies:
pipenv update
-
Set up your MySQL database and update the connection URL in
main.py
:SQLALCHEMY_DATABASE_URL = "mysql://user:password@localhost/db_name"
-
Run the FastAPI server:
uvicorn main:app --reload
-
Register a new user:
- Send a
POST
request to/users/register
with the following JSON payload:{ "email": "user@example.com", "password": "password", "username": "username" }
- Send a
-
Login to obtain an access token:
- Send a
POST
request to/token
with the following JSON payload:{ "username": "username", "password": "password" }
- The response will include an access token.
- Send a
-
Access protected endpoints:
- Include the access token in the
Authorization
header of your requests:curl -X GET "http://localhost:8000/users/me" -H "Authorization: Bearer <access_token>"
- Include the access token in the
/users/register
: Register a new user./token
: Obtain an access token./users/me
: Access user profile information (protected endpoint).
- FastAPI: Web framework for building APIs with Python.
- FastAPI-Users: Library for user authentication and management in FastAPI applications.
- SQLAlchemy: SQL toolkit and Object-Relational Mapping (ORM) library for Python.
Contributions are welcome! Feel free to open an issue or submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.