-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit db9a9b3
Showing
56 changed files
with
14,895 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
__pycache__ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# dependencies | ||
node_modules | ||
/media | ||
*.sqlite3 | ||
|
||
# testing | ||
/coverage | ||
|
||
# production | ||
/build | ||
/documentation | ||
|
||
# Distribution / packaging | ||
.Python build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
*.manifest | ||
*.spec | ||
|
||
# Log files | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
*.log | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
.pytest_cache/ | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
.hypothesis/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# pyflow | ||
__pypackages__/ | ||
|
||
# Environment | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
|
||
# If you are using PyCharm # | ||
.idea/ | ||
.idea/**/workspace.xml | ||
.idea/**/tasks.xml | ||
.idea/dictionaries | ||
.idea/**/dataSources/ | ||
.idea/**/dataSources.ids | ||
.idea/**/dataSources.xml | ||
.idea/**/dataSources.local.xml | ||
.idea/**/sqlDataSources.xml | ||
.idea/**/dynamic.xml | ||
.idea/**/uiDesigner.xml | ||
.idea/**/gradle.xml | ||
.idea/**/libraries | ||
*.iws /out/ | ||
|
||
# sftp configuration file | ||
sftp-config.json | ||
|
||
# Package control specific files Package | ||
Control.last-run | ||
Control.ca-list | ||
Control.ca-bundle | ||
Control.system-ca-bundle | ||
GitHub.sublime-settings | ||
|
||
# Visual Studio Code # | ||
vscode | ||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
.history | ||
|
||
# Git Auth File | ||
config.py |
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 |
---|---|---|
@@ -0,0 +1,178 @@ | ||
 | ||
 | ||
|
||
|
||
# Address Book API in FastAPI (Python) | ||
|
||
The purpose of this repository is to get my hands dirty with FastAPI skills. It would demonstrate various common operations which you perform for any API using FastAPI like Database connections, File uploads, Authentication, Sessions and More. | ||
|
||
# Why Fast API ? | ||
|
||
FastAPI is a modern back-end framework written in Python which can be used as an alternative to other popular Python based frameworks available like Django and Flask. | ||
|
||
### Reasons you might want to use Fast API: | ||
|
||
* **Fast**: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available. | ||
|
||
* **Fast to code**: Increase the speed to develop features by about 200% to 300%. | ||
|
||
* **Fewer bugs**: Reduce about 40% of human (developer) induced errors. | ||
|
||
* **Intuitive**: Great editor support. Completion everywhere. Less time debugging. | ||
|
||
* **Easy to learn (compared to Django and even Flask)**: Designed to be easy to use and learn. Less time reading docs. | ||
|
||
* **Short**: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs. | ||
|
||
* **Robust**: Get production-ready code. With automatic interactive documentation. | ||
|
||
* **Standards-based**: Based on (and fully compatible with) the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema. | ||
|
||
## Models | ||
|
||
At the time of writing this, the API has three models which would be Task, User and Address. Task model is totally unrestricted, anyone can perform CRUD operations on it. User module is for authentication which is token based. Authenticated users can add address and perform CRUD on it. Object level filtering has been applied which would restrict users to perform CRUD operations on addresses which were not created by them. | ||
|
||
```mermaid | ||
erDiagram | ||
TASK { | ||
int id | ||
string name | ||
string description | ||
} | ||
USER { | ||
int id | ||
string username | ||
string email | ||
string password | ||
} | ||
ADDRESS { | ||
int id | ||
int user_id | ||
string shipping_address | ||
string created_at | ||
} | ||
``` | ||
|
||
## Database | ||
|
||
Postgres is used for the database and Alembic package is used for migrations which means accomodating real time changes in the database. The code sample below demonstates the integration of SQLAlchemy and Postgres in a FastAPI project. | ||
|
||
``` | ||
from sqlalchemy import create_engine, MetaData | ||
from sqlalchemy.ext.declarative import declarative_base | ||
from sqlalchemy.orm import sessionmaker | ||
SQLALCHEMY_DATABASE_URL = f"postgresql://postgres:[email protected]/address-book" | ||
engine = create_engine(SQLALCHEMY_DATABASE_URL) | ||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | ||
Base = declarative_base() | ||
metadata = MetaData() | ||
def get_db(): | ||
db = SessionLocal() | ||
try: | ||
yield db | ||
finally: | ||
db.close() | ||
``` | ||
|
||
## Testing with Pytest | ||
|
||
A separate database is used exclusively for testing, configuration has been put inside a config.py file in the root folder of the project. | ||
|
||
``` | ||
DATABASE_USERNAME = 'postgres' | ||
DATABASE_PASSWORD = 'pass12345' | ||
DATABASE_HOST = '127.0.0.1' | ||
DATABASE_NAME = 'address-book' | ||
TEST_DATABASE_NAME = 'address-book-test' | ||
``` | ||
|
||
Code to test the app root end-point. We need to make sure 200 status code is returned when we hit this route. | ||
|
||
``` | ||
from fastapi.testclient import TestClient | ||
from main import app | ||
client = TestClient(app) | ||
def test_read_main(): | ||
response = client.get("/") | ||
assert response.status_code == 200 | ||
assert response.json() == {"message": "Hello Address Book API"} | ||
``` | ||
|
||
test_db.py | ||
|
||
``` | ||
from fastapi.testclient import TestClient | ||
from sqlalchemy import create_engine | ||
from sqlalchemy.orm import sessionmaker | ||
import config | ||
from db import Base, get_db | ||
from main import app | ||
DATABASE_USERNAME = config.DATABASE_USERNAME | ||
DATABASE_PASSWORD = config.DATABASE_PASSWORD | ||
DATABASE_HOST = config.DATABASE_HOST | ||
DATABASE_NAME = config.TEST_DATABASE_NAME | ||
SQLALCHEMY_DATABASE_URL = f"postgresql://{DATABASE_USERNAME}:{DATABASE_PASSWORD}@{DATABASE_HOST}/{DATABASE_NAME}" | ||
engine = create_engine(SQLALCHEMY_DATABASE_URL) | ||
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | ||
Base.metadata.create_all(bind=engine) | ||
def override_get_db(): | ||
try: | ||
db = TestingSessionLocal() | ||
yield db | ||
finally: | ||
db.close() | ||
app.dependency_overrides[get_db] = override_get_db | ||
client = TestClient(app) | ||
def test_create_user(): | ||
response = client.post( | ||
"/account/", | ||
json={"username": "Deadpool", "email": "[email protected]", "password": "chimichangas4life"}, | ||
) | ||
assert response.status_code == 201 | ||
data = response.json() | ||
assert data["email"] == "[email protected]" | ||
``` | ||
|
||
|
||
|
||
## Resources | ||
|
||
Following resources were used for motivation in designing this API. | ||
|
||
- [Fast API Official Website](https://fastapi.tiangolo.com/) | ||
- [Udemy Course - FastAPI](https://www.udemy.com/course/fastapi-the-complete-course/) | ||
|
||
## Authors | ||
|
||
* **Amit Prafulla (APFirebolt)** - [My Website](https://apgiiit.com) | ||
|
||
## License | ||
|
||
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details | ||
|
||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# A generic, single database configuration. | ||
|
||
[alembic] | ||
# path to migration scripts | ||
script_location = alembic | ||
|
||
# template used to generate migration files | ||
# file_template = %%(rev)s_%%(slug)s | ||
|
||
# sys.path path, will be prepended to sys.path if present. | ||
# defaults to the current working directory. | ||
prepend_sys_path = . | ||
|
||
# timezone to use when rendering the date within the migration file | ||
# as well as the filename. | ||
# If specified, requires the python-dateutil library that can be | ||
# installed by adding `alembic[tz]` to the pip requirements | ||
# string value is passed to dateutil.tz.gettz() | ||
# leave blank for localtime | ||
# timezone = | ||
|
||
# max length of characters to apply to the | ||
# "slug" field | ||
# truncate_slug_length = 40 | ||
|
||
# set to 'true' to run the environment during | ||
# the 'revision' command, regardless of autogenerate | ||
# revision_environment = false | ||
|
||
# set to 'true' to allow .pyc and .pyo files without | ||
# a source .py file to be detected as revisions in the | ||
# versions/ directory | ||
# sourceless = false | ||
|
||
# version location specification; This defaults | ||
# to alembic/versions. When using multiple version | ||
# directories, initial revisions must be specified with --version-path. | ||
# The path separator used here should be the separator specified by "version_path_separator" below. | ||
# version_locations = %(here)s/bar:%(here)s/bat:alembic/versions | ||
|
||
# version path separator; As mentioned above, this is the character used to split | ||
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep. | ||
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas. | ||
# Valid values for version_path_separator are: | ||
# | ||
# version_path_separator = : | ||
# version_path_separator = ; | ||
# version_path_separator = space | ||
version_path_separator = os # Use os.pathsep. Default configuration used for new projects. | ||
|
||
# the output encoding used when revision files | ||
# are written from script.py.mako | ||
# output_encoding = utf-8 | ||
|
||
sqlalchemy.url = driver://user:pass@localhost/dbname | ||
|
||
|
||
[post_write_hooks] | ||
# post_write_hooks defines scripts or Python functions that are run | ||
# on newly generated revision scripts. See the documentation for further | ||
# detail and examples | ||
|
||
# format using "black" - use the console_scripts runner, against the "black" entrypoint | ||
# hooks = black | ||
# black.type = console_scripts | ||
# black.entrypoint = black | ||
# black.options = -l 79 REVISION_SCRIPT_FILENAME | ||
|
||
# Logging configuration | ||
[loggers] | ||
keys = root,sqlalchemy,alembic | ||
|
||
[handlers] | ||
keys = console | ||
|
||
[formatters] | ||
keys = generic | ||
|
||
[logger_root] | ||
level = WARN | ||
handlers = console | ||
qualname = | ||
|
||
[logger_sqlalchemy] | ||
level = WARN | ||
handlers = | ||
qualname = sqlalchemy.engine | ||
|
||
[logger_alembic] | ||
level = INFO | ||
handlers = | ||
qualname = alembic | ||
|
||
[handler_console] | ||
class = StreamHandler | ||
args = (sys.stderr,) | ||
level = NOTSET | ||
formatter = generic | ||
|
||
[formatter_generic] | ||
format = %(levelname)-5.5s [%(name)s] %(message)s | ||
datefmt = %H:%M:%S |
Oops, something went wrong.