/financial-calculator-backend

This project contains an API for a financial calculator. The API is coded in Python, using Flask and NumPy. Automated tests provide 98% coverage for the codebase.

Primary LanguagePythonMIT LicenseMIT

Welcome to financial calculator backend 👋

Documentation Maintenance Coverage License: MIT Twitter: JavierFeliuA

About

This project contains an API for a financial calculator. I coded this project in Python, using Flask and NumPy.

In conjunction with the Financial Calculator Frontend, this project provides a companion calculator for the personal finance book "Mejora Tu Situación."

Design Considerations

For this project, the book author presented me with a series of calculators built on MS Excel. The goal was to produce an online calculator that, given the same inputs, would return the same results as the Excel samples.

Most of Excel's financial formulas have the calculation method obfuscated. That is, you can't see how it arrives at the result.

The Python library NumPy contains 1:1 equivalents to all of Excel's financial formulas. For this reason, I decided to break up the problem into two:

  1. (This repository) An API in Python would receive the calculation parameters from the frontend and return the results calculated with NumPy.
  2. (The frontend repository) A frontend would provide a user interface for receiving calculation parameters and presenting the results.

Important links

  1. You can take a look at the frontend code here.
  2. Find the live calculator here.
  3. The live backend is here. The API is open, so you can test it using Postman (see the Postman section below for instructions).
  4. This project runs in a Kubernetes cluster at DigitalOcean. For information on how to create your cluster visit my do-managed-kubernetes repository.

Install

To use the project in your development machine, clone it, and go to the project's root:

git clone https://github.com/wanderindev/financial-calculator-backend.git
cd financial-calculator-backend

From the project's root, create and activate your virtual environment:

python3 -m venv venv
. venv/bin/activate

And install the project's dependencies:

pip install -r requirements.txt

Development

During develpment use:

docker-compose up --build

to create a container running the backend. Access the container at http://localhost:5001

Modify the code as needed and test using Postman (see instructions below), sending requests to the container running at localhost.

The calculators package contains all de classes for the different financial calculators.

The resources package contains all the endpoints

Postman

Import fc-backend.postman_collection.json into Postman for a collection of all endpoints.

Add a url variable to the environment with value http://localhost:5001 for development. Make sure you have a container running the backend as per instructions above before sending requests.

To test against the live backend, add a url variable to the Postman environment with value https://api.calcfina.com and send your requests.

Tests

To insure code quality, I added UnitTest to the project. All tests all located in the tests package.

To test the project, make sure your virtual environment is activated and run:

python -m unittest tests/test_credit_card_calculators.py tests/test_loan_calculators.py tests/test_retirement_calculators.py tests/test_saving_calculators.py

The included tests provide 98% coverage for the codebase. You can find the coverage report here.

Deployment

Before deployment, you have to replace the SECRET_KEY in line 32 of the Dockerfile for something, well, secret. Make sure you don't commit the modified Dockerfile to version control.

Next, you need to build the Docker image for the project and push it to your Docker Hub account:

docker build -t wanderindev/fc-backend .
docker push wanderindev/fc-backend

Replace wanderindev above (my Docker Hub account id) with your Docker Hub id.

Then, from the root of do-managed-kubernetes project run:

kubectl delete deployment api-calcfina
kubectl apply -f ./python/api-calcfina.yml

to create two pods running the backend and a service exposing them at port 80.

For more information on deploying to a Kubernetes cluster, visit my do-managed-kubernetes repository.

Author

👤 Javier Feliu

Show your support

Give a ⭐️ if this project helped you!

📝 License

This project is MIT licensed.


This README was generated with ❤️ by readme-md-generator