11from typing import NoReturn , Optional
22
3- from whatsapp_api_client_python .API import GreenApi
3+ from whatsapp_api_client_python .API import GreenApi , Response
44
55from .manager .router import Router
66
@@ -18,7 +18,7 @@ def __init__(
1818 if not settings :
1919 self ._update_settings ()
2020 else :
21- self .api .account .setSettings (settings )
21+ self .__validate_response ( self . api .account .setSettings (settings ) )
2222
2323 if delete_notifications_at_startup :
2424 self ._delete_notifications_at_startup ()
@@ -29,23 +29,27 @@ def run_forever(self) -> Optional[NoReturn]:
2929 while True :
3030 try :
3131 response = self .api .receiving .receiveNotification ()
32- if response . error :
33- raise GreenAPIError (response . error )
32+
33+ self . __validate_response (response )
3434
3535 if not response .data :
3636 continue
3737 response = response .data
3838
3939 self .router .route_event (response ["body" ])
4040
41- self .api .receiving .deleteNotification (response ["receiptId" ])
41+ self .__validate_response (
42+ self .api .receiving .deleteNotification (
43+ response ["receiptId" ]
44+ )
45+ )
4246 except KeyboardInterrupt :
4347 break
4448
4549 def _update_settings (self ) -> Optional [NoReturn ]:
4650 settings = self .api .account .getSettings ()
47- if settings . error :
48- raise GreenAPIError (settings . error )
51+
52+ self . __validate_response (settings )
4953
5054 response = settings .data
5155
@@ -57,22 +61,37 @@ def _update_settings(self) -> Optional[NoReturn]:
5761 and outgoing_message_webhook == "no"
5862 and outgoing_api_message_webhook == "no"
5963 ):
60- self .api .account .setSettings ({
61- "incomingWebhook" : "yes" ,
62- "outgoingMessageWebhook" : "yes" ,
63- "outgoingAPIMessageWebhook" : "yes"
64- })
64+ self .__validate_response (
65+ self .api .account .setSettings ({
66+ "incomingWebhook" : "yes" ,
67+ "outgoingMessageWebhook" : "yes" ,
68+ "outgoingAPIMessageWebhook" : "yes"
69+ })
70+ )
6571
6672 def _delete_notifications_at_startup (self ) -> Optional [NoReturn ]:
6773 while True :
6874 response = self .api .receiving .receiveNotification ()
69- if response . error :
70- raise GreenAPIError (response . error )
75+
76+ self . __validate_response (response )
7177
7278 if not response .data :
7379 break
7480
75- self .api .receiving .deleteNotification (response .data ["receiptId" ])
81+ self .__validate_response (
82+ self .api .receiving .deleteNotification (
83+ response .data ["receiptId" ]
84+ )
85+ )
86+
87+ @staticmethod
88+ def __validate_response (response : Response ) -> Optional [NoReturn ]:
89+ if response .code != 200 :
90+ if response .error :
91+ raise GreenAPIError (response .error )
92+ raise GreenAPIError (
93+ f"GreenAPI error occurred with status code { response .code } "
94+ )
7695
7796
7897class GreenAPI (GreenApi ):
0 commit comments