itsMagondu / mincredy

Crediation interview API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

INTRODUCTION

Minicredy is a simple django API that connects to a json file instead of the normal ORM connection to a database.

Authentiation: jwt Authentication

DESCRIPTION

The following api endpoints are supported.

Endpoint HTTP Method CRUD Method Description Return On success
/api/v1/users/ GET READ Get all users all users
/api/v1/users/ POST CREATE Add a user id of newly added user
/api/v1/users/<int: id>/ GET READ Get a specific user details of specific user
/api/v1/users/<int: id>/ PUT UPDATE Edit a user updated details of edited user
/api/v1/users/<int: id>/ DELETE DELETE Delete a user message: user deleted
/api/v1/loans/ GET READ Get all loans all loans
/api/v1/loans/ POST CREATE Add a loan id of newly added loan
/api/v1/loans/<int: id>/ GET READ Get a specific user details of specific user
/api/v1/loans/<int: id>/ PUT UPDATE Edit a loan updated details of edited loan
/api/v1/loans/<int: id>/ DELETE DELETE Delete a loan message: loan deleted

Getting Started

Requirements

  1. Django==3.2.9
  2. djangorestframework==3.12.4
  3. djangorestframework-simplejwt

Initial setup

Clone this repository
Navigate to the main folder
Create a virtual environment
Activate the Virtual environment
Install all the requirements
Create a users.json file with the format exactly the same as the one attached or just use the attached file.
The steps are shown below

$ git clone https://github.com/kimengu-david/mincredy
$ cd mincredy
$ python -m venv venv
$ source venv/bin/activate
$ pip install -r requirements.txt 

Create a superuser

$ python manage.py createsuperuser


Perform all the migrations this is due to jwt authentication that is in use.

$ python manage.py migrate


Start your project

$ python manage.py runserver

Running the Tests

There are eleven test cases that have been written, run the following command to run the provided tests.

$ python manage.py test

Examples

Getting a specific user from the json file.

step: 1

In this example we will use curl to make our requests, you could as well use httpie or use a different client like postman.

Run the following command to get your bearer token.

$ BearerToken=$(curl -s -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' --data '{"username":"david","password":"save"}' http://127.0.0.1:8000/api/token/|python3 -c "import sys, json; print(json.load(sys.stdin)['access'])")

step: 2

make a request to the database using the Bearer token from the above step.

$ curl -H 'Accept: application/json' -H "Authorization: Bearer ${BearerToken}" http://127.0.0.1:8000/api/v1/users/1/;echo ""

Getting all the users from a json file.

Use the previously generated bearer token to make your requests.

$ curl -H 'Accept: application/json' -H "Authorization: Bearer ${BearerToken}" http://127.0.0.1:8000/api/v1/users/;echo ""

Getting a specific user from a json file.

$ curl -H 'Accept: application/json' -H "Authorization: Bearer ${BearerToken}" http://127.0.0.1:8000/api/v1/users/1/;echo ""

Adding a user to the json file.

Note that this method on success will return the id of the newly added when making the request set the id to any value but the api will overrite your value to a new id based on existing ids this is due to json limitations unlike if a database system was in use.

$ curl -X POST -H "Authorization: Bearer ${BearerToken}" -H "Content-Type: application/json" -d '{
    "id": -1,
    "firstname": "Ambitious",
    "lastname": "Johnson",
    "phone_number": "0713434342",
    "occupation": "teacher",
    "nationality": "Kenyan",
    "age": 30,
    "loan_limit": "ksh.200"
}' http://127.0.0.1:8000/api/v1/users/ ; echo""

Getting all the loans from the json file.

$ curl -H 'Accept: application/json' -H "Authorization: Bearer ${BearerToken}" http://127.0.0.1:8000/api/v1/loans/;echo ""

Getting a specific loan from the json file.

$ curl -H 'Accept: application/json' -H "Authorization: Bearer ${BearerToken}" http://127.0.0.1:8000/api/v1/loans/1/;echo ""

Adding a loan to the json file

The id is generated by the api but we set it temporarily to -1 due to json limitations, on success the id of the newly added user will be returned

$ curl -X POST -H "Authorization: Bearer ${BearerToken}" -H "Content-Type: application/json" -d '{
            "id": -1,
            "user_id": 3,
            "loan_amount": "ksh.300",
            "days": 30,
            "loan_status": "npl"
}' http://127.0.0.1:8000/api/v1/loans/ ; echo""

About

Crediation interview API

License:MIT License


Languages

Language:Python 100.0%