Skip to content

Commit 6784c5f

Browse files
authored
Add export_channel, get_export_channel_status (#71)
* Add export_channel, get_export_channel_status * add abstract methods
1 parent ee9319e commit 6784c5f

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

stream_chat/async_chat/client.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,55 @@ async def revoke_users_token(self, user_ids, before):
581581
)
582582
await self.update_users_partial(updates)
583583

584+
async def export_channel(self, channel_id: str, channel_type: str, messages_since: str = None,
585+
messages_until=None):
586+
"""
587+
Requests a channel export
588+
:param channel_id: channel_id of channel which needs to be exported
589+
:param channel_type: channel_type of channel which needs to be exported
590+
:param messages_since: RFC-3339 string or datetime to filter messages since that time, optional
591+
:param messages_until: RFC-3339 string or datetime to filter messages until that time, optional
592+
:type channel_id: str
593+
:type channel_type: str
594+
:type messages_since: Union[str, datetime.datetime]
595+
:type messages_until: Union[str, datetime.datetime]
596+
"""
597+
if isinstance(messages_since, datetime.datetime):
598+
messages_since = messages_since.isoformat()
599+
if isinstance(messages_until, datetime.datetime):
600+
messages_until = messages_until.isoformat()
601+
602+
return await self.export_channels(
603+
[
604+
{
605+
"id": channel_id,
606+
"type": channel_type,
607+
"messages_since": messages_since,
608+
"messages_until": messages_until
609+
}
610+
]
611+
)
612+
613+
async def export_channels(self, channels_data: list):
614+
"""
615+
Requests a channels export
616+
:param channels_data: list of channel's data which need to be exported with keys:
617+
- `channel_id`: str
618+
- `channel_type`: str
619+
- `messages_since` (optional, nullable): str
620+
- `messages_until` (optional, nullable): str
621+
:type channels_data: List[Dict[str, str]]
622+
"""
623+
return await self.post("export_channels", data={"channels": channels_data})
624+
625+
async def get_export_channel_status(self, task_id: str):
626+
"""
627+
Retrieves status of export
628+
:param task_id: task_id of task which status needs to be retrieved
629+
:type task_id: str
630+
"""
631+
return await self.get(f"export_channels/{task_id}")
632+
584633
async def close(self):
585634
await self.session.close()
586635

stream_chat/base/client.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,3 +547,25 @@ def revoke_users_token(self, user_ids, since):
547547
Revoke tokens for users issued since
548548
"""
549549
pass
550+
551+
@abc.abstractmethod
552+
def export_channel(self, channel_id: str, channel_type: str, messages_since: str = None,
553+
messages_until=None):
554+
"""
555+
Requests a channel export
556+
"""
557+
pass
558+
559+
@abc.abstractmethod
560+
def export_channels(self, channels_data: list):
561+
"""
562+
Requests a channels export
563+
"""
564+
pass
565+
566+
@abc.abstractmethod
567+
def get_export_channel_status(self, task_id: str):
568+
"""
569+
Retrieves status of export
570+
"""
571+
pass

stream_chat/client.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,52 @@ def revoke_users_token(self, user_ids, before):
572572
{"id": user_id, "set": {"revoke_tokens_issued_before": before}}
573573
)
574574
self.update_users_partial(updates)
575+
576+
def export_channel(self, channel_id: str, channel_type: str, messages_since: str = None,
577+
messages_until=None):
578+
"""
579+
Requests a channel export
580+
:param channel_id: channel_id of channel which needs to be exported
581+
:param channel_type: channel_type of channel which needs to be exported
582+
:param messages_since: RFC-3339 string or datetime to filter messages since that time, optional
583+
:param messages_until: RFC-3339 string or datetime to filter messages until that time, optional
584+
:type channel_id: str
585+
:type channel_type: str
586+
:type messages_since: Union[str, datetime.datetime]
587+
:type messages_until: Union[str, datetime.datetime]
588+
"""
589+
if isinstance(messages_since, datetime.datetime):
590+
messages_since = messages_since.isoformat()
591+
if isinstance(messages_until, datetime.datetime):
592+
messages_until = messages_until.isoformat()
593+
594+
return self.export_channels(
595+
[
596+
{
597+
"id": channel_id,
598+
"type": channel_type,
599+
"messages_since": messages_since,
600+
"messages_until": messages_until
601+
}
602+
]
603+
)
604+
605+
def export_channels(self, channels_data: list):
606+
"""
607+
Requests a channels export
608+
:param channels_data: list of channel's data which need to be exported with keys:
609+
- `channel_id`: str
610+
- `channel_type`: str
611+
- `messages_since` (optional, nullable): str
612+
- `messages_until` (optional, nullable): str
613+
:type channels_data: List[Dict[str, str]]
614+
"""
615+
return self.post("export_channels", data={"channels": channels_data})
616+
617+
def get_export_channel_status(self, task_id: str):
618+
"""
619+
Retrieves status of export
620+
:param task_id: task_id of task which status needs to be retrieved
621+
:type task_id: str
622+
"""
623+
return self.get(f"export_channels/{task_id}")

0 commit comments

Comments
 (0)