Skip to content

Commit

Permalink
introduce AccessToken dataclass (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
kacperf531 authored Feb 27, 2024
1 parent ee3bb5c commit ee811e1
Show file tree
Hide file tree
Showing 38 changed files with 156 additions and 52 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
- Updated outdated packages.
- 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.
- Enhanced timeouts for the RTM and WEB clients.
- 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.

### Bugfixes
- Enabled instantiation for `CustomerRtmV36` within the 3.6 version of the Customer RTM API.
Expand Down
6 changes: 5 additions & 1 deletion examples/agent_rtm_example.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
''' Agent RTM client example usage. '''

from livechat.agent.rtm.base import AgentRTM
from livechat.utils.structures import AccessToken, TokenType

agent_rtm = AgentRTM.get_client()
agent_rtm.open_connection()
agent_rtm.login(token='<your access token>')

# token can be also passed as a raw string like `Bearer dal:A420qcNvdVS4cRMJP269GfgT1LA`
agent_rtm.login(token=AccessToken(scheme=TokenType.BEARER,
token='dal:A420qcNvdVS4cRMJP269GfgT1LA'))
response = agent_rtm.start_chat(continuous=True)
chat_id = response.payload.get('chat_id')
thread_id = response.payload.get('thread_id')
Expand Down
5 changes: 4 additions & 1 deletion examples/agent_web_example.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
''' Agent WEB client example usage. '''

from livechat.agent.web.base import AgentWeb
from livechat.utils.structures import AccessToken, TokenType

agent_web = AgentWeb.get_client(access_token='<your access token>')
# token can be also passed as a raw string like `Bearer dal:A420qcNvdVS4cRMJP269GfgT1LA`
agent_web = AgentWeb.get_client(access_token=AccessToken(
scheme=TokenType.BEARER, token='dal:A420qcNvdVS4cRMJP269GfgT1LA'))
results = agent_web.start_chat(continuous=True)
chat_id = results.json().get('chat_id')
thread_id = results.json().get('thread_id')
Expand Down
4 changes: 3 additions & 1 deletion examples/configuration_api_example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
''' Configuration API client example usage. '''

from livechat.configuration.base import ConfigurationApi
from livechat.utils.structures import AccessToken, TokenType

# Get list of existing groups.
configuration_api = ConfigurationApi.get_client(token='<your access token>')
configuration_api = ConfigurationApi.get_client(token=AccessToken(
scheme=TokenType.BEARER, token='dal:A420qcNvdVS4cRMJP269GfgT1LA'))
groups = configuration_api.list_groups()
print(groups.json())

Expand Down
4 changes: 3 additions & 1 deletion examples/customer_rtm_example.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
''' Customer RTM client example usage. '''

from livechat.customer.rtm.base import CustomerRTM
from livechat.utils.structures import AccessToken, TokenType

customer_rtm = CustomerRTM.get_client(
organization_id='142cf3ad-5d54-4cf6-8ce1-3773d14d7f3f')
customer_rtm.open_connection()
customer_rtm.login(token='Bearer <your bearer token>')
customer_rtm.login(token=AccessToken(scheme=TokenType.BEARER,
token='dal:A6420cNvdVS4cRMJP269GfgT1LA'))
response = customer_rtm.start_chat(continuous=True)
chat_id = response.payload.get('chat_id')
thread_id = response.payload.get('thread_id')
Expand Down
4 changes: 3 additions & 1 deletion examples/customer_web_example.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
''' Customer WEB client example usage. '''

from livechat.customer.web.base import CustomerWeb
from livechat.utils.structures import AccessToken, TokenType

customer_web = CustomerWeb.get_client(
organization_id='142cf3ad-5d54-4cf6-8ce1-3773d14d7f3f',
access_token='<your access token>')
access_token=AccessToken(scheme=TokenType.BEARER,
token='dal:A6420cNvdVS4cRMJP269GfgT1LA'))
results = customer_web.start_chat(continuous=True)
chat_id = results.json().get('chat_id')
thread_id = results.json().get('thread_id')
Expand Down
4 changes: 3 additions & 1 deletion examples/reports_api_example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
''' Reports API client example usage. '''

from livechat.reports.base import ReportsApi
from livechat.utils.structures import AccessToken, TokenType

# Get number of chats occured during specified period.
reports_api = ReportsApi.get_client(token='<your access token>')
reports_api = ReportsApi.get_client(token=AccessToken(
scheme=TokenType.BEARER, token='dal:A420qcNvdVS4cRMJP269GfgT1LA'))
chats_occured = reports_api.total_chats(filters={
'to': '2020-09-14T23:59:59+02:00',
'from': '2020-09-01T00:00:00+02:00'
Expand Down
8 changes: 5 additions & 3 deletions livechat/agent/rtm/api/v33.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
''' Module containing Agent RTM API client implementation for v3.3. '''

from typing import Any, Optional
from typing import Any, Optional, Union

from livechat.utils.helpers import prepare_payload
from livechat.utils.structures import RtmResponse
from livechat.utils.structures import AccessToken, RtmResponse
from livechat.utils.ws_client import WebsocketClient

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

def login(self,
token: str = None,
token: Union[AccessToken, str] = None,
timezone: str = None,
reconnect: bool = None,
push_notifications: dict = None,
Expand Down Expand Up @@ -822,6 +822,8 @@ def login(self,
RtmResponse: RTM response structure (`request_id`, `action`,
`type`, `success` and `payload` properties)
'''
if token:
token = str(token)
if payload is None:
payload = prepare_payload(locals())
return self.ws.send({'action': 'login', 'payload': payload})
Expand Down
8 changes: 5 additions & 3 deletions livechat/agent/rtm/api/v34.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
''' Module containing Agent RTM API client implementation for v3.4. '''

from typing import Any, Optional
from typing import Any, Optional, Union

from livechat.utils.helpers import prepare_payload
from livechat.utils.structures import RtmResponse
from livechat.utils.structures import AccessToken, RtmResponse
from livechat.utils.ws_client import WebsocketClient

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

def login(self,
token: str = None,
token: Union[AccessToken, str] = None,
timezone: str = None,
reconnect: bool = None,
push_notifications: dict = None,
Expand Down Expand Up @@ -788,6 +788,8 @@ def login(self,
RtmResponse: RTM response structure (`request_id`, `action`,
`type`, `success` and `payload` properties)
'''
if token:
token = str(token)
if payload is None:
payload = prepare_payload(locals())
return self.ws.send({'action': 'login', 'payload': payload})
Expand Down
8 changes: 5 additions & 3 deletions livechat/agent/rtm/api/v35.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
''' Module containing Agent RTM API client implementation for v3.5. '''

from typing import Any, Optional
from typing import Any, Optional, Union

from livechat.utils.helpers import prepare_payload
from livechat.utils.structures import RtmResponse
from livechat.utils.structures import AccessToken, RtmResponse
from livechat.utils.ws_client import WebsocketClient

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

def login(self,
token: str = None,
token: Union[AccessToken, str] = None,
timezone: str = None,
reconnect: bool = None,
push_notifications: dict = None,
Expand Down Expand Up @@ -788,6 +788,8 @@ def login(self,
RtmResponse: RTM response structure (`request_id`, `action`,
`type`, `success` and `payload` properties)
'''
if token:
token = str(token)
if payload is None:
payload = prepare_payload(locals())
return self.ws.send({'action': 'login', 'payload': payload})
Expand Down
8 changes: 5 additions & 3 deletions livechat/agent/rtm/api/v36.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
''' Module containing Agent RTM API client implementation for v3.6. '''

from typing import Any, Optional
from typing import Any, Optional, Union

from livechat.utils.helpers import prepare_payload
from livechat.utils.structures import RtmResponse
from livechat.utils.structures import AccessToken, RtmResponse
from livechat.utils.ws_client import WebsocketClient

# pylint: disable=unused-argument, too-many-arguments, invalid-name, redefined-builtin
Expand Down Expand Up @@ -727,7 +727,7 @@ def unfollow_customer(self,
# Status

def login(self,
token: str = None,
token: Union[AccessToken, str] = None,
timezone: str = None,
reconnect: bool = None,
push_notifications: dict = None,
Expand Down Expand Up @@ -760,6 +760,8 @@ def login(self,
RtmResponse: RTM response structure (`request_id`, `action`,
`type`, `success` and `payload` properties)
'''
if token:
token = str(token)
if payload is None:
payload = prepare_payload(locals())
return self.ws.send({'action': 'login', 'payload': payload})
Expand Down
3 changes: 2 additions & 1 deletion livechat/agent/web/api/v33.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@

from livechat.utils.helpers import prepare_payload
from livechat.utils.http_client import HttpClient
from livechat.utils.structures import AccessToken


class AgentWebV33(HttpClient):
''' Agent Web API Class containing methods in version 3.3. '''
def __init__(self,
access_token: str,
access_token: typing.Union[AccessToken, str],
base_url: str,
http2: bool,
proxies=None,
Expand Down
3 changes: 2 additions & 1 deletion livechat/agent/web/api/v34.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

from livechat.utils.helpers import prepare_payload
from livechat.utils.http_client import HttpClient
from livechat.utils.structures import AccessToken

# pylint: disable=R0903


class AgentWebV34(HttpClient):
''' Agent Web API Class containing methods in version 3.4. '''
def __init__(self,
access_token: str,
access_token: typing.Union[AccessToken, str],
base_url: str,
http2: bool,
proxies=None,
Expand Down
3 changes: 2 additions & 1 deletion livechat/agent/web/api/v35.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

from livechat.utils.helpers import prepare_payload
from livechat.utils.http_client import HttpClient
from livechat.utils.structures import AccessToken

# pylint: disable=R0903


class AgentWebV35(HttpClient):
''' Agent Web API Class containing methods in version 3.5. '''
def __init__(self,
access_token: str,
access_token: typing.Union[AccessToken, str],
base_url: str,
http2: bool,
proxies=None,
Expand Down
3 changes: 2 additions & 1 deletion livechat/agent/web/api/v36.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

from livechat.utils.helpers import prepare_payload
from livechat.utils.http_client import HttpClient
from livechat.utils.structures import AccessToken

# pylint: disable=R0903


class AgentWebV36(HttpClient):
''' Agent Web API Class containing methods in version 3.6. '''
def __init__(self,
access_token: str,
access_token: typing.Union[AccessToken, str],
base_url: str,
http2: bool,
proxies=None,
Expand Down
3 changes: 2 additions & 1 deletion livechat/agent/web/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from livechat.agent.web.api.v35 import AgentWebV35
from livechat.agent.web.api.v36 import AgentWebV36
from livechat.config import CONFIG
from livechat.utils.structures import AccessToken

stable_version = CONFIG.get('stable')
api_url = CONFIG.get('url')
Expand All @@ -22,7 +23,7 @@ class AgentWeb:
API version. '''
@staticmethod
def get_client(
access_token: str,
access_token: Union[AccessToken, str],
version: str = stable_version,
base_url: str = api_url,
http2: bool = False,
Expand Down
5 changes: 4 additions & 1 deletion livechat/configuration/api/v33.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
''' Configuration API module with client class in version 3.3. '''

from typing import Union

import httpx

from livechat.utils.helpers import prepare_payload
from livechat.utils.http_client import HttpClient
from livechat.utils.structures import AccessToken


class ConfigurationApiV33(HttpClient):
''' Configuration API client class in version 3.3. '''
def __init__(self,
token: str,
token: Union[AccessToken, str],
base_url: str,
http2: bool,
proxies=None,
Expand Down
5 changes: 4 additions & 1 deletion livechat/configuration/api/v34.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
''' Configuration API module with client class in version 3.4. '''

from typing import Union

import httpx

from livechat.utils.helpers import prepare_payload
from livechat.utils.http_client import HttpClient
from livechat.utils.structures import AccessToken


class ConfigurationApiV34(HttpClient):
''' Configuration API client class in version 3.4. '''
def __init__(self,
token: str,
token: Union[AccessToken, str],
base_url: str,
http2: bool,
proxies=None,
Expand Down
5 changes: 3 additions & 2 deletions livechat/configuration/api/v35.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
''' Configuration API module with client class in version 3.5. '''

from typing import List
from typing import List, Union

import httpx

from livechat.utils.helpers import prepare_payload
from livechat.utils.http_client import HttpClient
from livechat.utils.structures import AccessToken


class ConfigurationApiV35(HttpClient):
''' Configuration API client class in version 3.5. '''
def __init__(self,
token: str,
token: Union[AccessToken, str],
base_url: str,
http2: bool,
proxies=None,
Expand Down
5 changes: 3 additions & 2 deletions livechat/configuration/api/v36.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
''' Configuration API module with client class in version 3.6. '''

from typing import List
from typing import List, Union

import httpx

from livechat.utils.helpers import prepare_payload
from livechat.utils.http_client import HttpClient
from livechat.utils.structures import AccessToken

# pylint: disable=unused-argument,too-many-arguments,redefined-builtin,invalid-name


class ConfigurationApiV36(HttpClient):
''' Configuration API client class in version 3.6. '''
def __init__(self,
token: str,
token: Union[AccessToken, str],
base_url: str,
http2: bool,
proxies=None,
Expand Down
3 changes: 2 additions & 1 deletion livechat/configuration/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from livechat.configuration.api.v34 import ConfigurationApiV34
from livechat.configuration.api.v35 import ConfigurationApiV35
from livechat.configuration.api.v36 import ConfigurationApiV36
from livechat.utils.structures import AccessToken

stable_version = CONFIG.get('stable')
api_url = CONFIG.get('url')
Expand All @@ -23,7 +24,7 @@ class ConfigurationApi:
API version. '''
@staticmethod
def get_client(
token: str,
token: Union[AccessToken, str],
version: str = stable_version,
base_url: str = api_url,
http2: bool = False,
Expand Down
Loading

0 comments on commit ee811e1

Please sign in to comment.