Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PBE-1392] add stop_at support #158

Merged
merged 4 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions stream_chat/async_chat/campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ async def delete(self, **options: Any) -> StreamResponse:
)

async def start(
self, scheduled_for: Optional[Union[str, datetime.datetime]] = None
self,
scheduled_for: Optional[Union[str, datetime.datetime]] = None,
stop_at: Optional[Union[str, datetime.datetime]] = None,
) -> StreamResponse:
return await self.client.start_campaign( # type: ignore
campaign_id=self.campaign_id, scheduled_for=scheduled_for
campaign_id=self.campaign_id, scheduled_for=scheduled_for, stop_at=stop_at
)

async def stop(self) -> StreamResponse:
Expand Down
7 changes: 6 additions & 1 deletion stream_chat/async_chat/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ async def send_file(
return await self._parse_response(response)

async def create_blocklist(
self, name: str, words: Iterable[str], type: str = "regular"
self, name: str, words: Iterable[str], type: str = "word"
) -> StreamResponse:
return await self.post(
"blocklists", data={"name": name, "words": words, "type": type}
Expand Down Expand Up @@ -676,12 +676,17 @@ async def start_campaign(
self,
campaign_id: str,
scheduled_for: Optional[Union[str, datetime.datetime]] = None,
stop_at: Optional[Union[str, datetime.datetime]] = None,
) -> StreamResponse:
payload = {}
if scheduled_for is not None:
if isinstance(scheduled_for, datetime.datetime):
scheduled_for = scheduled_for.isoformat()
payload["scheduled_for"] = scheduled_for
if stop_at is not None:
if isinstance(stop_at, datetime.datetime):
stop_at = stop_at.isoformat()
payload["stop_at"] = stop_at
return await self.post(f"campaigns/{campaign_id}/start", data=payload)

async def stop_campaign(self, campaign_id: str) -> StreamResponse:
Expand Down
4 changes: 3 additions & 1 deletion stream_chat/base/campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def delete(self) -> Union[StreamResponse, Awaitable[StreamResponse]]:

@abc.abstractmethod
def start(
self, scheduled_for: Optional[Union[str, datetime.datetime]] = None
self,
scheduled_for: Optional[Union[str, datetime.datetime]] = None,
stop_at: Optional[Union[str, datetime.datetime]] = None,
) -> Union[StreamResponse, Awaitable[StreamResponse]]:
pass

Expand Down
3 changes: 2 additions & 1 deletion stream_chat/base/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ def send_file(

@abc.abstractmethod
def create_blocklist(
self, name: str, words: Iterable[str], type: str = "regular"
self, name: str, words: Iterable[str], type: str = "word"
) -> Union[StreamResponse, Awaitable[StreamResponse]]:
"""
Create a blocklist
Expand Down Expand Up @@ -1102,6 +1102,7 @@ def start_campaign(
self,
campaign_id: str,
scheduled_for: Optional[Union[str, datetime.datetime]] = None,
stop_at: Optional[Union[str, datetime.datetime]] = None,
) -> Union[StreamResponse, Awaitable[StreamResponse]]:
"""
Start a campaign at given time or now if not specified
Expand Down
6 changes: 4 additions & 2 deletions stream_chat/campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ def delete(self, **options: Any) -> StreamResponse:
)

def start(
self, scheduled_for: Optional[Union[str, datetime.datetime]] = None
self,
scheduled_for: Optional[Union[str, datetime.datetime]] = None,
stop_at: Optional[Union[str, datetime.datetime]] = None,
) -> StreamResponse:
return self.client.start_campaign( # type: ignore
campaign_id=self.campaign_id, scheduled_for=scheduled_for
campaign_id=self.campaign_id, scheduled_for=scheduled_for, stop_at=stop_at
)

def stop(self) -> StreamResponse:
Expand Down
9 changes: 7 additions & 2 deletions stream_chat/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def send_file(
return self._parse_response(response)

def create_blocklist(
self, name: str, words: Iterable[str], type: str = "regular"
self, name: str, words: Iterable[str], type: str = "word"
) -> StreamResponse:
return self.post(
"blocklists", data={"name": name, "words": words, "type": type}
Expand Down Expand Up @@ -609,7 +609,7 @@ def remove_segment_targets(
)

def campaign( # type: ignore
self, campaign_id: Optional[str] = None, data: CampaignData = None
self, campaign_id: Optional[str] = None, data: Optional[CampaignData] = None
) -> Campaign:
return Campaign(client=self, campaign_id=campaign_id, data=data)

Expand Down Expand Up @@ -649,12 +649,17 @@ def start_campaign(
self,
campaign_id: str,
scheduled_for: Optional[Union[str, datetime.datetime]] = None,
stop_at: Optional[Union[str, datetime.datetime]] = None,
) -> StreamResponse:
payload = {}
if scheduled_for is not None:
if isinstance(scheduled_for, datetime.datetime):
scheduled_for = scheduled_for.isoformat()
payload["scheduled_for"] = scheduled_for
if stop_at is not None:
if isinstance(stop_at, datetime.datetime):
stop_at = stop_at.isoformat()
payload["stop_at"] = stop_at
return self.post(f"campaigns/{campaign_id}/start", data=payload)

def stop_campaign(self, campaign_id: str) -> StreamResponse:
Expand Down
5 changes: 4 additions & 1 deletion stream_chat/tests/async_chat/test_campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ async def test_campaign_start_stop(

now = datetime.datetime.now(datetime.timezone.utc)
one_hour_later = now + datetime.timedelta(hours=1)
two_hours_later = now + datetime.timedelta(hours=2)

started = await campaign.start(scheduled_for=one_hour_later)
started = await campaign.start(
scheduled_for=one_hour_later, stop_at=two_hours_later
)
assert started.is_ok()
assert "campaign" in started
assert "id" in started["campaign"]
Expand Down
2 changes: 1 addition & 1 deletion stream_chat/tests/async_chat/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ async def test_query_channels_members_in(
assert len(response["channels"][0]["members"]) == 9

async def test_create_blocklist(self, client: StreamChatAsync):
await client.create_blocklist(name="Foo", words=["fudge", "heck"])
await client.create_blocklist(name="Foo", words=["fudge", "heck"], type="word")

async def test_list_blocklists(self, client: StreamChatAsync):
response = await client.list_blocklists()
Expand Down
3 changes: 2 additions & 1 deletion stream_chat/tests/test_campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ def test_campaign_start_stop(self, client: StreamChat, random_user: Dict):

now = datetime.datetime.now(datetime.timezone.utc)
one_hour_later = now + datetime.timedelta(hours=1)
two_hours_later = now + datetime.timedelta(hours=2)

started = campaign.start(scheduled_for=one_hour_later)
started = campaign.start(scheduled_for=one_hour_later, stop_at=two_hours_later)
assert started.is_ok()
assert "campaign" in started
assert "id" in started["campaign"]
Expand Down
2 changes: 1 addition & 1 deletion stream_chat/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ def test_query_channels_members_in(
assert len(response["channels"][0]["members"]) == 9

def test_create_blocklist(self, client: StreamChat):
client.create_blocklist(name="Foo", words=["fudge", "heck"], type="regular")
client.create_blocklist(name="Foo", words=["fudge", "heck"], type="word")

def test_list_blocklists(self, client: StreamChat):
response = client.list_blocklists()
Expand Down
4 changes: 3 additions & 1 deletion stream_chat/types/campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ class ChannelTemplate(TypedDict, total=False):
Parameters:
type: The type of channel.
id: The ID of the channel.
members: List of member IDs.
custom: Custom data.
"""

type: str
id: str
id: Optional[str]
members: Optional[List[str]]
custom: Optional[Dict]


Expand Down
Loading