Simple CLI App using Python for making requests to an API.
API: https://gorest.co.in/
For methods POST, PUT, PATCH and DELETE you must generate an access token from the API and add it to a .env file, similar to the .env.sample file in this repository
.env
AUTHORIZATION='Bearer yourKeyHere'
python = "^3.10"
requests = "^2.27.1"
click = "^8.1.3"
python-decouple = "^3.6"
- Create virtual environment
python -m virtualenv cli_venv
- Activate virtual environment
- Windows
cli_venv\Scripts\activate
- Linux
source cli_venv/bin/activate
- Clone repository and cd to api-testing
cd api-testing
- Install Requirements
pip install -r requirements.txt
Running 'python main.py', will display the possible options and args that can be used
python main.py
Usage: main.py [OPTIONS] COMMAND [ARGS]...
CLI app for making calls to endpoints at https://gorest.co.in/
Options:
--help Show this message and exit.
Commands:
comments Endpoint: '/comments'
posts Endpoint: '/posts'
todos Endpoint: '/todos'
users Endpoint: '/users'
python main.py users
Usage: main.py users [OPTIONS] COMMAND [ARGS]...
Endpoint: '/users'
Options:
--help Show this message and exit.
Commands:
create-post Create post for existing user by passing its id and...
create-todo Create todo for existing user by passing its id and...
delete Delete existing user by passing its id (PRIMARY_KEY)
get-detail Call users api to retrieve user detail with id...
get-list Call users api to retrieve list of users with optional...
get-posts View posts of existing user by passing its id (PRIMARY_KEY)
get-todos View todos of existing user by passing its id (PRIMARY_KEY)
patch Partially modify existing user by passing its id...
post Create a new user
put Modify existing user by passing its id (PRIMARY_KEY)
pytho main.py users get-detail 2488
User details...
{'id': 2488, 'name': 'Gitanjali Joshi', 'email': 'joshi_gitanjali@hayes.net', 'gender': 'female', 'status': 'active'}
Tests were written using the standard unittest library
To run all test:
python -m unittest discover -s tests
The test mostly use the Mock class and patch() function from the unittest.mock library
To try to ensure that the mock requests represented real data from the actual API, tests that made calls to the real API were written to compare the keys returned to the keys used in the mock tests.
python -m unittest tests.test_post_service
Test files:
- test_comment_service.py
- test_post_service.py
- test_todo_service.py
- test_user_service.py