Skip to content

Commit 22dbad9

Browse files
authored
Merge pull request #144 from livechat/add-custom-header-to-ws-handshake
Allow passing custom header for websocket handshake. Change pre-commi…
2 parents 4d8c3a5 + fbec948 commit 22dbad9

File tree

12 files changed

+85
-35
lines changed

12 files changed

+85
-35
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ repos:
3434
--disable=too-many-locals,
3535
--disable=duplicate-code,
3636
--disable=logging-fstring-interpolation]
37-
- repo: https://gitlab.com/pycqa/flake8
37+
- repo: https://github.com/pycqa/flake8
3838
rev: '3.8.4'
3939
hooks:
4040
- id: flake8

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ All notable changes to this project will be documented in this file.
88
- Added `response_timeout` parameter in `open_connection` methods.
99
- New `get_license_info` method in agent-api v3.5.
1010
- New `update_session` method in agent-api v3.6 (rtm).
11+
- Allow passing custom header for websocket handshake.
1112

1213
### Changed
1314
- Added missing top-level arguments to `update_auto_access` method in configuration-api.
1415
- Updated outdated packages.
16+
- Changed pre-commit flake8 URL
1517

1618
## [0.3.9] - 2024-04-22
1719

livechat/agent/rtm/api/v33.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
''' Module containing Agent RTM API client implementation for v3.3. '''
22

3-
from typing import Any, Optional, Union
3+
from typing import Any, Callable, Optional, Union
44

55
from livechat.utils.helpers import prepare_payload
66
from livechat.utils.structures import AccessToken, RtmResponse
@@ -11,8 +11,13 @@
1111

1212
class AgentRtmV33:
1313
''' Agent RTM API Class containing methods in version 3.3. '''
14-
def __init__(self, url: str):
15-
self.ws = WebsocketClient(url=f'wss://{url}/v3.3/agent/rtm/ws')
14+
def __init__(
15+
self,
16+
url: str,
17+
header: Union[list, dict, Callable, None],
18+
):
19+
self.ws = WebsocketClient(url=f'wss://{url}/v3.3/agent/rtm/ws',
20+
header=header)
1621

1722
def open_connection(self,
1823
origin: dict = None,

livechat/agent/rtm/api/v34.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
''' Module containing Agent RTM API client implementation for v3.4. '''
22

3-
from typing import Any, Optional, Union
3+
from typing import Any, Callable, Optional, Union
44

55
from livechat.utils.helpers import prepare_payload
66
from livechat.utils.structures import AccessToken, RtmResponse
@@ -11,8 +11,13 @@
1111

1212
class AgentRtmV34:
1313
''' Agent RTM API Class containing methods in version 3.4. '''
14-
def __init__(self, url: str):
15-
self.ws = WebsocketClient(url=f'wss://{url}/v3.4/agent/rtm/ws')
14+
def __init__(
15+
self,
16+
url: str,
17+
header: Union[list, dict, Callable, None],
18+
):
19+
self.ws = WebsocketClient(url=f'wss://{url}/v3.4/agent/rtm/ws',
20+
header=header)
1621

1722
def open_connection(self,
1823
origin: dict = None,

livechat/agent/rtm/api/v35.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
''' Module containing Agent RTM API client implementation for v3.5. '''
22

3-
from typing import Any, Optional, Union
3+
from typing import Any, Callable, Optional, Union
44

55
from livechat.utils.helpers import prepare_payload
66
from livechat.utils.structures import AccessToken, RtmResponse
@@ -11,8 +11,13 @@
1111

1212
class AgentRtmV35:
1313
''' Agent RTM API Class containing methods in version 3.5. '''
14-
def __init__(self, url: str):
15-
self.ws = WebsocketClient(url=f'wss://{url}/v3.5/agent/rtm/ws')
14+
def __init__(
15+
self,
16+
url: str,
17+
header: Union[list, dict, Callable, None],
18+
):
19+
self.ws = WebsocketClient(url=f'wss://{url}/v3.5/agent/rtm/ws',
20+
header=header)
1621

1722
def open_connection(self,
1823
origin: dict = None,

livechat/agent/rtm/api/v36.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
''' Module containing Agent RTM API client implementation for v3.6. '''
22

3-
from typing import Any, Optional, Union
3+
from typing import Any, Callable, Optional, Union
44

55
from livechat.utils.helpers import prepare_payload
66
from livechat.utils.structures import AccessToken, RtmResponse
@@ -11,8 +11,15 @@
1111

1212
class AgentRtmV36:
1313
''' Agent RTM API Class containing methods in version 3.6. '''
14-
def __init__(self, url: str):
15-
self.ws = WebsocketClient(url=f'wss://{url}/v3.6/agent/rtm/ws')
14+
def __init__(
15+
self,
16+
url: str,
17+
header: Union[list, dict, Callable, None],
18+
):
19+
self.ws = WebsocketClient(
20+
url=f'wss://{url}/v3.6/agent/rtm/ws',
21+
header=header,
22+
)
1623

1724
def open_connection(self,
1825
origin: dict = None,

livechat/agent/rtm/base.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# pylint: disable=W0613,W0622,C0103,R0913,R0903,W0107,W0221
44
from __future__ import annotations
55

6-
from typing import Union
6+
from typing import Callable, Union
77

88
from livechat.agent.rtm.api.v33 import AgentRtmV33
99
from livechat.agent.rtm.api.v34 import AgentRtmV34
@@ -20,13 +20,16 @@ class AgentRTM:
2020
@staticmethod
2121
def get_client(
2222
version: str = stable_version,
23-
base_url: str = api_url
23+
base_url: str = api_url,
24+
header: Union[list, dict, Callable, None] = None,
2425
) -> Union[AgentRtmV33, AgentRtmV34, AgentRtmV35, AgentRtmV36]:
2526
''' Returns client for specific Agent RTM version.
2627
2728
Args:
2829
version (str): API's version. Defaults to the stable version of API.
2930
base_url (str): API's base url. Defaults to API's production URL.
31+
header (Union[list, dict, Callable, None]): Custom header for websocket handshake.
32+
If the parameter is a callable object, it is called just before the connection attempt.
3033
3134
Returns:
3235
API client object for specified version.
@@ -42,4 +45,4 @@ def get_client(
4245
}.get(version)
4346
if not client:
4447
raise ValueError('Provided version does not exist.')
45-
return client(base_url)
48+
return client(base_url, header)

livechat/customer/rtm/api/v33.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# pylint: disable=C0103,R0903,R0913,W0107,W0231,W0613,W0622
44

5-
from typing import Optional, Union
5+
from typing import Callable, Optional, Union
66

77
from livechat.utils.helpers import prepare_payload
88
from livechat.utils.structures import AccessToken, RtmResponse
@@ -11,12 +11,17 @@
1111

1212
class CustomerRtmV33:
1313
''' Customer RTM API client class in version 3.3. '''
14-
def __init__(self, license_id: str, base_url: str):
14+
def __init__(
15+
self,
16+
license_id: str,
17+
base_url: str,
18+
header: Union[list, dict, Callable, None],
19+
):
1520
if isinstance(license_id, (int, str)):
1621
self.ws = WebsocketClient(
1722
url=
18-
f'wss://{base_url}/v3.3/customer/rtm/ws?license_id={license_id}'
19-
)
23+
f'wss://{base_url}/v3.3/customer/rtm/ws?license_id={license_id}',
24+
header=header)
2025
else:
2126
raise ValueError(
2227
f'Provided `license_id` (`{license_id}`) seems invalid. Websocket connection may not open.'

livechat/customer/rtm/api/v34.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# pylint: disable=C0103,R0903,R0913,W0107,W0231,W0613,W0622
44

5-
from typing import Optional, Union
5+
from typing import Callable, Optional, Union
66

77
from livechat.utils.helpers import prepare_payload
88
from livechat.utils.structures import AccessToken, RtmResponse
@@ -11,12 +11,17 @@
1111

1212
class CustomerRtmV34:
1313
''' Customer RTM API client class in version 3.4. '''
14-
def __init__(self, organization_id: str, base_url: str):
14+
def __init__(
15+
self,
16+
organization_id: str,
17+
base_url: str,
18+
header: Union[list, dict, Callable, None],
19+
):
1520
if isinstance(organization_id, str):
1621
self.ws = WebsocketClient(
1722
url=
18-
f'wss://{base_url}/v3.4/customer/rtm/ws?organization_id={organization_id}'
19-
)
23+
f'wss://{base_url}/v3.5/customer/rtm/ws?organization_id={organization_id}',
24+
header=header)
2025
else:
2126
raise ValueError(
2227
f'Provided `organization_id` (`{organization_id}`) seems invalid. Websocket connection may not open.'

livechat/customer/rtm/api/v35.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# pylint: disable=C0103,R0903,R0913,W0107,W0231,W0613,W0622
44

5-
from typing import Optional, Union
5+
from typing import Callable, Optional, Union
66

77
from livechat.utils.helpers import prepare_payload
88
from livechat.utils.structures import AccessToken, RtmResponse
@@ -11,12 +11,17 @@
1111

1212
class CustomerRtmV35:
1313
''' Customer RTM API client class in version 3.5. '''
14-
def __init__(self, organization_id: str, base_url: str):
14+
def __init__(
15+
self,
16+
organization_id: str,
17+
base_url: str,
18+
header: Union[list, dict, Callable, None],
19+
):
1520
if isinstance(organization_id, str):
1621
self.ws = WebsocketClient(
1722
url=
18-
f'wss://{base_url}/v3.5/customer/rtm/ws?organization_id={organization_id}'
19-
)
23+
f'wss://{base_url}/v3.5/customer/rtm/ws?organization_id={organization_id}',
24+
header=header)
2025
else:
2126
raise ValueError(
2227
f'Provided `organization_id` (`{organization_id}`) seems invalid. Websocket connection may not open.'

0 commit comments

Comments
 (0)