This documentation outlines how to set up and use the Loans API, a Django backend designed for secure and efficient management of customer and loan data.
The API is structured around the following endpoints:
- Obtain Token Pair:
POST /api/token-auth/
- Obtain an access token pair.
- Python 3.10.0 or higher
- Django 5.0.6 or higher
- Django REST framework 3.15.1 or higher
- Clone the repository:
git clone https://github.com/Sergioarg/loans_api.git
- Navigate to the project directory:
cd users_api/
- Create a virtual environment (optional but recommended):
python3 -m venv venv
- Activate the virtual environment:
- On Windows:
.\venv\Scripts\activate
- On Unix or MacOS:
source venv/bin/activate
- On Windows:
- Install the required packages:
pip3 install -r requirements.txt
- Apply migrations to set up the database:
python3 manage.py migrate
- Create an admin user:
python3 manage.py createsuperuser --username admin --email admin@example.com
To start the server, run:
python manage.py runserver
The server will start at http://localhost:8000
.
To interact with the API, you can use tools like curl
, Postman, or any HTTP client library in your preferred programming language.
Endpoint: http://127.0.0.1:8000/api/token-auth/
- Obtain API Token
- Endpoint:
/api/token-auth/
- Method:
POST
- Body:
{ "username": "exampleuser", "password": "examplepassword" }
- Response:
{ "token": "API-TOKEN" }
- Endpoint:
Once the application is running, you can access the API endpoints using a web browser or a tool like curl
or Postman.
- Curl Example:
curl --location --request GET 'http://127.0.0.1:8000/api/customers/' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token <YOUR_TOKEN>'
Table of methods
Endpoint | Method | Description | Response | Request Body | Parameters |
---|---|---|---|---|---|
/api/customers/ | GET | Retrieves a list of all customers. | An array of Customer objects. |
N/A | N/A |
/api/customers/{id}/ | GET | Retrieves a specific customer by their ID. | A Customer object or 404 Not Found if the customer does not exist. |
N/A | id : The ID of the customer. |
/api/customers/{id}/balance | GET | Retrieves the balance of the customer by their ID. | Balance of the customer | N/A | id : The ID of the customer. |
/api/customers/{id}/loans | GET | Retrieves the loans of the customer by their ID. | Array of loans | N/A | id : The ID of the customer. |
/api/customers/{id}/payments | GET | Retrieves the payments of the customer by their ID. | Array of payments | N/A | id : The ID of the customer. |
/api/customers/ | POST | Creates a new customer. | 201 Created with the created Customer object and the location of the new resource. |
A Customer object. |
N/A |
/api/customers/{id}/ | PUT | Updates an existing customer. | 204 No Content if the update is successful, or 404 Not Found if the customer does not exist. |
A Customer object with the fields to update. |
id : The ID of the customer to update. |
/api/customers/{id}/ | DELETE | Deletes a specific customer by their ID. | 200 OK if the deletion is successful, or 404 Not Found if the customer does not exist. |
N/A | id : The ID of the customer to delete. |
Endpoint: http://127.0.0.1:8000/api/customers/
- Create a Customer
- Endpoint:
/api/customers/
- Method:
POST
- Body:
{ "score": 6000, "external_id": "customer_01" }
- Response:
{ "score": "6000.00", "status": 1, "external_id": "customer_01", "preapproved_at": null }
- Endpoint:
Endpoint: http://127.0.0.1:8000/api/loans/
- Create a Loan
- Endpoint:
/api/loans/
- Method:
POST
- Body:
{ "amount": 4000, "external_id": "loan_01", "customer": 1 }
- Response:
{ "external_id": "loan_01", "amount": "4000.00", "status": 1, "outstanding": "4000.00", "customer_external_id": "customer_01" }
- Endpoint:
-
Endpoint:
http://127.0.0.1:8000/api/payments/
-
Create a Payment
- Endpoint:
/api/payments/
- Method:
POST
- Body:
{ "total_amount": 2000, "external_id": "payment_01", "customer": 1, "payment_loan_detail": [ {"loan": 1, "amount": 2000}, ] }
- Response:
{ "total_amount": "2000", "status": 4, "paid_at": "2024-05-11T23:34:29.703632Z", "external_id": "payment_01", "customer_external_id": "customer_01", "loan_external_id": "loan_01", "payment_amount": 2000.0 }
- Endpoint:
Execute the Django test runner to run all tests in the project.
python3 manage.py test
This is the ER diagram of the project.