From fbec9482ef88894a72f50f28dcc6803687364f16 Mon Sep 17 00:00:00 2001 From: zuczkows Date: Fri, 27 Dec 2024 14:53:36 +0100 Subject: [PATCH] Allow passing custom header for websocket handshake. Change pre-commit flake8 URL. --- .pre-commit-config.yaml | 2 +- changelog.md | 2 ++ livechat/agent/rtm/api/v33.py | 11 ++++++++--- livechat/agent/rtm/api/v34.py | 11 ++++++++--- livechat/agent/rtm/api/v35.py | 11 ++++++++--- livechat/agent/rtm/api/v36.py | 13 ++++++++++--- livechat/agent/rtm/base.py | 9 ++++++--- livechat/customer/rtm/api/v33.py | 13 +++++++++---- livechat/customer/rtm/api/v34.py | 13 +++++++++---- livechat/customer/rtm/api/v35.py | 13 +++++++++---- livechat/customer/rtm/api/v36.py | 13 +++++++++---- livechat/customer/rtm/base.py | 9 ++++++--- 12 files changed, 85 insertions(+), 35 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index dd28229..849a14c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -34,7 +34,7 @@ repos: --disable=too-many-locals, --disable=duplicate-code, --disable=logging-fstring-interpolation] -- repo: https://gitlab.com/pycqa/flake8 +- repo: https://github.com/pycqa/flake8 rev: '3.8.4' hooks: - id: flake8 diff --git a/changelog.md b/changelog.md index b66fb3a..e3ff782 100644 --- a/changelog.md +++ b/changelog.md @@ -8,10 +8,12 @@ All notable changes to this project will be documented in this file. - Added `response_timeout` parameter in `open_connection` methods. - New `get_license_info` method in agent-api v3.5. - New `update_session` method in agent-api v3.6 (rtm). +- Allow passing custom header for websocket handshake. ### Changed - Added missing top-level arguments to `update_auto_access` method in configuration-api. - Updated outdated packages. +- Changed pre-commit flake8 URL ## [0.3.9] - 2024-04-22 diff --git a/livechat/agent/rtm/api/v33.py b/livechat/agent/rtm/api/v33.py index 495c01d..320cf00 100644 --- a/livechat/agent/rtm/api/v33.py +++ b/livechat/agent/rtm/api/v33.py @@ -1,6 +1,6 @@ ''' Module containing Agent RTM API client implementation for v3.3. ''' -from typing import Any, Optional, Union +from typing import Any, Callable, Optional, Union from livechat.utils.helpers import prepare_payload from livechat.utils.structures import AccessToken, RtmResponse @@ -11,8 +11,13 @@ class AgentRtmV33: ''' Agent RTM API Class containing methods in version 3.3. ''' - def __init__(self, url: str): - self.ws = WebsocketClient(url=f'wss://{url}/v3.3/agent/rtm/ws') + def __init__( + self, + url: str, + header: Union[list, dict, Callable, None], + ): + self.ws = WebsocketClient(url=f'wss://{url}/v3.3/agent/rtm/ws', + header=header) def open_connection(self, origin: dict = None, diff --git a/livechat/agent/rtm/api/v34.py b/livechat/agent/rtm/api/v34.py index db368b9..02983de 100644 --- a/livechat/agent/rtm/api/v34.py +++ b/livechat/agent/rtm/api/v34.py @@ -1,6 +1,6 @@ ''' Module containing Agent RTM API client implementation for v3.4. ''' -from typing import Any, Optional, Union +from typing import Any, Callable, Optional, Union from livechat.utils.helpers import prepare_payload from livechat.utils.structures import AccessToken, RtmResponse @@ -11,8 +11,13 @@ class AgentRtmV34: ''' Agent RTM API Class containing methods in version 3.4. ''' - def __init__(self, url: str): - self.ws = WebsocketClient(url=f'wss://{url}/v3.4/agent/rtm/ws') + def __init__( + self, + url: str, + header: Union[list, dict, Callable, None], + ): + self.ws = WebsocketClient(url=f'wss://{url}/v3.4/agent/rtm/ws', + header=header) def open_connection(self, origin: dict = None, diff --git a/livechat/agent/rtm/api/v35.py b/livechat/agent/rtm/api/v35.py index 518bd98..52d3ab1 100644 --- a/livechat/agent/rtm/api/v35.py +++ b/livechat/agent/rtm/api/v35.py @@ -1,6 +1,6 @@ ''' Module containing Agent RTM API client implementation for v3.5. ''' -from typing import Any, Optional, Union +from typing import Any, Callable, Optional, Union from livechat.utils.helpers import prepare_payload from livechat.utils.structures import AccessToken, RtmResponse @@ -11,8 +11,13 @@ class AgentRtmV35: ''' Agent RTM API Class containing methods in version 3.5. ''' - def __init__(self, url: str): - self.ws = WebsocketClient(url=f'wss://{url}/v3.5/agent/rtm/ws') + def __init__( + self, + url: str, + header: Union[list, dict, Callable, None], + ): + self.ws = WebsocketClient(url=f'wss://{url}/v3.5/agent/rtm/ws', + header=header) def open_connection(self, origin: dict = None, diff --git a/livechat/agent/rtm/api/v36.py b/livechat/agent/rtm/api/v36.py index 7cdd7e2..8fa82d7 100644 --- a/livechat/agent/rtm/api/v36.py +++ b/livechat/agent/rtm/api/v36.py @@ -1,6 +1,6 @@ ''' Module containing Agent RTM API client implementation for v3.6. ''' -from typing import Any, Optional, Union +from typing import Any, Callable, Optional, Union from livechat.utils.helpers import prepare_payload from livechat.utils.structures import AccessToken, RtmResponse @@ -11,8 +11,15 @@ class AgentRtmV36: ''' Agent RTM API Class containing methods in version 3.6. ''' - def __init__(self, url: str): - self.ws = WebsocketClient(url=f'wss://{url}/v3.6/agent/rtm/ws') + def __init__( + self, + url: str, + header: Union[list, dict, Callable, None], + ): + self.ws = WebsocketClient( + url=f'wss://{url}/v3.6/agent/rtm/ws', + header=header, + ) def open_connection(self, origin: dict = None, diff --git a/livechat/agent/rtm/base.py b/livechat/agent/rtm/base.py index f0535c1..a7937f8 100644 --- a/livechat/agent/rtm/base.py +++ b/livechat/agent/rtm/base.py @@ -3,7 +3,7 @@ # pylint: disable=W0613,W0622,C0103,R0913,R0903,W0107,W0221 from __future__ import annotations -from typing import Union +from typing import Callable, Union from livechat.agent.rtm.api.v33 import AgentRtmV33 from livechat.agent.rtm.api.v34 import AgentRtmV34 @@ -20,13 +20,16 @@ class AgentRTM: @staticmethod def get_client( version: str = stable_version, - base_url: str = api_url + base_url: str = api_url, + header: Union[list, dict, Callable, None] = None, ) -> Union[AgentRtmV33, AgentRtmV34, AgentRtmV35, AgentRtmV36]: ''' Returns client for specific Agent RTM version. Args: version (str): API's version. Defaults to the stable version of API. base_url (str): API's base url. Defaults to API's production URL. + header (Union[list, dict, Callable, None]): Custom header for websocket handshake. + If the parameter is a callable object, it is called just before the connection attempt. Returns: API client object for specified version. @@ -42,4 +45,4 @@ def get_client( }.get(version) if not client: raise ValueError('Provided version does not exist.') - return client(base_url) + return client(base_url, header) diff --git a/livechat/customer/rtm/api/v33.py b/livechat/customer/rtm/api/v33.py index e53fe8f..ebab40b 100644 --- a/livechat/customer/rtm/api/v33.py +++ b/livechat/customer/rtm/api/v33.py @@ -2,7 +2,7 @@ # pylint: disable=C0103,R0903,R0913,W0107,W0231,W0613,W0622 -from typing import Optional, Union +from typing import Callable, Optional, Union from livechat.utils.helpers import prepare_payload from livechat.utils.structures import AccessToken, RtmResponse @@ -11,12 +11,17 @@ class CustomerRtmV33: ''' Customer RTM API client class in version 3.3. ''' - def __init__(self, license_id: str, base_url: str): + def __init__( + self, + license_id: str, + base_url: str, + header: Union[list, dict, Callable, None], + ): if isinstance(license_id, (int, str)): self.ws = WebsocketClient( url= - f'wss://{base_url}/v3.3/customer/rtm/ws?license_id={license_id}' - ) + f'wss://{base_url}/v3.3/customer/rtm/ws?license_id={license_id}', + header=header) else: raise ValueError( f'Provided `license_id` (`{license_id}`) seems invalid. Websocket connection may not open.' diff --git a/livechat/customer/rtm/api/v34.py b/livechat/customer/rtm/api/v34.py index 9809de3..1a8bba7 100644 --- a/livechat/customer/rtm/api/v34.py +++ b/livechat/customer/rtm/api/v34.py @@ -2,7 +2,7 @@ # pylint: disable=C0103,R0903,R0913,W0107,W0231,W0613,W0622 -from typing import Optional, Union +from typing import Callable, Optional, Union from livechat.utils.helpers import prepare_payload from livechat.utils.structures import AccessToken, RtmResponse @@ -11,12 +11,17 @@ class CustomerRtmV34: ''' Customer RTM API client class in version 3.4. ''' - def __init__(self, organization_id: str, base_url: str): + def __init__( + self, + organization_id: str, + base_url: str, + header: Union[list, dict, Callable, None], + ): if isinstance(organization_id, str): self.ws = WebsocketClient( url= - f'wss://{base_url}/v3.4/customer/rtm/ws?organization_id={organization_id}' - ) + f'wss://{base_url}/v3.5/customer/rtm/ws?organization_id={organization_id}', + header=header) else: raise ValueError( f'Provided `organization_id` (`{organization_id}`) seems invalid. Websocket connection may not open.' diff --git a/livechat/customer/rtm/api/v35.py b/livechat/customer/rtm/api/v35.py index 0ccb279..7dc5568 100644 --- a/livechat/customer/rtm/api/v35.py +++ b/livechat/customer/rtm/api/v35.py @@ -2,7 +2,7 @@ # pylint: disable=C0103,R0903,R0913,W0107,W0231,W0613,W0622 -from typing import Optional, Union +from typing import Callable, Optional, Union from livechat.utils.helpers import prepare_payload from livechat.utils.structures import AccessToken, RtmResponse @@ -11,12 +11,17 @@ class CustomerRtmV35: ''' Customer RTM API client class in version 3.5. ''' - def __init__(self, organization_id: str, base_url: str): + def __init__( + self, + organization_id: str, + base_url: str, + header: Union[list, dict, Callable, None], + ): if isinstance(organization_id, str): self.ws = WebsocketClient( url= - f'wss://{base_url}/v3.5/customer/rtm/ws?organization_id={organization_id}' - ) + f'wss://{base_url}/v3.5/customer/rtm/ws?organization_id={organization_id}', + header=header) else: raise ValueError( f'Provided `organization_id` (`{organization_id}`) seems invalid. Websocket connection may not open.' diff --git a/livechat/customer/rtm/api/v36.py b/livechat/customer/rtm/api/v36.py index 5c9e89c..ae9ca50 100644 --- a/livechat/customer/rtm/api/v36.py +++ b/livechat/customer/rtm/api/v36.py @@ -2,7 +2,7 @@ # pylint: disable=C0103,R0903,R0913,W0107,W0231,W0613,W0622 -from typing import Optional, Union +from typing import Callable, Optional, Union from livechat.utils.helpers import prepare_payload from livechat.utils.structures import AccessToken, RtmResponse @@ -11,12 +11,17 @@ class CustomerRtmV36: ''' Customer RTM API client class in version 3.6. ''' - def __init__(self, organization_id: str, base_url: str): + def __init__( + self, + organization_id: str, + base_url: str, + header: Union[list, dict, Callable, None], + ): if isinstance(organization_id, str): self.ws = WebsocketClient( url= - f'wss://{base_url}/v3.6/customer/rtm/ws?organization_id={organization_id}' - ) + f'wss://{base_url}/v3.5/customer/rtm/ws?organization_id={organization_id}', + header=header) else: raise ValueError( f'Provided `organization_id` (`{organization_id}`) seems invalid. Websocket connection may not open.' diff --git a/livechat/customer/rtm/base.py b/livechat/customer/rtm/base.py index 3e739b8..e384866 100644 --- a/livechat/customer/rtm/base.py +++ b/livechat/customer/rtm/base.py @@ -2,7 +2,7 @@ # pylint: disable=C0103,R0903,R0913,W0107,W0231,W0613,W0622 -from typing import Union +from typing import Callable, Union from livechat.config import CONFIG from livechat.customer.rtm.api.v33 import CustomerRtmV33 @@ -21,7 +21,8 @@ def get_client( version: str = stable_version, base_url: str = api_url, license_id: int = None, - organization_id: str = None + organization_id: str = None, + header: Union[list, dict, Callable, None] = None, ) -> Union[CustomerRtmV33, CustomerRtmV34, CustomerRtmV35, CustomerRtmV36]: ''' Returns client for specific Customer RTM version. @@ -30,6 +31,8 @@ def get_client( base_url (str): API's base url. Defaults to API's production URL. license_id (int): License ID. Required to use for API version <= 3.3. organization_id (str): Organization ID, replaced license ID in v3.4. + header (Union[list, dict, Callable, None]): Custom header for websocket handshake. + If the parameter is a callable object, it is called just before the connection attempt. Returns: API client object for specified version. @@ -62,5 +65,5 @@ def get_client( }, }.get(version) if client: - return client(**client_kwargs) + return client(**client_kwargs, header=header) raise ValueError('Provided version does not exist.')