The provided code is a Flask-based API application that exposes...
The provided code is a Flask-based API application that exposes an endpoint /emi-calc-endpoint
for calculating the Equated Monthly Installment (EMI) for a loan based on the principal loan amount, annual interest rate, and tenure (number of months).
Here's a breakdown of how the code works:
Key Points:
-
Endpoint:
/emi-calc-endpoint
: Expects aPOST
request with data in JSON format.- The data should include three key parameters:
amount
(loan principal),interest
(annual interest rate as a percentage), andtenure
(loan tenure in months).
-
Logic:
- The EMI is calculated using the formula:
[
EMI = \frac{P \cdot R \cdot (1 + R)^N}{(1 + R)^N - 1}
]
Where:
- (P) = principal loan amount.
- (R) = monthly interest rate = (annual interest rate / 12 / 100).
- (N) = loan tenure in months.
- The EMI is calculated using the formula:
[
EMI = \frac{P \cdot R \cdot (1 + R)^N}{(1 + R)^N - 1}
]
Where:
-
Response:
- On success, the response includes:
{ "version": "7.2", "data": { "emi_result": "₹ {calculated_emi}", "rate": "{interest}% pa" } }
- On failure (e.g., missing input data or invalid format), it returns an error with a status code of 400 and an error message.
- On success, the response includes:
Example Request:
A sample client request to the /emi-calc-endpoint
would look like this:
HTTP Request
POST /emi-calc-endpoint HTTP/1.1
Content-Type: application/json
Host: localhost:5000
{
"values": {
"amount": 500000, // Loan amount in currency units
"interest": 7.5, // Annual interest rate in percentage
"tenure": 60 // Tenure in months
}
}
Example Data in JSON Format:
{
"values": {
"amount": 500000,
"interest": 7.5,
"tenure": 60
}
}
Example Response:
If the input is valid, the response returned by the API will look like:
JSON Response
{
"version": "7.2",
"data": {
"emi_result": "₹ 10137.37",
"rate": "7.5% pa"
}
}
This means that for a loan amount of ₹500,000 at an annual interest rate of 7.5% over 60 months (5 years), the calculated EMI is ₹10,137.37 per month.
Error Handling:
If invalid data is sent, the response might contain:
Example Error Response:
{
"error": "could not convert string to float: 'invalid_amount'"
}
This indicates either invalid data or a missing input in the request payload. The HTTP status code in this case would be 400
.