Skip to content

Commit

Permalink
Merge pull request #138 from livechat/Expose_response_timeout_in_open…
Browse files Browse the repository at this point in the history
…_connection_method

Exposed response_timeout parameter in open_connection methods
  • Loading branch information
marcindebski authored Jul 25, 2024
2 parents fa62161 + 51d88d5 commit 75bf5de
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 54 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.

### Added
- New `get_company_details` method in configuration-api v3.6.
- Added `response_timeout` parameter in `open_connection` methods.

### Changed
- Updated outdated packages.
Expand Down
13 changes: 8 additions & 5 deletions livechat/agent/rtm/api/v33.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ def __init__(self, url: str):

def open_connection(self,
origin: dict = None,
ping_timeout: float = 3,
ping_interval: float = 5,
ws_conn_timeout: float = 10,
keep_alive: bool = True) -> None:
ping_timeout: Union[float, int] = 3,
ping_interval: Union[float, int] = 5,
ws_conn_timeout: Union[float, int] = 10,
keep_alive: bool = True,
response_timeout: Union[float, int] = 3) -> None:
''' Opens WebSocket connection.
Args:
Expand All @@ -31,9 +32,11 @@ def open_connection(self,
ws_conn_timeout (int or float): timeout (in seconds) to wait for WebSocket connection,
by default sets to 10 seconds.
keep_alive(bool): Bool which states if connection should be kept, by default sets to `True`.
response_timeout (int or float): timeout (in seconds) to wait for the response,
by default sets to 3 seconds.
'''
self.ws.open(origin, ping_timeout, ping_interval, ws_conn_timeout,
keep_alive)
keep_alive, response_timeout)

def close_connection(self) -> None:
''' Closes WebSocket connection. '''
Expand Down
13 changes: 8 additions & 5 deletions livechat/agent/rtm/api/v34.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ def __init__(self, url: str):

def open_connection(self,
origin: dict = None,
ping_timeout: float = 3,
ping_interval: float = 5,
ws_conn_timeout: float = 10,
keep_alive: bool = True) -> None:
ping_timeout: Union[float, int] = 3,
ping_interval: Union[float, int] = 5,
ws_conn_timeout: Union[float, int] = 10,
keep_alive: bool = True,
response_timeout: Union[float, int] = 3) -> None:
''' Opens WebSocket connection.
Args:
Expand All @@ -31,9 +32,11 @@ def open_connection(self,
ws_conn_timeout (int or float): timeout (in seconds) to wait for WebSocket connection,
by default sets to 10 seconds.
keep_alive(bool): Bool which states if connection should be kept, by default sets to `True`.
response_timeout (int or float): timeout (in seconds) to wait for the response,
by default sets to 3 seconds.
'''
self.ws.open(origin, ping_timeout, ping_interval, ws_conn_timeout,
keep_alive)
keep_alive, response_timeout)

def close_connection(self) -> None:
''' Closes WebSocket connection. '''
Expand Down
13 changes: 8 additions & 5 deletions livechat/agent/rtm/api/v35.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ def __init__(self, url: str):

def open_connection(self,
origin: dict = None,
ping_timeout: float = 3,
ping_interval: float = 5,
ws_conn_timeout: float = 10,
keep_alive: bool = True) -> None:
ping_timeout: Union[float, int] = 3,
ping_interval: Union[float, int] = 5,
ws_conn_timeout: Union[float, int] = 10,
keep_alive: bool = True,
response_timeout: Union[float, int] = 3) -> None:
''' Opens WebSocket connection.
Args:
Expand All @@ -31,9 +32,11 @@ def open_connection(self,
ws_conn_timeout (int or float): timeout (in seconds) to wait for WebSocket connection,
by default sets to 10 seconds.
keep_alive(bool): Bool which states if connection should be kept, by default sets to `True`.
response_timeout (int or float): timeout (in seconds) to wait for the response,
by default sets to 3 seconds.
'''
self.ws.open(origin, ping_timeout, ping_interval, ws_conn_timeout,
keep_alive)
keep_alive, response_timeout)

def close_connection(self) -> None:
''' Closes WebSocket connection. '''
Expand Down
13 changes: 8 additions & 5 deletions livechat/agent/rtm/api/v36.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ def __init__(self, url: str):

def open_connection(self,
origin: dict = None,
ping_timeout: float = 3,
ping_interval: float = 5,
ws_conn_timeout: float = 10,
keep_alive: bool = True) -> None:
ping_timeout: Union[float, int] = 3,
ping_interval: Union[float, int] = 5,
ws_conn_timeout: Union[float, int] = 10,
keep_alive: bool = True,
response_timeout: Union[float, int] = 3) -> None:
''' Opens WebSocket connection.
Args:
Expand All @@ -31,9 +32,11 @@ def open_connection(self,
ws_conn_timeout (int or float): timeout (in seconds) to wait for WebSocket connection,
by default sets to 10 seconds.
keep_alive(bool): Bool which states if connection should be kept, by default sets to `True`.
response_timeout (int or float): timeout (in seconds) to wait for the response,
by default sets to 3 seconds.
'''
self.ws.open(origin, ping_timeout, ping_interval, ws_conn_timeout,
keep_alive)
keep_alive, response_timeout)

def close_connection(self) -> None:
''' Closes WebSocket connection. '''
Expand Down
13 changes: 8 additions & 5 deletions livechat/customer/rtm/api/v33.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ def __init__(self, license_id: str, base_url: str):

def open_connection(self,
origin: dict = None,
ping_timeout: float = 3,
ping_interval: float = 5,
ws_conn_timeout: float = 10,
keep_alive: bool = True) -> None:
ping_timeout: Union[float, int] = 3,
ping_interval: Union[float, int] = 5,
ws_conn_timeout: Union[float, int] = 10,
keep_alive: bool = True,
response_timeout: Union[float, int] = 3) -> None:
''' Opens WebSocket connection.
Args:
Expand All @@ -39,9 +40,11 @@ def open_connection(self,
ws_conn_timeout (int or float): timeout (in seconds) to wait for WebSocket connection,
by default sets to 10 seconds.
keep_alive(bool): Bool which states if connection should be kept, by default sets to `True`.
response_timeout (int or float): timeout (in seconds) to wait for the response,
by default sets to 3 seconds.
'''
self.ws.open(origin, ping_timeout, ping_interval, ws_conn_timeout,
keep_alive)
keep_alive, response_timeout)

def close_connection(self) -> None:
''' Closes WebSocket connection. '''
Expand Down
13 changes: 8 additions & 5 deletions livechat/customer/rtm/api/v34.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ def __init__(self, organization_id: str, base_url: str):

def open_connection(self,
origin: dict = None,
ping_timeout: float = 3,
ping_interval: float = 5,
ws_conn_timeout: float = 10,
keep_alive: bool = True) -> None:
ping_timeout: Union[float, int] = 3,
ping_interval: Union[float, int] = 5,
ws_conn_timeout: Union[float, int] = 10,
keep_alive: bool = True,
response_timeout: Union[float, int] = 3) -> None:
''' Opens WebSocket connection.
Args:
Expand All @@ -39,9 +40,11 @@ def open_connection(self,
ws_conn_timeout (int or float): timeout (in seconds) to wait for WebSocket connection,
by default sets to 10 seconds.
keep_alive(bool): Bool which states if connection should be kept, by default sets to `True`.
response_timeout (int or float): timeout (in seconds) to wait for the response,
by default sets to 3 seconds.
'''
self.ws.open(origin, ping_timeout, ping_interval, ws_conn_timeout,
keep_alive)
keep_alive, response_timeout)

def close_connection(self) -> None:
''' Closes WebSocket connection. '''
Expand Down
13 changes: 8 additions & 5 deletions livechat/customer/rtm/api/v35.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ def __init__(self, organization_id: str, base_url: str):

def open_connection(self,
origin: dict = None,
ping_timeout: float = 3,
ping_interval: float = 5,
ws_conn_timeout: float = 10,
keep_alive: bool = True) -> None:
ping_timeout: Union[float, int] = 3,
ping_interval: Union[float, int] = 5,
ws_conn_timeout: Union[float, int] = 10,
keep_alive: bool = True,
response_timeout: Union[float, int] = 3) -> None:
''' Opens WebSocket connection.
Args:
Expand All @@ -39,9 +40,11 @@ def open_connection(self,
ws_conn_timeout (int or float): timeout (in seconds) to wait for WebSocket connection,
by default sets to 10 seconds.
keep_alive(bool): Bool which states if connection should be kept, by default sets to `True`.
response_timeout (int or float): timeout (in seconds) to wait for the response,
by default sets to 3 seconds.
'''
self.ws.open(origin, ping_timeout, ping_interval, ws_conn_timeout,
keep_alive)
keep_alive, response_timeout)

def close_connection(self) -> None:
''' Closes WebSocket connection. '''
Expand Down
13 changes: 8 additions & 5 deletions livechat/customer/rtm/api/v36.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ def __init__(self, organization_id: str, base_url: str):

def open_connection(self,
origin: dict = None,
ping_timeout: float = 3,
ping_interval: float = 5,
ws_conn_timeout: float = 10,
keep_alive: bool = True) -> None:
ping_timeout: Union[float, int] = 3,
ping_interval: Union[float, int] = 5,
ws_conn_timeout: Union[float, int] = 10,
keep_alive: bool = True,
response_timeout: Union[float, int] = 3) -> None:
''' Opens WebSocket connection.
Args:
Expand All @@ -39,9 +40,11 @@ def open_connection(self,
ws_conn_timeout (int or float): timeout (in seconds) to wait for WebSocket connection,
by default sets to 10 seconds.
keep_alive(bool): Bool which states if connection should be kept, by default sets to `True`.
response_timeout (int or float): timeout (in seconds) to wait for the response,
by default sets to 3 seconds.
'''
self.ws.open(origin, ping_timeout, ping_interval, ws_conn_timeout,
keep_alive)
keep_alive, response_timeout)

def close_connection(self) -> None:
''' Closes WebSocket connection. '''
Expand Down
30 changes: 16 additions & 14 deletions livechat/utils/ws_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import ssl
import threading
from time import sleep
from typing import List, NoReturn
from typing import List, NoReturn, Union

from loguru import logger
from websocket import WebSocketApp, WebSocketConnectionClosedException
Expand All @@ -27,13 +27,15 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.messages: List[dict] = []
self.on_message = on_message
self.response_timeout = None

def open(self,
origin: dict = None,
ping_timeout: float = 3,
ping_interval: float = 5,
ws_conn_timeout: float = 10,
keep_alive: bool = True) -> NoReturn:
ping_timeout: Union[float, int] = 3,
ping_interval: Union[float, int] = 5,
ws_conn_timeout: Union[float, int] = 10,
keep_alive: bool = True,
response_timeout: Union[float, int] = 3) -> NoReturn:
''' Opens websocket connection and keep running forever.
Args:
origin (dict): Specifies origin while creating websocket connection.
Expand All @@ -43,7 +45,10 @@ def open(self,
If set to 0, no ping is sent periodically, by default sets to 5 seconds.
ws_conn_timeout (int or float): timeout (in seconds) to wait for WebSocket connection,
by default sets to 10 seconds.
keep_alive(bool): Bool which states if connection should be kept, by default sets to `True`. '''
keep_alive(bool): Bool which states if connection should be kept, by default sets to `True`.
response_timeout (int or float): timeout (in seconds) to wait for the response,
by default sets to 3 seconds. '''
self.response_timeout = response_timeout
run_forever_kwargs = {
'sslopt': {
'cert_reqs': ssl.CERT_NONE
Expand All @@ -60,17 +65,13 @@ def open(self,
return
self.run_forever(**run_forever_kwargs)

def send(self,
request: dict,
opcode=ABNF.OPCODE_TEXT,
response_timeout=2) -> dict:
def send(self, request: dict, opcode=ABNF.OPCODE_TEXT) -> dict:
'''
Sends message, assigining a random request ID, fetching and returning response(s).
Args:
request (dict): message to send. If you set opcode to OPCODE_TEXT,
data must be utf-8 string or unicode.
opcode (int): operation code of data. default is OPCODE_TEXT.
response_timeout (int): time in seconds to wait for the response.
Returns:
RtmResponse: RTM response structure (`request_id`, `action`,
Expand All @@ -87,13 +88,14 @@ def send(self,
(item
for item in self.messages if item.get('request_id') == request_id
and item.get('type') == 'response'),
None)) and response_timeout > 0:
None)) and self.response_timeout > 0:
sleep(0.2)
response_timeout -= 0.2
self.response_timeout -= 0.2
logger.info(f'\nRESPONSE:\n{json.dumps(response, indent=4)}')
return RtmResponse(response)

def _wait_till_sock_connected(self, timeout: float = 10) -> NoReturn:
def _wait_till_sock_connected(self,
timeout: Union[float, int] = 10) -> NoReturn:
''' Polls until `self.sock` is connected.
Args:
timeout (float): timeout value in seconds, default 10. '''
Expand Down

0 comments on commit 75bf5de

Please sign in to comment.