Fastapi-Serviceutils: Release 2.0.0
Fastapi-Serviceutils: Release 2.0.0
Release 2.0.0
Simon Kallfass
1 Features 3
2 Content 5
3 Table of Contents 7
3.1 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.3 fastapi_serviceutils package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.4 See also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
i
ii
fastapi-serviceutils, Release 2.0.0
Services stand for portability and scalability, so the deployment and configuration of these service should be as
easy as possible. To achieve this a service based on fastapi-serviceutils is configured using a config.yml. These
settings can be overwritten using environment-variables. Dependency management for these services is generalized
using a combination of Dephell and Poetry.
For monitoring and chaining of service-calls some default endpoints should always be defined. For example an end-
point to check if the service is alive (/api/alive) and an endpoint to access the config of the service (/api/
config). These endpoints are automatically added in services using fastapi-serviceutils if defined in the config.
yml of the service.
Because a service should focus on only one task it may be required to create multiple small services in a short time. As
always time matters. For this fastapi-serviceutils allows ** fast creation of new services** with create_service.
If an error occurs during a service-call it is important to have detailed logs with a good traceback. To achieve this
the default logging of fastapi is optimized in fastapi-serviceutils using loguru.
Fastapi allows easily created swagger-documentation for service-endpoints. This is optimal for clients wanting to
integrate these endpoints. For developers of the service an additional apidoc-documentation of the service and
the source-code is required (most popular are documentations created using Sphinx or MKDocs). Fastapi-serviceutils
based services serve sphinx-based documentations using google-documentation style in the code and rst-files inside
the docs-folder.
The development of these services should be as much generalized as possible for easy workflows, as less manual steps
as possible for the developer and short onboarding times. For this fastapi-serviceutils includes a Makefile for most
common tasks during development. There is also a Tmuxp-config file to create a tmux-session for development.
CONTENTS 1
fastapi-serviceutils, Release 2.0.0
2 CONTENTS
CHAPTER
ONE
FEATURES
3
fastapi-serviceutils, Release 2.0.0
4 Chapter 1. Features
CHAPTER
TWO
CONTENT
5
fastapi-serviceutils, Release 2.0.0
6 Chapter 2. Content
CHAPTER
THREE
TABLE OF CONTENTS
3.1 Usage
3.1.1 exampleservice
The easiest way to explain how to use fastapi-serviceutils is to demonstrate usage inside an exampleservice. Here we
will explain the parts of the service and which functions and classes when to use.
To create a new service we use the tool create_service which is available after installing fastapi-serviceutils.
create_service -n exampleservice \
-p 50001 \
-a "Dummy User" \
-e [email protected] \
-ep example \
-o /tmp
This creates the service exampleservice inside the folder /tmp/exampleservice. As author with email we define
Dummy User and [email protected]. The initial endpoint we want to create is example. The service
should listen to port 50001.
If we change into the created directory we will have the following folder-structure:
exampleservice
app
config.yml
endpoints
__init__.py
v1
errors.py
example.py
__init__.py
models.py
__init__.py
main.py
.codespell-ignore-words.txt
docker-compose.yml
Dockerfile
docs
...
(continues on next page)
7
fastapi-serviceutils, Release 2.0.0
The files docker-compose.yml and Dockerfile are required for deployment of the service as docker-
container.
.tmuxp.yml is used for development of the service if you prefer to develop inside tmux in combination with for
example vim or emacs.
The .python-version defines which python-version this service uses and is used by poetry / dephell workflow
inside virtual-environments.
The pyproject.toml is used for dependency-management and package-creation.
setup.cfg contains configurations for tools used during development like yapf, flake8, pytest, etc.
The .pre-commit-config.yaml allows the usage of pre-commit and is also used in the make command make
check. It enables running of multiple linters, checkers, etc. to ensure a fixed codestyle.
The Makefile contains helper command like initializing the project, updating the virtual-environment, running tests,
etc.
Because codespell is used inside the configuration of pre-commit, the file .codespell-ignore-words.txt is
used to be able to define words to be ignored during check with codespell.
Initialising project
make init
This creates the virtual-environment and installs the dependencies as defined in the pyproject.toml. It also
initialises the project as a git-folder and creates the initial commit.
We now activate the poetry-shell to enable the environment:
poetry shell
Attention: Please ensure to always enable the poetry-shell before development using:
poetry shell
Folder-structure
Following shows code-relevant files for an exampleservice as created using the create_service-tool of fastapi-
serviceutils.
exampleservice
app
config.yml
endpoints
__init__.py
v1
errors.py
example.py
__init__.py
models.py
__init__.py
main.py
pyproject.toml
tests
__init__.py
service_test.py
pyproject.toml
The dependencies and definitions like the package-name, version, etc. are defined inside the pyproject.toml.
This file is used by Poetry and Dephell. Following the pyproject.toml for our exampleservice:
[tool.poetry.dependencies]
python = ">=3.7,<4"
fastapi-serviceutils = ">=2"
[tool.poetry.dev-dependencies]
autoflake = ">=1.3"
coverage-badge = ">=1"
flake8 = ">=3.7"
ipython = ">=7.8"
isort = ">=4.3"
jedi = ">=0.14"
neovim = ">=0.3.1"
pre-commit = ">=1.18.3"
pudb = ">=2019.1"
pygments = ">=2.4"
pytest = ">=5"
pytest-asyncio = ">=0.10"
pytest-cov = ">=2"
pytest-xdist = ">=1.30"
(continues on next page)
3.1. Usage 9
fastapi-serviceutils, Release 2.0.0
[tool.poetry.extras]
devs = [
"autoflake", "coverage", "coverage-badge", "flake8", "ipython", "isort",
"jedi", "neovim", "pre-commit", "pudb", "pygments", "pytest",
"pytest-asyncio", "pytest-cov", "pytest-xdist", "sphinx",
"sphinx-autodoc-typehints", "sphinx-rtd-theme", "yapf"
]
[tool.dephell.devs]
from = {format = "poetry", path = "pyproject.toml"}
envs = ["main", "devs"]
[tool.dephell.main]
from = {format = "poetry", path = "pyproject.toml"}
to = {format = "setuppy", path = "setup.py"}
envs = ["main"]
versioning = "semver"
[tool.dephell.lock]
from = {format = "poetry", path = "pyproject.toml"}
to = {format = "poetrylock", path = "poetry.lock"}
[tool.poetry.scripts]
exampleservice = "app.main:main"
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
app/config.yml
The service is configured using a config-file (config.yml). It is possible to overwrite these setting using
environment-variables. An example for the config.yml of the exampleservice is shown below:
˓→{extra[request_id]}] - <level>{message}</level>"
available_environment_variables:
env_vars:
- SERVICE__MODE
- SERVICE__PORT
- LOGGER__LEVEL
- LOGGER__PATH
- LOGGER__FILENAME
- LOGGER__ROTATION
- LOGGER__RETENTION
- LOGGER__FORMAT
external_resources_env_vars:
- EXTERNAL_RESOURCES__API__URL
- EXTERNAL_RESOURCES__API__SCHEMA
rules_env_vars: []
config: [service]
Inside this section we define the name of the service name. This name is used for the swagger-documentation and
extraction of the environment-variables.
The mode define the runtime-mode of the service. This mode can be overwritten with the environment-variable
EXAMPLESERVICE__SERVICE__MODE (where 'EXAMPLESERVICE' is the name of the service, meaning if you
have a service named SOMETHING the environment-variable would be SOMETHING__SERVICE__MODE).
The port configure the port the service will listen to. This can also be overwritten using the environment variable
EXAMPLESERVICE__SERVICE__PORT.
The description is used for the swagger-documentation.
To define the folder where the to find the apidoc to serve by route /api/apidoc/index.html the keyword
apidoc_dir is used.
readme defines where to get the readme from to be used as main description for the swagger-documentation at
/docs / /redoc.
To controll if only specific hosts are allowed to controll the service we use allowed_hosts. Per default a service
would allow all hosts ('*') but this can be customized here in the config.
3.1. Usage 11
fastapi-serviceutils, Release 2.0.0
To define which default endpoints should be included in our service we use use_default_endpoints. Cur-
rently we support the default endpoints /api/alive (inside config: 'alive') and /api/config (inside config:
'alive').
config: [external_resources]
Inside this section external dependencies (resources) are defines. A service can depend on other services, databases,
remote-connections or files / folders.
Dependencies to other services should be defined inside services. Database connections inside databases
(currently only postgres is supported). If any other dependency exist define it in other.
Defined services can be accessed in the code using app.config.external_resources.services or
ENDPOINT.config.external_resources.services depending if you are in a main part of the app or
inside an endpoint.
Databases are automatically included into the startup and shutdown handlers. You can access the database con-
nection using app.databases['DATABASE_NAME'] or ENDPOINT.databases['DATABASE_NAME']
depending if you are in a main part of the app or inside an endpoint.
config: [logger]
All settings inside this section are default Loguru settings to configure the logger. You can control where to log (path)
and how the logfile should be named (filename). Also which minimum level to log (level). To control when to
rotate the logfile use rotation. retention defines when to delete old logfiles. The format defines the format
to be used for log-messages.
config: [available_environment_variables]
app/__init__.py
Inside the __init__.py file of the app we only define the version of our service.
If we bump the version using either dephell bump {major, minor, fix} or poetry version
{major, minor, patch}, both the version defined here, and the version defined inside the pyproject.toml
will be increased.
app/main.py
app = make_app(
config_path=Path(__file__).with_name('config.yml'),
version=__version__,
endpoints=ENDPOINTS,
enable_middlewares=['trusted_hosts',
'log_exception'],
additional_middlewares=[]
)
if __name__ == '__main__':
main()
We define where to collect the config-file of the service from, the version of the service and which endpoints and
middlewares to use.
3.1. Usage 13
fastapi-serviceutils, Release 2.0.0
app/endpoints/v1/example.py
The ENDPOINT includes the router, route and the version of our endpoint.
Inside the endpoint-function we create a new bound logger with the request-id of the request to allow useful traceback.
Note: Defining endpoints like this allows our worklow with endpoint-versioning and usage of
fastapi_serviceutils.endpoints.set_version_endpoints() inside app/endpoints/v1/
__init__.py and app/endpoints/__init__.py.
app/endpoints/v1/models.py
The models.py contains models for the endpoints in version 1 of our exampleservice.
For each endpoint we create the model for the input (request) and the model for the output (response).
The models are of type pydantic.BaseModel
class GetExample(BaseModel):
msg: str
class Example(BaseModel):
msg: str
"""
In special cases also an ``alias_generator`` has to be defined.
An example for such a special case is the attribute ``schema`` of
:class:`SpecialParams`. The schema is already an attribute of a BaseModel,
so it can't be used and an alias is required.
@dataclass
class Complex:
"""Represent example model with attribute-change of model after init."""
accuracy: str
class SpecialParams(BaseModel):
"""Represent example model with special attribute name requiring alias."""
msg: str
schema_: str = Schema(None, alias='schema')
class Config:
"""Required for special attribute ``schema``."""
alias_generator = _alias_for_special_model_attribute
app/endpoints/v1/__init__.py
Note: If additional endpoints are available, these should be added here, too.
Note: If we would increase our version to version 2 and we want to change the endpoint example we would add an
additional folder inside app/endpoints named v2 and place the new version files there.
3.1. Usage 15
fastapi-serviceutils, Release 2.0.0
ENDPOINTS = set_version_endpoints(
endpoints=[example],
version='v1',
prefix_template='/api/{version}{route}'
)
__all__ = ['ENDPOINTS']
app/endpoints/__init__.py
In this file we import all endpoint-versions like in this example from app.endpoints.v1 import
ENDPOINTS as v1.
Note: If we would have an additional version 2 we would also add from app.endpoints.v2 import
ENDPOINTS as v2.
Note: If we would have version 2, too we would replace parameter endpoints with v2.
LATEST = set_version_endpoints(
endpoints=v1,
version='latest',
prefix_template='{route}'
)
ENDPOINTS = LATEST + v1
__all__ = ['ENDPOINTS']
tests
The tests for the exampleservice are using Pytest. We also used the testutils of fastapi-serviceutils. An
example for simple endpoint tests of our exampleservice:
Listing 9: tests/service_test.py
import pytest
from app.main import app
def test_endpoint_example():
json_endpoint(
application=app,
endpoint='/api/v1/example/',
payload={'msg': 'test'},
expected={'msg': 'test'}
)
@pytest.mark.parametrize(
'endpoint, status_code',
[
('/api/v1/example',
307),
('/api/',
404),
('/api/v1/',
404),
('/api/v1/example/',
200),
]
)
def test_endpoint_invalid(endpoint, status_code):
json_endpoint(
application=app,
endpoint=endpoint,
status_code=status_code,
payload={'msg': 'test'}
)
Databases
config.yml
If we use a database in our service we declare the connection info in the config.yml of the service like the follow-
ing:
3.1. Usage 17
fastapi-serviceutils, Release 2.0.0
For each database we want to use in our service, we define a new item inside databases. The key will be the name
of our database. The connection itself is defined as dsn. The databasetype defines the type of the database we are
using. This setting is for future releases of fastapi-serviceutils. Currently we only support postgres and this setting
has no effect. min_size and max_size define the minimum and maximum amount of connections to open to the
database.
app/endpoints/v1/dbs.py
Inside the module dbs.py we define our datatables like the following:
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
app/endpoints/v1/models.py
As for each endpoint we declare the input- and output-models we are using in our new endpoints like the following:
class InsertUser(BaseModel):
email: str
password: str
class Inserted(BaseModel):
msg: bool = True
class User(BaseModel):
id: int
(continues on next page)
app/endpoints/v1/insert_user.py
app/endpoints/v1/get_users.py
3.1. Usage 19
fastapi-serviceutils, Release 2.0.0
app/endpoints/v1/__init__.py
ENDPOINTS = set_version_endpoints(
endpoints=[get_users, insert_user],
version='v1',
prefix_template='/api/{version}{route}'
)
__all__ = ['ENDPOINTS']
The rest of our service, like the main.py, the __init__.py files of the modules, etc. have the same content as
described in exampleservice.
Services
If we need to call external services we first have to declare the service inside the config.yml like the following:
class CallExternalService(BaseModel):
street: str
street_number: str
zip_code: str
city: str
country: str
class ExternalServiceResult(BaseModel):
longitude: str
latitude: str
3.1.3 Helpers
3.1. Usage 21
fastapi-serviceutils, Release 2.0.0
create_service
Create new service following the structure as described in the fastapi_serviceutils documentation. Using Cookiecutter
to create the new folder.
optional arguments:
-h, --help show this help message and exit
-n SERVICE_NAME, --service_name SERVICE_NAME
the name of the service to create. ATTENTION: only
ascii-letters, "_" and digits are allowed. Must not
start with a digit!
-p SERVICE_PORT, --service_port SERVICE_PORT
the port for the service to listen.
-a AUTHOR, --author AUTHOR
the name of the author of the service.
-e AUTHOR_EMAIL, --author_email AUTHOR_EMAIL
the email of the author of the service.
-ep ENDPOINT, --endpoint ENDPOINT
the name of the endpoint for the service to create.
ATTENTION: only lower ascii-letters, "_" and digits
are allowed. Must not start with a digit!
-o OUTPUT_DIR, --output_dir OUTPUT_DIR
Makefile
Usual tasks during development are wrapped inside the Makefile. This contains updating of the environment, creation
of the docs, etc.
Usage:
Targets:
Flags:
tmuxp
For a predefined development environment the .tmuxp.yml configuration can be used to create a Tmux-session
(using Tmuxp) with a window including three panels:
• one panel for editing files
• one panel running the service
• one panel running the tests
Run the following command to create the tmux-session:
tmuxp load .
3.1.4 Deployment
For more detailed information about deployment of fastapi-based services see FastAPI deployment
Services based on fastapi-serviceutils can be easily deployed inside a docker-container.
Before deployment you need to:
• update the dependencies
• run all tests
• create the current requirements.txt
• ensure the docker-compose.yml is defined correctly including the environment-variables
To run these tasks run:
make finalize
To run the service using docker-compose customize the docker-compose.yml and run:
sudo docker-compose up -d
Basics
Docker
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
COPY requirements.txt ./
3.1. Usage 23
fastapi-serviceutils, Release 2.0.0
COPY . /app
Docker-compose
The service can be deployed with Docker compose using the Docker compose file:
services:
<SERVICENAME>:
build:
context: .
dockerfile: Dockerfile
image: <SERVICENAME>
ports:
- "<SERVICE_PORT>:80"
environment:
- <SERVICENAME>_SERVICE__MODE="prod"
- ...
volumes:
- type: bind
source: <LOGFOLDER_ON_HOST>
target: <LOGFOLDER_INSIDE_DOCKER_CONTAINER>
Environment-variables
3.2 Development
After cloning the repository the development environment can be initialized using:
make init
Note: Make sure to always activate the environment when you start working on the project in a new terminal using
poetry shell
make update
We use Poetry including the dependency definition inside the pyproject.toml and python-venv for environ-
ment management. Additionally we use Dephell and make for easier workflow.
• pyproject.toml: stores what dependencies are required in which versions. Required by Dephell and Po-
etry.
• poetry.lock: locked definition of installed packages and their versions of currently used devs-environment.
Created by Poetry using make init, make update, make tests or make finalize.
• .python-version: the version of the python-interpreter used for this project. Created by python-venv
using make init, required by Poetry and Dephell.
3.2.3 Testing
All tests are located inside the folder tests. Tests for a module should be names like <MODULE_NAME>_test.py.
Note: For often used functions and workflows during testing the functions and classes inside
fastapi_serviceutils.utils.tests can be used.
make tests
3.2. Development 25
fastapi-serviceutils, Release 2.0.0
3.2.4 Documentation
make docs
The created documentation (as html files) will be inside the docs/_build directory.
There is also a swagger-documentation to be used for users of the service. After starting the service the documentation
can be viewed at:
• http://0.0.0.0:<SERVICE_PORT>/docs
• http://0.0.0.0:<SERVICE_PORT>/redoc
The sphinx-documentation can be viewed after service-started and docs created at http://0.0.0.
0:<SERVICE_PORT>/apidoc/index.html.
3.3.1 Subpackages
fastapi_serviceutils.app package
Subpackages
fastapi_serviceutils.app.endpoints package
Subpackages
fastapi_serviceutils.app.endpoints.default package
Submodules
fastapi_serviceutils.app.endpoints.default.alive module
fastapi_serviceutils.app.endpoints.default.config module
fastapi_serviceutils.app.endpoints.default.models module
fastapi_serviceutils.app.handlers package
fastapi_serviceutils.app.middlewares package
Submodules
fastapi_serviceutils.app.logger module
fastapi_serviceutils.app.service_config module
fastapi_serviceutils.cli package
Submodules
fastapi_serviceutils.cli.create_service module
fastapi_serviceutils.utils package
Subpackages
fastapi_serviceutils.utils.docs package
Submodules
fastapi_serviceutils.utils.docs.apidoc module
fastapi_serviceutils.utils.external_resources package
Submodules
fastapi_serviceutils.utils.external_resources.dbs module
fastapi_serviceutils.utils.external_resources.services module
fastapi_serviceutils.utils.tests package
Submodules
fastapi_serviceutils.utils.tests.endpoints module
Internal documentation:
• API Documentation
• Development
• Deployment
Used tools:
• Cookiecutter
• Dephell
• Docker
• Docker compose
• Make
• Poetry
• restructuredText
• Sphinx
• Tmux
• Tmuxp
Used packages:
• Databases
• FastAPI
• Loguru
• Requests
• Toolz
• SQLAlchemy
Additional sources:
• FastAPI deployment
• Google docstring standard
• reStructuredText reference
• Type Annotations