Skip to content

Commit ee811e1

Browse files
authored
introduce AccessToken dataclass (#128)
1 parent ee3bb5c commit ee811e1

38 files changed

+156
-52
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
1010
- Updated outdated packages.
1111
- Enhanced error logging for improved troubleshooting: Automatically includes response headers in the log for server errors, providing detailed information (such as x-debug-id) for more effective issue diagnosis.
1212
- Enhanced timeouts for the RTM and WEB clients.
13+
- Introduced `AccessToken` structure which allows keeping token type in separate field. Previous way of passing tokens as a string of format `type: token` remains supported for backwards compatibility.
1314

1415
### Bugfixes
1516
- Enabled instantiation for `CustomerRtmV36` within the 3.6 version of the Customer RTM API.

examples/agent_rtm_example.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
''' Agent RTM client example usage. '''
22

33
from livechat.agent.rtm.base import AgentRTM
4+
from livechat.utils.structures import AccessToken, TokenType
45

56
agent_rtm = AgentRTM.get_client()
67
agent_rtm.open_connection()
7-
agent_rtm.login(token='<your access token>')
8+
9+
# token can be also passed as a raw string like `Bearer dal:A420qcNvdVS4cRMJP269GfgT1LA`
10+
agent_rtm.login(token=AccessToken(scheme=TokenType.BEARER,
11+
token='dal:A420qcNvdVS4cRMJP269GfgT1LA'))
812
response = agent_rtm.start_chat(continuous=True)
913
chat_id = response.payload.get('chat_id')
1014
thread_id = response.payload.get('thread_id')

examples/agent_web_example.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
''' Agent WEB client example usage. '''
22

33
from livechat.agent.web.base import AgentWeb
4+
from livechat.utils.structures import AccessToken, TokenType
45

5-
agent_web = AgentWeb.get_client(access_token='<your access token>')
6+
# token can be also passed as a raw string like `Bearer dal:A420qcNvdVS4cRMJP269GfgT1LA`
7+
agent_web = AgentWeb.get_client(access_token=AccessToken(
8+
scheme=TokenType.BEARER, token='dal:A420qcNvdVS4cRMJP269GfgT1LA'))
69
results = agent_web.start_chat(continuous=True)
710
chat_id = results.json().get('chat_id')
811
thread_id = results.json().get('thread_id')

examples/configuration_api_example.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
''' Configuration API client example usage. '''
22

33
from livechat.configuration.base import ConfigurationApi
4+
from livechat.utils.structures import AccessToken, TokenType
45

56
# Get list of existing groups.
6-
configuration_api = ConfigurationApi.get_client(token='<your access token>')
7+
configuration_api = ConfigurationApi.get_client(token=AccessToken(
8+
scheme=TokenType.BEARER, token='dal:A420qcNvdVS4cRMJP269GfgT1LA'))
79
groups = configuration_api.list_groups()
810
print(groups.json())
911

examples/customer_rtm_example.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
''' Customer RTM client example usage. '''
22

33
from livechat.customer.rtm.base import CustomerRTM
4+
from livechat.utils.structures import AccessToken, TokenType
45

56
customer_rtm = CustomerRTM.get_client(
67
organization_id='142cf3ad-5d54-4cf6-8ce1-3773d14d7f3f')
78
customer_rtm.open_connection()
8-
customer_rtm.login(token='Bearer <your bearer token>')
9+
customer_rtm.login(token=AccessToken(scheme=TokenType.BEARER,
10+
token='dal:A6420cNvdVS4cRMJP269GfgT1LA'))
911
response = customer_rtm.start_chat(continuous=True)
1012
chat_id = response.payload.get('chat_id')
1113
thread_id = response.payload.get('thread_id')

examples/customer_web_example.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
''' Customer WEB client example usage. '''
22

33
from livechat.customer.web.base import CustomerWeb
4+
from livechat.utils.structures import AccessToken, TokenType
45

56
customer_web = CustomerWeb.get_client(
67
organization_id='142cf3ad-5d54-4cf6-8ce1-3773d14d7f3f',
7-
access_token='<your access token>')
8+
access_token=AccessToken(scheme=TokenType.BEARER,
9+
token='dal:A6420cNvdVS4cRMJP269GfgT1LA'))
810
results = customer_web.start_chat(continuous=True)
911
chat_id = results.json().get('chat_id')
1012
thread_id = results.json().get('thread_id')

examples/reports_api_example.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
''' Reports API client example usage. '''
22

33
from livechat.reports.base import ReportsApi
4+
from livechat.utils.structures import AccessToken, TokenType
45

56
# Get number of chats occured during specified period.
6-
reports_api = ReportsApi.get_client(token='<your access token>')
7+
reports_api = ReportsApi.get_client(token=AccessToken(
8+
scheme=TokenType.BEARER, token='dal:A420qcNvdVS4cRMJP269GfgT1LA'))
79
chats_occured = reports_api.total_chats(filters={
810
'to': '2020-09-14T23:59:59+02:00',
911
'from': '2020-09-01T00:00:00+02:00'

livechat/agent/rtm/api/v33.py

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

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

55
from livechat.utils.helpers import prepare_payload
6-
from livechat.utils.structures import RtmResponse
6+
from livechat.utils.structures import AccessToken, RtmResponse
77
from livechat.utils.ws_client import WebsocketClient
88

99
# pylint: disable=unused-argument, too-many-arguments, invalid-name, redefined-builtin
@@ -789,7 +789,7 @@ def unfollow_customer(self,
789789
# Status
790790

791791
def login(self,
792-
token: str = None,
792+
token: Union[AccessToken, str] = None,
793793
timezone: str = None,
794794
reconnect: bool = None,
795795
push_notifications: dict = None,
@@ -822,6 +822,8 @@ def login(self,
822822
RtmResponse: RTM response structure (`request_id`, `action`,
823823
`type`, `success` and `payload` properties)
824824
'''
825+
if token:
826+
token = str(token)
825827
if payload is None:
826828
payload = prepare_payload(locals())
827829
return self.ws.send({'action': 'login', 'payload': payload})

livechat/agent/rtm/api/v34.py

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

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

55
from livechat.utils.helpers import prepare_payload
6-
from livechat.utils.structures import RtmResponse
6+
from livechat.utils.structures import AccessToken, RtmResponse
77
from livechat.utils.ws_client import WebsocketClient
88

99
# pylint: disable=unused-argument, too-many-arguments, invalid-name, redefined-builtin
@@ -755,7 +755,7 @@ def unfollow_customer(self,
755755
# Status
756756

757757
def login(self,
758-
token: str = None,
758+
token: Union[AccessToken, str] = None,
759759
timezone: str = None,
760760
reconnect: bool = None,
761761
push_notifications: dict = None,
@@ -788,6 +788,8 @@ def login(self,
788788
RtmResponse: RTM response structure (`request_id`, `action`,
789789
`type`, `success` and `payload` properties)
790790
'''
791+
if token:
792+
token = str(token)
791793
if payload is None:
792794
payload = prepare_payload(locals())
793795
return self.ws.send({'action': 'login', 'payload': payload})

livechat/agent/rtm/api/v35.py

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

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

55
from livechat.utils.helpers import prepare_payload
6-
from livechat.utils.structures import RtmResponse
6+
from livechat.utils.structures import AccessToken, RtmResponse
77
from livechat.utils.ws_client import WebsocketClient
88

99
# pylint: disable=unused-argument, too-many-arguments, invalid-name, redefined-builtin
@@ -755,7 +755,7 @@ def unfollow_customer(self,
755755
# Status
756756

757757
def login(self,
758-
token: str = None,
758+
token: Union[AccessToken, str] = None,
759759
timezone: str = None,
760760
reconnect: bool = None,
761761
push_notifications: dict = None,
@@ -788,6 +788,8 @@ def login(self,
788788
RtmResponse: RTM response structure (`request_id`, `action`,
789789
`type`, `success` and `payload` properties)
790790
'''
791+
if token:
792+
token = str(token)
791793
if payload is None:
792794
payload = prepare_payload(locals())
793795
return self.ws.send({'action': 'login', 'payload': payload})

0 commit comments

Comments
 (0)