The application provides a cloud-based solution for managing IoT devices, offering a reliable and scalable API for interacting with these devices. This project includes CRUD functionality for device management, enabling users to add, update, retrieve, and delete devices, thus supporting their entire lifecycle. The application is deployed with Docker, ensuring ease of installation and portability, as well as a stable environment for interacting with the PostgreSQL database.
- Python
- Aiohttp
- PostgreSQL
- Peewee
- Docker
- Pytest
Additional libraries are specified in the requirements.txt
file.
In this section, we will deploy the project on PC using Docker and Docker Compose.
Create a root directory on your computer, then open it in your code editor or terminal.
Next, write this command into the command line:
git clone https://github.com/S0fft/Device-Cloud-Hub.git .
You will see the project files appear in your directory.
Afterwards, launch the already installed Docker Desktop. Next, while it is running, you can return to the editor or terminal and enter the following command in the terminal:
docker-compose up --build
This command "collects and runs all the code", allowing you to interact with the project through a browser.
Now the project is already running on your computer, and is available on port 8000. Go to this address to open it:
http://localhost:8000/
Thus, we have run the project locally on computer.
Next, let's look at the functionality of this small project. Implemented CRUD keeping REST architecture in mind.
The goal of the assignment was to implement CRUD for Devices, which was done.
Below are the routes of this service and what request methods they support:
http://localhost:8000/devices/
This route supports: GET (All Devices) and POST (the request body must contain a new object that matches the validator, otherwise there will be an error)
http://localhost:8000/devices/{id}/
This route supports: GET (by ID) to get the item, PUT for major update, PATCH for minor update, DELETE - to completely remove the item. The routes follow REST architecture.
Important Note: After deploying a project on a computer, the database is empty, therefore, to retrieve devices via the API, you must create them using the POST method, respecting all database fields. Otherwise, you will receive empty JSON!
{
"name": "Smart Thermostat",
"device_type": "Thermostat",
"login": "thermo_01",
"password": "securePassword123",
"location_id": 2,
"api_user_id": 1
}
You can use all of the above addresses in Postman (or other similar tools) by sending requests with different methods (GET, POST, PUT, PATCH, DELETE). This gives you a full opportunity to test the API.
As mentioned before, you can run this project in the browser, but in this case, you will not be able to make POST, PUT, PATCH and DELETE requests. This is because a standard browser, without plugins, does not provide such capabilities when working with Aiohttp.
Therefore, it is highly recommended to use Postman.
This Aiohttp based API is asynchronous, but it does not take full advantage of it's capabilities due to the synchronous use of the Peewee ORM. To take full advantage of asynchrony, we must use an asynchronous ORM such as Tortoise-ORM or GINO.
Even though all functions in the code are defined as asynchronous (async def), they call synchronous Peewee ORM methods to work with the database. This blocks the main thread of execution while the database executes queries, which violates the core principle of asynchronous programming - not to block the thread of execution.
To improve our code, we can use an asynchronous ORM such as Tortoise-ORM, which fully supports asynchronous operations.
These commands will help you deploy the project locally.
Important Note: The project is configured to work with Docker. If you use this deployment approach, you need to change the configuration in the .env and app.py files before starting the server. Specifically, you should: Uncomment the necessary commands that are already commented out and replace mutually exclusive lines as needed. These lines are marked in the code.
Otherwise, you will receive an error!
app.py:
if __name__ == '__main__':
db.connect()
db_setup()
# web.run_app(app, host="0.0.0.0", port=8080) # Docker - MUST BE COMMENTED
web.run_app(app, host='127.0.0.1', port=8080) # Local - MUST BE UNCOMMENTED
.env:
DB_NAME=gma_task
DB_USER=postgres
DB_PASSWORD=admin
# DB_HOST=db # Docker - MUST BE COMMENTED
DB_HOST=localhost # Local - MUST BE UNCOMMENTED
DB_PORT=5432
In this approach, the service routes are different, they are listed below.
http://127.0.0.1:8080/devices/
http://127.0.0.1:8080/devices/{id}/
The functionality is the same, the HOST and PORT of the service are just different.
To begin, install: Python | PostgreSQL
Links are provided to the latest versions of the tools.
All the same, сreate a root directory on your computer, then open it in your code editor or terminal.
Next, write this command into the command line:
git clone https://github.com/S0fft/Device-Cloud-Hub.git .
You will see the project files appear in your directory. After, continue to enter the following commands.
Create a virtual environment:
python -m venv .venv
And activate it:
.venv\Scripts\Activate
Next, install packages:
python.exe -m pip install --upgrade pip
pip install -r requirements.txt
Then, run server:
python app.py
These commands do the same thing as described above, only on UNIX systems.
Before this, the code must be modified as indicated in the note above!
python3 -m venv ../venv
source ../venv/bin/activate
pip3 install --upgrade pip
pip3 install -r requirements.txt
python3 app.py