Skip to content

Commit

Permalink
feat: add custom event endpoint (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdeme committed Feb 11, 2022
1 parent 1343f43 commit b619130
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions stream_chat/async_chat/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,9 @@ async def get_export_channel_status(self, task_id: str) -> StreamResponse:
async def get_task(self, task_id: str) -> StreamResponse:
return await self.get(f"tasks/{task_id}")

async def send_user_custom_event(self, user_id: str, event: Dict) -> StreamResponse:
return await self.post(f"users/{user_id}/event", data={"event": event})

async def close(self) -> None:
await self.session.close()

Expand Down
9 changes: 9 additions & 0 deletions stream_chat/base/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,15 @@ def get_task(
"""
pass

@abc.abstractmethod
def send_user_custom_event(
self, user_id: str, event: Dict
) -> Union[StreamResponse, Awaitable[StreamResponse]]:
"""
Allows you to send custom events to a connected user.
"""
pass

#####################
# Private methods #
#####################
Expand Down
3 changes: 3 additions & 0 deletions stream_chat/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,3 +560,6 @@ def get_export_channel_status(self, task_id: str) -> StreamResponse:

def get_task(self, task_id: str) -> StreamResponse:
return self.get(f"tasks/{task_id}")

def send_user_custom_event(self, user_id: str, event: Dict) -> StreamResponse:
return self.post(f"users/{user_id}/event", data={"event": event})
7 changes: 7 additions & 0 deletions stream_chat/tests/async_chat/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,13 @@ async def test_delete_channels(self, client: StreamChatAsync, channel: Channel):

pytest.fail("task did not succeed")

async def test_send_user_custom_event(
self, client: StreamChatAsync, random_user: Dict
):
await client.send_user_custom_event(
random_user["id"], {"type": "friendship_request", "text": "testtext"}
)

@pytest.mark.asyncio
async def test_stream_response(self, client: StreamChatAsync):
resp = await client.get_app_settings()
Expand Down
5 changes: 5 additions & 0 deletions stream_chat/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,11 @@ def test_delete_channels(self, client: StreamChat, channel: Channel):

pytest.fail("task did not succeed")

def test_send_user_custom_event(self, client: StreamChat, random_user: Dict):
client.send_user_custom_event(
random_user["id"], {"type": "friendship_request", "text": "testtext"}
)

def test_stream_response_contains_metadata(self, client: StreamChat):
resp = client.get_app_settings()

Expand Down

0 comments on commit b619130

Please sign in to comment.