Skip to content

Commit 32df5f5

Browse files
authored
Merge pull request #62 from livechat/API-9999-align-responses-from-rtm-and-web
API-9999: Add RtmResponse structure for RTM clients
2 parents 5ecf116 + b10b567 commit 32df5f5

File tree

9 files changed

+302
-159
lines changed

9 files changed

+302
-159
lines changed

examples/agent_rtm_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
agent_rtm = AgentRTM.get_client()
66
agent_rtm.open_connection()
77
agent_rtm.login(token='<your access token>')
8-
results = agent_rtm.start_chat(continuous=True)
9-
chat_id = results['response']['payload']['chat_id']
10-
thread_id = results['response']['payload']['thread_id']
8+
response = agent_rtm.start_chat(continuous=True)
9+
chat_id = response.payload.get('chat_id')
10+
thread_id = response.payload.get('thread_id')
1111
agent_rtm.send_event(chat_id=chat_id,
1212
event={
1313
'type': 'message',

examples/customer_rtm_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
customer_rtm = CustomerRTM.get_client(license_id=12345)
66
customer_rtm.open_connection()
77
customer_rtm.login(token='Bearer <your bearer token>')
8-
results = customer_rtm.start_chat(continuous=True)
9-
chat_id = results['response']['payload']['chat_id']
10-
thread_id = results['response']['payload']['thread_id']
8+
response = customer_rtm.start_chat(continuous=True)
9+
chat_id = response.payload.get('chat_id')
10+
thread_id = response.payload.get('thread_id')
1111
customer_rtm.send_event(chat_id=chat_id,
1212
event={
1313
'type': 'message',

livechat/agent/rtm/client.py

Lines changed: 139 additions & 92 deletions
Large diffs are not rendered by default.

livechat/customer/rtm/client.py

Lines changed: 86 additions & 54 deletions
Large diffs are not rendered by default.

livechat/tests/test_agent_rtm_client.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_client_logs_in_with_token():
4040
client.open_connection()
4141
response = client.login(token='Bearer 10386012')
4242
client.close_connection()
43-
assert response['response']['payload'] == {
43+
assert response.payload == {
4444
'error': {
4545
'type': 'authentication',
4646
'message': 'Invalid access token'
@@ -57,9 +57,23 @@ def test_client_logs_in_with_payload():
5757
'token': 'Bearer 10386012'
5858
})
5959
client.close_connection()
60-
assert response['response']['payload'] == {
60+
assert response.payload == {
6161
'error': {
6262
'type': 'authentication',
6363
'message': 'Invalid access token'
6464
}
6565
}, 'Request was not sent.'
66+
67+
68+
def test_rtm_response_structure():
69+
''' Test if returned `RtmResponse` structure contains expected properties. '''
70+
client = AgentRTM.get_client()
71+
client.open_connection()
72+
response = client.login(token='Bearer 10386012')
73+
client.close_connection()
74+
assert isinstance(response.request_id,
75+
str) and len(response.request_id) >= 1
76+
assert response.action == 'login'
77+
assert response.type == 'response'
78+
assert response.success is False
79+
assert isinstance(response.payload, dict)

livechat/tests/test_customer_rtm_client.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_client_logs_in_with_token():
6161
client.open_connection()
6262
response = client.login(token='Bearer 10386012')
6363
client.close_connection()
64-
assert response['response']['payload'] == {
64+
assert response.payload == {
6565
'error': {
6666
'type': 'authentication',
6767
'message': 'Invalid access token'
@@ -83,9 +83,23 @@ def test_client_logs_in_with_payload():
8383
'token': 'Bearer 10386012'
8484
})
8585
client.close_connection()
86-
assert response['response']['payload'] == {
86+
assert response.payload == {
8787
'error': {
8888
'type': 'authentication',
8989
'message': 'Invalid access token'
9090
}
9191
}, 'Request was not sent.'
92+
93+
94+
def test_rtm_response_structure():
95+
''' Test if returned `RtmResponse` structure contains expected properties. '''
96+
client = CustomerRTM.get_client(organization_id=ORGANIZATION_ID)
97+
client.open_connection()
98+
response = client.login(token='Bearer 10386012')
99+
client.close_connection()
100+
assert isinstance(response.request_id,
101+
str) and len(response.request_id) >= 1
102+
assert response.action == 'login'
103+
assert response.type == 'response'
104+
assert response.success is False
105+
assert isinstance(response.payload, dict)

livechat/tests/test_ws_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def test_websocket_send_and_receive_message():
5858
ws.open()
5959
response = ws.send({'action': 'login', 'payload': {'token': 'Bearer xxx'}})
6060
ws.close()
61-
assert response['response']['payload'] == {
61+
assert response.payload == {
6262
'error': {
6363
'type': 'authentication',
6464
'message': 'Invalid access token'

livechat/utils/structures.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
''' Module containing structures. '''
2+
3+
4+
class RtmResponse:
5+
''' RTM response structure class. '''
6+
def __init__(self, rtm_response: dict):
7+
self.rtm_response = rtm_response
8+
9+
@property
10+
def request_id(self) -> str:
11+
''' `request_id` from the RTM response. '''
12+
return self.rtm_response.get('request_id')
13+
14+
@property
15+
def action(self) -> str:
16+
''' `action` from the RTM response. '''
17+
return self.rtm_response.get('action')
18+
19+
@property
20+
def type(self) -> str:
21+
''' Response `type` from the RTM response. '''
22+
return self.rtm_response.get('type')
23+
24+
@property
25+
def success(self) -> bool:
26+
''' Response `success` state from the RTM response. '''
27+
return self.rtm_response.get('success')
28+
29+
@property
30+
def payload(self) -> dict:
31+
''' `payload` from the RTM response. '''
32+
return self.rtm_response.get('payload')

livechat/utils/ws_client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from websocket import WebSocketApp, WebSocketConnectionClosedException
1414
from websocket._abnf import ABNF
1515

16+
from livechat.utils.structures import RtmResponse
17+
1618

1719
def on_message(ws_client: WebSocketApp, message: str):
1820
''' Custom WebSocketApp handler that inserts new messages in front of `self.messages` list. '''
@@ -67,8 +69,10 @@ def send(self,
6769
data must be utf-8 string or unicode.
6870
opcode (int): operation code of data. default is OPCODE_TEXT.
6971
response_timeout (int): time in seconds to wait for the response.
72+
7073
Returns:
71-
dict: Dictionary with response.
74+
RtmResponse: RTM response structure (`request_id`, `action`,
75+
`type`, `success` and `payload` properties)
7276
'''
7377
request_id = str(random.randint(1, 9999999999))
7478
request.update({'request_id': request_id})
@@ -83,7 +87,7 @@ def send(self,
8387
sleep(0.2)
8488
response_timeout -= 0.2
8589
self.logger.info(f'\nRESPONSE:\n{json.dumps(response, indent=4)}')
86-
return {'response': response}
90+
return RtmResponse(response)
8791

8892
def _wait_till_sock_connected(self, timeout: float = 3) -> NoReturn:
8993
''' Polls until `self.sock` is connected.

0 commit comments

Comments
 (0)