Good Energy Tariff Calculator is a flask microservice for calculating tariff for local consumers in germany
Type | flask REST microservice |
Python Version | 3.9 |
Flask Version | 2.0.1 |
Authentication | Token based authentication |
Persistence | SQLite |
Providing an API interface to calculate tariff on the fly
REST Endpoint | Description |
---|---|
POST /api/auth/register |
Register new user in the system |
POST /api/auth/login |
Generate a token for API authentication |
GET /api/user/<username> |
Retrieve user by username |
POST /api/price/tariff |
Calculate tariff |
- Postman collection is available on path
/postman
- NOTE: API will require registering and login to proceed with protected endpoint
Issue following commands from root folder of the project (This will use the location_prices.csv for data sourcing)
- Build :
docker build -t <image-name> -f infrastructure/docker/Dockerfile .
- Run:
docker run -p 5000:5000 -td <image-name>
Environment variables defined in refer: Environment Variables* can be provided like below. (If not provided, will fallback to default)
- Run:
docker run -e SECRET_KEY=<secret_key> -p 5000:5000 -td <image-name>
It is preferred to create a virtual environment for project. Once you install virtual env, inside the project.
python3 -m venv .venv
To install the necessary packages:
source venv/bin/activate
pip3 install -r requirements.txt
This will install the required packages within your venv.
(SQLite3 is used for demonstration purposes. For production required a global MySQL/ Postgres database)
Database migrations are handled through Flask's Migrate Package. Migrations are used for updating and creating necessary tables/entries in your database.
To setup a SQLite3 database, navigate to the folder where good_energy_tariff_calc.py
is located and run:
export FLASK_APP=good_energy_tariff_calc.py
To migrate changes and create database
flask db upgrade
Use following commands to start service in development environment.
Note: Needs to have environment variables exposed. refer: Environment Variables Otherwise default values will be used.
export FLASK_APP=good_energy_tariff_calc.py
You can go ahead and run the application with a simple command:
flask run
For production, CSV file should be imported from a different method to the database. suggested approaches are,
- A database population script which will upsert data in CSV file to database (This can be triggered timely or as csv file as input (file change in S3))
- process:
- External provider upload file to a secure storage(SFTP)
- DB population script look for file changes or fetch file every few hours etc
- Upsert data in csv file to DB matching zip_code, city, street and house number fields
For demonstration purpose, following command can be used to populate the database with a local csv file or file URL.
FLASK_APP=good_energy_tariff_calc flask import_prices <file path or URL>
Following environment variables will be used by the service.
Variable Name | Description | Defaults |
---|---|---|
DATABASE_URL |
SQLite database url | /app.db |
JWT_SECRET_KEY |
secret key for JWT generation | my_precious_jwt |
SECRET_KEY |
secret for signing session cookie | my_precious |
- For development creating
.env
file with above environment variables is enough. - If not provided default configuration will be used
- SQLite database
- SQLAlchemy as the ORM
- Alembic used as the database migration tool
- Python 3.9 is recommended
Utilize unit test to provide proper test harness.
- unittest framework
- Execute the tests via
python3 -m unittest discover -s app/tests/
- Project is version controlled and available in GitHub
- Automated Build pipeline for build and test the service
- Static code analysis integration
- Add Swagger documentation
- Improve data persistence ( Postgres or MySQL database )
- Cloud deployment configuration (kube) and infrastructure versioning (Helm)
- Flask - https://flask.palletsprojects.com/en/2.0.x/
- Database migrations - https://flask-migrate.readthedocs.io/
- JWT authentication - https://flask-jwt-extended.readthedocs.io/en/stable/