Api-Demo: Platform-As-A-Service (Paas) Based Solution
Api-Demo: Platform-As-A-Service (Paas) Based Solution
build passing
codecov 96%
Sample microservice API with Python, Flask, Flask-Alchemy, Flask-RESTPlus and Pytest
Architecture Overview
Solution uses two tiers:
Multiple infrastructure topologies are supported. For example we can deploy the application,
using cloud platform-as-a-service-based solution or docker-based solution.
Auto scaling
Monitoring
Alerting
Diagnostics
Security
Authentication
CD integration with GitHub, Bitbucket, etc.
To create complete solution this approach would require much more work on the infrastructure.
Application Design
Packages
Solution uses layered style with three layers:
Scaling business logic (horizontal extensibility) - business packages can be added and
maintained without any impact to the existing code base
Python Frameworks
Application is implemented using Python.
Flask
Flask is a lightweight web application framework. It enables agile, quick and easy development
and can scale up to complex applications. Free, open source, active community, provides a lot of
plugins and extensions.
Compared to Django is more lightweight and suited better for REST API.
Flask-RESTPlus
Adds some RESTful capabilities to Flask:
Defines namespaces which are ways of creating prefixes and structuring the code. This
helps long-term maintenance and helps with the design when creating new endpoints.
It has a full solution for parsing input parameters.
It has a serialization framework for the resulting objects - response marshalling.
It has Swagger API documentation support, automatically generates Swagger specification,
self-documenting page and Swagger UI.
Flask-Alchemy
Python flask plugin, wrapper around the popular Python's SQLAlchemy ORM.
Pytest
Python testing framework. Plugin for Flask application integration.
Compared to other frameworks, e.g. unittest and nose - more flexibility, reusability. Considered
more pythonic. Can generate coverage reports in HTML and XML formats.
Directory Structure
/
├─ zentopia/
│ ├─ __init__.py
| └─ db.py
|
├─ zentopia_api/
│ ├─ __init__.py
| └─ app.py
|
├─ zentopia_product/
│ ├─ __init__.py
│ ├─ business.py
│ └─ models.py
|
├─ zentopia_product_api/
│ ├─ __init__.py
| └─ routes.py
|
├─ tests/
| ├─ __init__.py
| ├─ conftest.py
| ├─ test_zentopia_api.py
| ├─ test_zentopia_product.py
| └─ test_zentopia_product_api.py
|
├─ .gitignore
├─ .travis.yml
├─ logging.conf
├─ README.md
├─ requirements.txt
├─ settings.py
├─ setup.py
├─ wsgi.py
└─ ...
Continuous Integration
Travis is used for CI. Travis executes pytest test cases and generates coverage report which is
uploaded to Codecov.io.
How To
Create Virtual Environment
Testing
For application tests, pytest is used. To execute application tests:
(.venv) C:\Sandbox\Learn\Python\restplus\basic-api>
pytest -v --cov=zentopia --
cov=zentopia_product --cov=zentopia_product_api
============================= test session starts ==============================
platform win32 -- Python 3.7.6rc1, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 --
c:\sandbox\learn\python\restplus\basic-api\.venv\scripts\python.exe
cachedir: .pytest_cache
rootdir: C:\Sandbox\Learn\Python\restplus\basic-api
plugins: cov-2.10.0, flask-1.0.0
collected 23 items
In addition it will generate HTML and XML coverage reports. You can open the
htmlcov/index.html file in a browser to observe the coverage details.