Call Billing Service has two main APIs:
- API receives information of a call, including user_name and call_duration
- API returns billing information of a user based on total duration of all calls
Call |
---|
id (bigint) |
user_name (varchar(32)) |
call_duration (int) |
created_at (datetime(6)) |
updated_at (datetime(6)) |
A call object contains user_name of caller and call_duration is the duration of the call
Framework: Django
.
├── app # Django app setting
│ ├── settings # Settings folder
│ │ ├── base.py # Basic settings
│ │ └── production.py # For production settings
│ ├── asgi.py # ASGI config
│ ├── env.py # Env config
│ ├── urls.py # API urls of app
│ └── wsgi.py # WSGI config
├── call_billing # call_billing app, contains business about call and billing
│ ├── apis # Define APIs of app
│ │ ├── billing_api.py # Define APIs of billing
│ │ └── call_api.py # Define APIs of call
│ ├── migrations # Migrations folder (generated by framework)
│ ├── services # Services contain business logic which are called by apis
│ │ ├── billing_service.py # Service for billing
│ │ └── call_service.py # Service for call
│ ├── tests
│ │ ├── apis
│ │ │ ├── test_billing_api.py # Integration test of billing api
│ │ │ └── test_call_api.py # Integration test of call api
│ │ ├── models # Unit test of model
│ │ │ └── test_call_model.py # Unit test of call model
│ │ ├── services # Unit test of services
│ │ │ ├── test_billing_service.py # Unit test of billing service
│ │ │ └── test_call_service.py # Unit test of call service
│ │ └── factories.py # Provide a default of getting a new instance
├── docker # docker config
│ ├── local.Dockerfile # Dockerfile for local build
│ ├── production.Dockerfile # Dockerfile for production build
│ └── web_entrypoint.sh # Web entrypoint config
├── requirments # Dependency
│ ├── base.txt # Base dependecies
│ ├── local.txt # For local env
├── manage.py # manage file of framework
└── README.md # Readme
This project contains many layers:
- Models
- Models take care of the data model
- Services
- Business logic lives in services
- Services access the database & other resources & interact with other parts in system
- APIs
- Interface of input and output that interact with clients
- Python=3.10
- MySQL ver 8
- Create virtualenv
python3.10 -m venv .venv
source .venv/bin/activate
- Install requirements
(.venv) pip install -r requirements/local.txt
- Create
.env
file with configDB_NAME
,DB_USERNAME
,DB_PASSWORD
,DB_PORT
,DB_HOST
- Run app
(.venv) python manage runserver
The app will run on port 8000
in docker-compose.yaml
, there are 2 services: db
and django
- For compose v2:
docker compose up -d
- For compose v1:
docker-compose up -d
The app will run on port 8000
Example:
curl --location --request PUT 'http://localhost:8000/mobile/user_name/call' \
--header 'Content-Type: application/json' \
--data-raw '{
"call_duration": 545000
}'
After this API has performed, a record will be inserted into calls
table in the database
Example:
curl --location --request GET 'http://localhost:8000/mobile/user_name/billing'
The billing info will be retured for the user
The code includes both unit test and integration test
- Unit test for models and services
- Integration test for apis
- Factory boy for creating object in database in test