Python SDE, Full-Stack Dev, Data Scientist, ML Engineer, R, PHP, Java.
- Python
- RESTful API
- Flask
- Postman
- Docker
- Launching the APP locally
- Test cases to test on Postman (link: https://postman.com/)
Postman is a standalone software testing API platform that allows users to build, test, design, modify, and document APIs
PyCharm ver. 2024.2.0.1
| Name |
|---|
| Docker Desktop |
| XAMPP |
| Postman Agent |
| Github Desktop |
- For SQL I'm using XAMPP Server with the following configuration
- username: root
- password:
- database: task_db
- An account on Postman (link: https://postman.com/).
This code requires Python 3.1x.x to run.
Install the dependencies and devDependencies. project_name/requirements.txt
Enter the following in PyCharm Bash Shell (Terminal):
cd task_management_api
pip install -r requirements.txtThis will install all the necessary packages to run the APP.
For SQL environments: project_name/task_management_api/initialize_db.py
python initialize_db.pyThis will create all the necessay Tables and their columns inside MySQL Server
The following is the structure of the project. Your directory should look similar to this.
task_management_api/
│
├── app/
│ ├── __init__.py
│ ├── models.py
│ └── routes.py
│
├── migrations/
│ ├── .
│ └── .
│
├── docker-compose.yml
├── rquirements.txt
├── test.py
└── run.py
Open PyCharm Terminal and make sure the current directory is project_name/task_management_api/.
The following file must be present in the directory:
run.pyNext, Type the following in the terminal:
python run.pyIf done correctly, The following should appear on the terminal:
* Serving Flask app 'app'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: xxx-xxx-xxxNow the APP has launched!
- Open the Postman site
- Login/Signup into your account
-
Download and Install Postman: Download Postman.
-
Create a New Request: Open Postman and click on “New” > “Request”. Enter a name for your request and choose a collection if desired.
-
Set Up the Request: Method: Choose POST (or other methods depending on the endpoint). URL: Enter the URL of your endpoint, e.g., http://127.0.0.1:5000/api/register. Headers: Add a header with Key: Content-Type and Value: application/json. Body: Select raw and enter your JSON payload, e.g.
{ "username": "testuser", "password": "testpass" } -
Send the Request: Click “Send” and view the response in the lower section of the Postman window.
Endpoint: POST /api/register Request: URL: http://127.0.0.1:5000/api/register Method: POST Headers: Content-Type: application/json Body:
{
"username": "testuser",
"password": "testpass"
}Expected Response: Status Code: 201 Created Body:
{
"message": "User registered successfully"
}Endpoint: POST /api/login Request: URL: http://127.0.0.1:5000/api/login Method: POST Headers: Content-Type: application/json Body:
{
"username": "testuser",
"password": "testpass"
}Expected Response: Status Code: 200 OK Body:
{
"access_token": "your_jwt_token_here"
}Endpoint: POST /api/tasks Request: URL: http://127.0.0.1:5000/api/tasks Method: POST Headers: Content-Type: application/json Authorization: Bearer <your_jwt_token_here> Body:
{
"title": "Test Task",
"description": "This is a test task.",
"status": "Todo",
"priority": "High",
"due_date": "2024-08-31",
"user_id": 1
}Expected Response: Status Code: 201 Created Body:
{
"message": "Task created successfully"
}Endpoint: GET /api/tasks Request: URL: http://127.0.0.1:5000/api/tasks Method: GET Headers: Authorization: Bearer <your_jwt_token_here>
Expected Response: Status Code: 200 OK Body:
[
{
"id": 1,
"title": "Test Task",
"description": "This is a test task.",
"status": "Todo",
"priority": "High",
"due_date": "2024-08-31T00:00:00",
"created_at": "2024-08-24T00:00:00",
"updated_at": "2024-08-24T00:00:00",
"user_id": 1
}
]Endpoint: PUT /api/tasks/<task_id> Request: URL: http://127.0.0.1:5000/api/tasks/1 (replace <task_id> with the actual task ID) Method: PUT Headers: Content-Type: application/json Authorization: Bearer <your_jwt_token_here> Body:
{
"title": "Updated Task",
"description": "This task has been updated.",
"status": "In Progress",
"priority": "Medium"
}Expected Response: Status Code: 200 OK Body:
{
"message": "Task updated successfully"
}Endpoint: DELETE /api/tasks/<task_id> Request: URL: http://127.0.0.1:5000/api/tasks/1 (replace <task_id> with the actual task ID) Method: DELETE Headers: Authorization: Bearer <your_jwt_token_here>
Expected Response: Status Code: 200 OK Body:
{
"message": "Task deleted successfully"
}Notes: • Replace <your_jwt_token_here> with the JWT token you receive from the login endpoint. • Ensure that your Flask server is running and accessible at http://127.0.0.1:5000. • Adjust the test cases as needed based on your actual implementation and requirements.
If you haven’t already, install Docker Desktop for Windows:
- Download Docker Desktop from the Official docker website.
- Run the installer and follow the prompts.
- After installation, launch Docker Desktop and ensure it’s running. You should see the Docker icon in your system tray.
For better performance, especially with file system operations, enable WSL 2 (Windows Subsystem for Linux):
- Go to the Docker Desktop settings.
- Under the General tab, check the option to Use the WSL 2 based engine.
- Install a Linux distribution from the Microsoft Store if you haven't already.
- Open your project in PyCharm.
- Ensure that you have your Dockerfile and docker-compose.yml files in the root directory of your project.
In PyCharm, open the terminal by selecting View > Tool Windows > Terminal. You should see a terminal window at the bottom of the PyCharm interface.
In the terminal, navigate to the directory containing your docker-compose.yml file (if not already there) and run:
Directory: project_name/task_management_api/.
docker-compose buildThis command builds the Docker images according to the instructions in the Dockerfile.
After the build process is complete, run the containers using:
docker-compose upThis will start your Flask app and MySQL database in their respective containers. You should see logs appearing in the terminal as the containers start.
Once the containers are running, you can access your Flask application by navigating to: http://localhost:5000 or http://127.0.0.1:5000 in your web browser.
Check the Docker Containers: In Docker Desktop, you can see the running containers and their status.
Logs: You can monitor the logs in the PyCharm terminal to see the output from your Flask app and database.
When you’re done testing:
- Go to the PyCharm terminal.
- Press Ctrl + C to stop the running containers.
Alternatively, you can stop the containers using:
docker-compose downThis command will stop and remove the containers, networks, and any associated volumes.
Docker Desktop provides a GUI to manage containers, images, networks, and volumes, making it easy to interact with your Docker environment without using the command line.