Skip to content

Commit 2105438

Browse files
committed
Add message_type and space_id to fetch_history endpoint
1 parent 1109fad commit 2105438

File tree

7 files changed

+119
-8
lines changed

7 files changed

+119
-8
lines changed

pubnub/endpoints/fetch_messages.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ def include_space_id(self, include_space_id):
8585
def custom_params(self):
8686
params = {
8787
'max': int(self._count),
88-
'include_message_type': "true" if self._include_message_type else "false",
88+
'include_type': 'true' if self._include_message_type else 'false',
89+
'include_message_type': 'true' if self._include_message_type else 'false',
8990
}
9091

9192
if self._start is not None:
@@ -163,6 +164,8 @@ def create_response(self, envelope): # pylint: disable=W0221
163164
return PNFetchMessagesResult.from_json(
164165
json_input=envelope,
165166
include_message_actions=self._include_message_actions,
167+
include_message_type=self._include_message_type,
168+
include_space_id=self._include_space_id,
166169
start_timetoken=self._start,
167170
end_timetoken=self._end)
168171

pubnub/models/consumer/history.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from pubnub.models.consumer.message_type import PNMessageType
12
class PNHistoryResult(object):
23
def __init__(self, messages, start_timetoken, end_timetoken):
34
self.messages = messages
@@ -63,7 +64,8 @@ def __str__(self):
6364
return "Fetch messages result for range %d..%d" % (self.start_timetoken, self.end_timetoken)
6465

6566
@classmethod
66-
def from_json(cls, json_input, include_message_actions=False, start_timetoken=None, end_timetoken=None):
67+
def from_json(cls, json_input, include_message_actions=False, include_message_type=False, include_space_id=False,
68+
start_timetoken=None, end_timetoken=None):
6769
channels = {}
6870

6971
for key, entry in json_input['channels'].items():
@@ -84,6 +86,14 @@ def from_json(cls, json_input, include_message_actions=False, start_timetoken=No
8486
else:
8587
message.actions = {}
8688

89+
if include_message_type:
90+
message.message_type = PNMessageType.from_response(
91+
user_type=item['type'] if 'type' in item.keys() else None,
92+
internal_type=item['message_type'])
93+
94+
if include_space_id:
95+
message.space_id = item['space_id']
96+
8797
channels[key].append(message)
8898

8999
return PNFetchMessagesResult(

pubnub/models/consumer/message_type.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
class PNMessageType:
2-
_internal_type: str = 0
2+
_internal_type: str = None
33
_user_type: str = None
4+
_type_mapping = {
5+
'None': 'message',
6+
'0': 'message',
7+
'1': 'signal',
8+
'2': 'object',
9+
'3': 'message_action',
10+
'4': 'file',
11+
}
412

513
def __init__(self, user_type: str = None) -> None:
614
self._user_type = user_type
715

816
def set_internal_type(self, internal_type: str):
9-
self._internal_type = internal_type
17+
self._internal_type = self._type_mapping[str(internal_type)]
18+
return self
1019

1120
def from_response(user_type: str = None, internal_type: str = None):
12-
message_type = PNMessageType(user_type)
13-
if internal_type is not None:
14-
message_type.set_internal_type(internal_type)
15-
return message_type
21+
return PNMessageType(user_type).set_internal_type(internal_type)
1622

1723
def __str__(self) -> str:
1824
return self._user_type if self._user_type is not None else str(self._internal_type)
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import requests
2+
3+
from tests.acceptance import MOCK_SERVER_URL, CONTRACT_INIT_ENDPOINT, CONTRACT_EXPECT_ENDPOINT
4+
5+
6+
def before_scenario(context, feature):
7+
for tag in feature.tags:
8+
if "contract" in tag:
9+
_, contract_name = tag.split("=")
10+
response = requests.get(MOCK_SERVER_URL + CONTRACT_INIT_ENDPOINT + contract_name)
11+
assert response
12+
13+
14+
def after_scenario(context, feature):
15+
for tag in feature.tags:
16+
if "contract" in tag:
17+
response = requests.get(MOCK_SERVER_URL + CONTRACT_EXPECT_ENDPOINT)
18+
assert response
19+
20+
response_json = response.json()
21+
22+
assert not response_json["expectations"]["failed"]
23+
assert not response_json["expectations"]["pending"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from behave import given
2+
from tests.helper import pnconf_demo_copy
3+
from pubnub.pubnub import PubNub
4+
5+
6+
@given('the demo keyset with enabled storage')
7+
def step_impl(context):
8+
config = pnconf_demo_copy()
9+
config.origin = "localhost:8090"
10+
config.ssl = False
11+
pubnub_instance = PubNub(config)
12+
context.peer = pubnub_instance
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from behave import then
2+
from pubnub.models.consumer.history import PNFetchMessagesResult
3+
4+
5+
@then("I receive a successful response")
6+
def step_impl(context):
7+
assert context.status.error is None
8+
assert type(context.result) == PNFetchMessagesResult
9+
10+
11+
@then("history response contains messages with '{type1}' and '{type2}' message types")
12+
def step_impl(context, type1, type2):
13+
assert all(str(item.message_type) in [type1, type2] for item in context.messages)
14+
15+
16+
@then('history response contains messages with message types')
17+
def step_impl(context):
18+
assert all('message_type' in item.__dict__.keys() for item in context.messages)
19+
20+
21+
@then('history response contains messages without message types')
22+
def step_impl(context):
23+
assert all('message_type' not in item.__dict__.keys() for item in context.messages)
24+
25+
26+
@then('history response contains messages without space ids')
27+
def step_impl(context):
28+
assert all('space_id' not in item.__dict__.keys() for item in context.messages)
29+
30+
31+
@then('history response contains messages with space ids')
32+
def step_impl(context):
33+
assert all('space_id' in item.__dict__.keys() for item in context.messages)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from behave import when
2+
3+
4+
@when("I fetch message history for '{channel}' channel")
5+
def step_impl(context, channel):
6+
envelope = context.peer.fetch_messages().channels(channel).include_message_type(True).sync()
7+
context.status = envelope.status
8+
context.result = envelope.result
9+
context.messages = envelope.result.channels[channel]
10+
11+
12+
@when("I fetch message history with '{flag}' set to '{value}' for '{channel}' channel")
13+
def step_impl(context, flag, value, channel):
14+
request = context.peer.fetch_messages().channels(channel)
15+
value = True if value == 'true' else False
16+
if flag == 'includeMessageType':
17+
request.include_message_type(value)
18+
if flag == 'includeSpaceId':
19+
request.include_space_id(value)
20+
21+
envelope = request.sync()
22+
context.status = envelope.status
23+
context.result = envelope.result
24+
context.messages = envelope.result.channels[channel]

0 commit comments

Comments
 (0)