22
33import logging
44from abc import ABC , abstractmethod
5- from http import HTTPStatus
65from typing import TYPE_CHECKING , Generic , Literal , TypeVar
76
87from ._product import BaseProduct
9- from ._response import BaseResponse , ErrorResponse , decode_response
8+ from ._response import BaseResponse , decode_response
109
1110if TYPE_CHECKING :
1211 from collections .abc import Sequence
@@ -57,11 +56,7 @@ def send( # noqa: PLR0913
5756 json = {k : v for k , v in payload .items () if v is not None },
5857 )
5958
60- if response .status_code != HTTPStatus .OK :
61- data = decode_response (response .content , type = ErrorResponse )
62- msg = f"failed to send instant message. { response .status_code } { data .error } "
63- raise ValueError (msg )
64-
59+ self ._client .handle_error (response , fail_message = "failed to send instant message" )
6560 data = decode_response (response .content , type = IMSendResponse )
6661 logger .debug ("successfully sent %s message to %r" , self ._api_path [1 :], to )
6762 return data
@@ -78,11 +73,7 @@ def _typing(self, *, to: str, action: Literal["start", "stop"], from_: str | Non
7873 json = {k : v for k , v in payload .items () if v is not None },
7974 )
8075
81- if response .status_code != HTTPStatus .OK :
82- data = decode_response (response .content , type = ErrorResponse )
83- msg = f"failed to { action } { self ._api_path [1 :]} typing indicator. { response .status_code } { data .error } "
84- raise ValueError (msg )
85-
76+ self ._client .handle_error (response , fail_message = f"failed to { action } { self ._api_path [1 :]} typing indicator" )
8677 data = decode_response (response .content , type = IMTypingResponse )
8778 logger .debug ("successfully %s %s typing indicator for %r" , action , self ._api_path [1 :], to )
8879 return data
@@ -113,11 +104,7 @@ def _reactions(
113104 json = {k : v for k , v in payload .items () if v is not None },
114105 )
115106
116- if response .status_code != HTTPStatus .OK :
117- data = decode_response (response .content , type = ErrorResponse )
118- msg = f"failed to { action } { self ._api_path [1 :]} reaction. { response .status_code } { data .error } "
119- raise ValueError (msg )
120-
107+ self ._client .handle_error (response , fail_message = f"failed to { action } { self ._api_path [1 :]} reaction" )
121108 data = decode_response (response .content , type = IMReactionResponse )
122109 logger .debug ("successfully %s %s reaction for %r" , action , self ._api_path [1 :], to )
123110 return data
0 commit comments