Skip to content

pranjal-tank/Rest-API-Django

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SETUP

  1. Clone the Repository https://github.com/pranjal-tank/twassignment.git.
  2. Install Docker on your system.
  3. Redirect to the clone repository on your local machine and run the Dockerfile which is in the clone repository to create project image by executing the following command in the terminal:
docker build . -t [image name that you want to give]

Eg. docker build . -t api-container

  1. Now with the help of the docker image create and run the docker container by executing the following command:
docker run -d -p 8000:8000 [image name]

Eg. docker run -d -p 8000:8000 api-container

  1. Now in your docker container shell execute the following command to create table in your database with the help of model schemas in models.py file in api django application:
python manage.py makemigrations
python manage.py migrate
  1. now create superuser by executing the following command in your docker container shell:
python manage.py createsuperuser

Testing API's

NOTE: The API's can only be access by the superuser

To test the API's you can use any fronend application or 3rd party apps or library but here I am using HTTPie.

  • To Install HTTPie run the following command in your local machine terminal (make sure pip is installed in your local machine):
pip install httpie

Now to Access the API's the superuser need a access token as all the API's are using JWT Authentication.

  • To generate access token using HTTPie run the following command in the terminal:

I am assuming that the server is running on localhost:8000

http POST http://localhost:8000/gettoken username=[superuser name] password=[superuser password]

You will get two token as a JSON response:

  1. Access Token (lifetime: 60 minutes)
  2. Refresh Token (lifetime: 1 day)

Eg.

Request:

get token

Response:

get token response

The refresh Token is use to generate new access token if the old token got expired.

  • To generate new token from refresh token run the following command in terminal:
http POST http://localhost:8000/refreshtoken refresh=[refresh token]

This will return a JSON response with a new assess token.

Eg.

Request:

refresh token

Response:

refresh token response

  • To verify if the access token is expired or not run the following command in your terminal:
http POST http://localhost:8000/verifytoken token=[access token]

This will return a JSON response about the access token.

Eg.

Request:

verify token

Response:

If the access token is not expired then it will return a empty JSON response

verify token response 1

If the access token is expired then it will return a JSON response that Token is Invalid or Expired

verify token response 2

Now as we have the access token lets test our API's.

API: Create a Company

To test this API using HTTPie run the following command in the terminal:

http -f POST http://localhost:8000/createcompany company_name=[company name] company_ceo=[company CEO] company_address=[company address] inception_date=[inception date in YYYY-MM-DD formate] 'Authorization:Bearer [access token]'

This will return a JSON message Company created.

Eg.

Request:

create company

Response:

create company response

API: Create a Team

To test this API using HTTPie run the following command in the terminal:

http POST http://localhost:8000/createteam/[Company id] team_leader=[team leader] 'Authorization:Bearer [access token]'

This will return a JSON message Team created.

Eg.

Request:

create team

Response:

create team response

API: Get Company object from ID

To test this API using HTTPie run the following command in the terminal:

http  GET http://localhost:8000/getcompany/[Company ID] 'Authorization:Bearer [access token]'

This will return a JSON response with all the details of the company requested.

Eg.

Request:

get company

Response:

get company response

API: Search company by name

To test this API using HTTPie run the following command in the terminal:

http  GET http://localhost:8000/searchcompany/[Company name] 'Authorization:Bearer [access token]'

This will return a JSON response with all the details of the company searched.

Eg.

Request:

search company

Response:

search company response

API: Get All teams

To test this API using HTTPie run the following command in the terminal:

http  GET http://localhost:8000/allteam/[Company ID] 'Authorization:Bearer [access token]'

This will return a JSON response with all the teams of a company grouped within company object.

Eg.

Request:

all team

Response:

all team response

Thank you

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published