Search⌘ K

The CLI

Explore how to update the CLI tool to implement filtering by various attributes in a clean architecture Python project. Learn to apply single and multiple filters on data requests, test the updated code, and understand input validation for better error management.

Updating the CLI file

At this point, fixing the CLI is extremely simple. We need to imitate what we did for the HTTP server, only without considering the filters because they weren’t part of the command-line tool.

Python 3.5
#!/usr/bin/env python
from rentomatic.repository.memrepo import MemRepo
from rentomatic.use_cases.room_list import room_list_use_case
from rentomatic.requests.room_list import build_room_list_request
rooms = [
{
"code": "f853578c-fc0f-4e65-81b8-566c5dffa35a",
"size": 215,
"price": 39,
"longitude": -0.09998975,
"latitude": 51.75436293,
},
{
"code": "fe2c3195-aeff-487a-a08f-e0bdc0ec6e9a",
"size": 405,
"price": 66,
"longitude": 0.18228006,
"latitude": 51.74640997,
},
{
"code": "913694c6-435a-4366-ba0d-da5334a611b2",
"size": 56,
"price": 60,
"longitude": 0.27891577,
"latitude": 51.45994069,
},
{
"code": "eed76e77-55c1-41ce-985d-ca49bf6c0585",
"size": 93,
"price": 48,
"longitude": 0.33894476,
"latitude": 51.39916678,
},
]
url_request = requests.get('https://localhost:5000/rooms')
request = build_room_list_request(url_request)
repo = MemRepo(rooms)
response = room_list_use_case(repo, request)
print([room.to_dict() for room in response.value])

In line 38, we get the inputs from the URL using the GET method. In line 39, we pass these inputs to build_room_list_request(url_request) to apply the filter on the inputs given in the URL. If we skip line 38, we’ll ...