-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
104 lines (81 loc) · 3.06 KB
/
client.py
File metadata and controls
104 lines (81 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import logging
from dataclasses import dataclass
from typing import Optional
import requests
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@dataclass
class TodoRecord:
content: str
is_complete: bool
# HTTP client for interacting with the to-do server
class TodoClient:
def __init__(self, server_url: str = "http://localhost:5000"):
self.server_url = server_url
# Get a to-do record by its ID
def get(self, id: int) -> Optional[TodoRecord]:
response = requests.get(f"{self.server_url}/{id}")
if response.status_code == 200:
json = response.json()
logger.info("got json: %s", json)
return TodoRecord(
content=json["content"],
is_complete=json["is_complete"]
)
elif response.status_code == 404:
return None
else:
raise Exception(f"Unexpected status code: {response.status_code}")
# Create a to-do record
def create(self, todo_record: TodoRecord) -> int:
response = requests.post(self.server_url, json={
"content": todo_record.content,
"is_complete": todo_record.is_complete,
})
if response.status_code == 200:
json = response.json()
logger.info("got json: %s", json)
return json["id"]
else:
raise Exception(f"Unexpected status code: {response.status_code}")
# Delete a to-do record
def delete(self, id: int) -> bool:
response = requests.delete(f"{self.server_url}/{id}")
return response.status_code == 200
def main():
# create the client
client = TodoClient()
# check does not exist for -1
non_existing_id = -1
does_not_exist_yet = client.get(non_existing_id)
if does_not_exist_yet is not None:
logger.error(f"{non_existing_id} should never exist!")
else:
logger.info(f"As expected, received null from server for {non_existing_id}")
# create a new to-do
todo_record = TodoRecord("complete a coding test", False)
new_id = client.create(todo_record)
logger.info(f"Created to-do {new_id}")
# fetch the new to-do
fetched = client.get(new_id)
# check the fetched to-do matches the to-do we created
if fetched is None:
logger.error(f"{new_id} is null despite us just creating it")
elif fetched != todo_record:
logger.error(f"To-do {new_id} does not match what we inserted")
else:
logger.info(f"Created and fetched to-do {new_id} as expected")
# delete the to-do
success = client.delete(new_id)
if not success:
logger.error(f"Delete failed for id {new_id}")
else:
logger.info(f"Delete success for id {new_id}")
# check we can no longer fetch the deleted to-do
deleted_record = client.get(new_id)
if deleted_record is not None:
logger.error(f"Delete failed for id {new_id} - to-do still exists!")
else:
logger.info(f"As expected, was unable to get to-do {new_id} after deletion")
if __name__ == '__main__':
main()