22from typing import List
33from typing import Optional
44
5+ import requests
6+
57import shellhub .models .base
8+ from shellhub .exceptions import DeviceNotFoundError
69from shellhub .exceptions import ShellHubApiError
710
811
@@ -56,14 +59,11 @@ def __init__(self, api_object: shellhub.models.base.ShellHub, device_json): # t
5659 self .info = ShellHubDeviceInfo (device_json ["info" ])
5760 self .public_key = device_json ["public_key" ]
5861 self .tenant_id = device_json ["tenant_id" ]
59- # TODO: Convert to datetime object
6062 self .last_seen = device_json ["last_seen" ]
6163 self .online = device_json ["online" ]
6264 self .namespace = device_json ["namespace" ]
6365 self .status = device_json ["status" ]
64- # TODO: Convert to datetime object
6566 self .status_updated_at = device_json ["status_updated_at" ]
66- # TODO: Convert to datetime object
6767 self .created_at = device_json ["created_at" ]
6868 self .remote_addr = device_json ["remote_addr" ]
6969 self .tags = device_json ["tags" ]
@@ -74,10 +74,14 @@ def delete(self) -> bool:
7474 if response .status_code == 200 :
7575 return True
7676 elif response .status_code == 404 :
77- raise ShellHubApiError (f"Device { self .uid } not found." )
77+ raise DeviceNotFoundError (f"Device { self .uid } not found." )
7878 else :
79- response .raise_for_status ()
80- return False
79+ try :
80+ response .raise_for_status ()
81+ except requests .exceptions .HTTPError as e :
82+ raise ShellHubApiError (e )
83+ else :
84+ return False
8185
8286 def rename (self , name : Optional [str ] = None ) -> bool :
8387 """
@@ -90,12 +94,16 @@ def rename(self, name: Optional[str] = None) -> bool:
9094 self .name = name
9195 return True
9296 elif response .status_code == 404 :
93- raise ShellHubApiError (f"Device { self .uid } not found." )
97+ raise DeviceNotFoundError (f"Device { self .uid } not found." )
9498 elif response .status_code == 409 :
9599 raise ShellHubApiError (f"Device with name { name } already exists." )
96100 else :
97- response .raise_for_status ()
98- return False
101+ try :
102+ response .raise_for_status ()
103+ except requests .exceptions .HTTPError as e :
104+ raise ShellHubApiError (e )
105+ else :
106+ return False
99107
100108 def accept (self ) -> bool :
101109 if not self .acceptable :
@@ -106,18 +114,26 @@ def accept(self) -> bool:
106114 self .refresh ()
107115 return True
108116 elif response .status_code == 404 :
109- raise ShellHubApiError (f"Device { self .uid } not found." )
117+ raise DeviceNotFoundError (f"Device { self .uid } not found." )
110118 else :
111- response .raise_for_status ()
112- return False
119+ try :
120+ response .raise_for_status ()
121+ except requests .exceptions .HTTPError as e :
122+ raise ShellHubApiError (e )
123+ else :
124+ return False
113125
114126 def refresh (self ) -> None :
115127 response = self ._api .make_request (endpoint = f"/api/devices/{ self .uid } " , method = "GET" )
116128 if response .status_code == 404 :
117- raise ShellHubApiError (f"Device { self .uid } not found." )
118- elif response .status_code != 200 :
119- response .raise_for_status ()
120- self .__init__ (self ._api , response .json ()) # type: ignore
129+ raise DeviceNotFoundError (f"Device { self .uid } not found." )
130+ elif response .status_code == 200 :
131+ self .__init__ (self ._api , response .json ()) # type: ignore
132+ else :
133+ try :
134+ response .raise_for_status ()
135+ except requests .exceptions .HTTPError as e :
136+ raise ShellHubApiError (e )
121137
122138 def __repr__ (self ) -> str :
123139 return (
0 commit comments