Coder Social home page Coder Social logo

goodenergytariffcalculator's Introduction

Good Energy Tariff Calculator API

Build

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

Table Of Contents

  1. Motivation
  2. API Documentation
  3. Getting Started
    1. Docker
    2. Local
      1. Virtual Environment
      2. Dependency Installation
      3. Setting up Database
      4. Running the Application
    3. Data sourcing
  4. Developer Notes
    1. Environment Setup
      1. Environment Variables
      2. Database
      3. Python
    2. Testing
    3. Continuous Integration
  5. Developer Checklist
  6. References

Motivation

Providing an API interface to calculate tariff on the fly

API Documentation

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

Getting Started

Via docker manually

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>

locally

Virtual Environment

It is preferred to create a virtual environment for project. Once you install virtual env, inside the project.

python3 -m venv .venv

Dependency installation

To install the necessary packages:

source venv/bin/activate
pip3 install -r requirements.txt

This will install the required packages within your venv.


Setting up SQLite3 Database

(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

Running the Application

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

Data sourcing

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>

Developer Notes

Environment setup

Environment Variables

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

Database

  • SQLite database
  • SQLAlchemy as the ORM
  • Alembic used as the database migration tool

Python

  • Python 3.9 is recommended

Testing

Utilize unit test to provide proper test harness.

  • unittest framework
  • Execute the tests via python3 -m unittest discover -s app/tests/

Continuous Integration

  • Project is version controlled and available in GitHub
  • Automated Build pipeline for build and test the service

Developer Checklist

  • Static code analysis integration
  • Add Swagger documentation
  • Improve data persistence ( Postgres or MySQL database )
  • Cloud deployment configuration (kube) and infrastructure versioning (Helm)

References

goodenergytariffcalculator's People

Contributors

thulana avatar tkan3934 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.