Skip to content

Commit 42ed093

Browse files
authored
Add channel update partial (#52)
1 parent 156b076 commit 42ed093

5 files changed

Lines changed: 55 additions & 0 deletions

File tree

stream_chat/async_chat/channel.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ async def update(self, channel_data, update_message=None):
121121
payload = {"data": channel_data, "message": update_message}
122122
return await self.client.post(self.url, data=payload)
123123

124+
async def update_partial(self, to_set=None, to_unset=None):
125+
"""
126+
Update channel partially
127+
128+
:param to_set: a dictionary of key/value pairs to set or to override
129+
:param to_unset: a list of keys to clear
130+
"""
131+
payload = {"set": to_set or {}, "unset": to_unset or []}
132+
return await self.client.patch(self.url, data=payload)
133+
124134
async def delete(self):
125135
"""
126136
Delete the channel. Messages are permanently removed.

stream_chat/base/channel.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ def update(self, channel_data, update_message=None):
113113
"""
114114
pass
115115

116+
@abc.abstractmethod
117+
def update_partial(self, to_set=None, to_unset=None):
118+
"""
119+
Update channel partially
120+
121+
:param to_set: a dictionary of key/value pairs to set or to override
122+
:param to_unset: a list of keys to clear
123+
"""
124+
pass
125+
116126
@abc.abstractmethod
117127
def delete(self):
118128
"""

stream_chat/channel.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ def update(self, channel_data, update_message=None):
119119
payload = {"data": channel_data, "message": update_message}
120120
return self.client.post(self.url, data=payload)
121121

122+
def update_partial(self, to_set=None, to_unset=None):
123+
"""
124+
Update channel partially
125+
126+
:param to_set: a dictionary of key/value pairs to set or to override
127+
:param to_unset: a list of keys to clear
128+
"""
129+
payload = {"set": to_set or {}, "unset": to_unset or []}
130+
return self.client.patch(self.url, data=payload)
131+
122132
def delete(self):
123133
"""
124134
Delete the channel. Messages are permanently removed.

stream_chat/tests/async_chat/test_channel.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ async def test_update(self, event_loop, channel):
6262
assert "channel" in response
6363
assert response["channel"]["motd"] == "one apple a day..."
6464

65+
@pytest.mark.asyncio
66+
async def test_update_partial(self, event_loop, channel):
67+
response = await channel.update({"color": "blue", "age": 30})
68+
assert "channel" in response
69+
assert response["channel"]["color"] == "blue"
70+
assert response["channel"]["age"] == 30
71+
72+
response = await channel.update_partial(
73+
to_set={"color": "red"}, to_unset=["age"]
74+
)
75+
assert "channel" in response
76+
assert response["channel"]["color"] == "red"
77+
assert "age" not in response["channel"]
78+
6579
@pytest.mark.asyncio
6680
async def test_delete(self, event_loop, channel):
6781
response = await channel.delete()

stream_chat/tests/test_channel.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ def test_update(self, channel):
5454
assert "channel" in response
5555
assert response["channel"]["motd"] == "one apple a day..."
5656

57+
def test_update_partial(self, channel):
58+
response = channel.update({"color": "blue", "age": 30})
59+
assert "channel" in response
60+
assert response["channel"]["color"] == "blue"
61+
assert response["channel"]["age"] == 30
62+
63+
response = channel.update_partial(to_set={"color": "red"}, to_unset=["age"])
64+
assert "channel" in response
65+
assert response["channel"]["color"] == "red"
66+
assert "age" not in response["channel"]
67+
5768
def test_delete(self, channel):
5869
response = channel.delete()
5970
assert "channel" in response

0 commit comments

Comments
 (0)