- Clone the Repository https://github.com/pranjal-tank/twassignment.git.
- Install Docker on your system.
- 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
- 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
- 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
- now create superuser by executing the following command in your docker container shell:
python manage.py createsuperuser
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:
- Access Token (lifetime: 60 minutes)
- Refresh Token (lifetime: 1 day)
Eg.
Request:
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:
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:
Response:
If the access token is not expired then it will return a empty JSON response
If the access token is expired then it will return a JSON response that Token is Invalid or Expired
Now as we have the access token lets test our API's.
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:
Response:
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:
Response:
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:
Response:
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:
Response:
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:
Response:
Thank you