-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PBE-1321] support campaign endpoints (#156)
* [PBE-1321] align campaign api with backend impl * query_campaigns & query_segments => POST requests * [PBE-1321] create resource objects; test segment CRUD * [PBE-1321] support sync channel & segment res objects * [PBE-1321] support async layer * [PBE-1321] rename targets to target_ids * [PBE-1321] add targets query support, fix tests * [PBE-1321] lint fixes * [PBE-1321] lint fixes * [PBE-1321] lint fixes * [PBE-1321] return module scope * [PBE-1321] add custom fields * [PBE-1321] align with API * [PBE-1321] align query_targets * [PBE-1321] use SortOrder * [PBE-1321] align query API * [PBE-1321] lint fixes * [PBE-1321] revert async test init * [PBE-1321] lint fixes * [PBE-1321] v3.7 compatibility issues * [PBE-1321] disable new endpoint tests * [PBE-1321] test fixes * [PBE-1321] more scope changes
- Loading branch information
Showing
18 changed files
with
1,485 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import datetime | ||
from typing import Any, Optional, Union | ||
|
||
from stream_chat.base.campaign import CampaignInterface | ||
from stream_chat.types.campaign import CampaignData | ||
from stream_chat.types.stream_response import StreamResponse | ||
|
||
|
||
class Campaign(CampaignInterface): | ||
async def create( | ||
self, campaign_id: Optional[str] = None, data: Optional[CampaignData] = None | ||
) -> StreamResponse: | ||
if campaign_id is not None: | ||
self.campaign_id = campaign_id | ||
if data is not None: | ||
self.data = data | ||
state = await self.client.create_campaign( # type: ignore | ||
campaign_id=self.campaign_id, data=self.data | ||
) | ||
|
||
if self.campaign_id is None and state.is_ok() and "campaign" in state: | ||
self.campaign_id = state["campaign"]["id"] | ||
return state | ||
|
||
async def get(self) -> StreamResponse: | ||
return await self.client.get_campaign( # type: ignore | ||
campaign_id=self.campaign_id | ||
) | ||
|
||
async def update(self, data: CampaignData) -> StreamResponse: | ||
return await self.client.update_campaign( # type: ignore | ||
campaign_id=self.campaign_id, data=data | ||
) | ||
|
||
async def delete(self, **options: Any) -> StreamResponse: | ||
return await self.client.delete_campaign( # type: ignore | ||
campaign_id=self.campaign_id, **options | ||
) | ||
|
||
async def start( | ||
self, scheduled_for: Optional[Union[str, datetime.datetime]] = None | ||
) -> StreamResponse: | ||
return await self.client.start_campaign( # type: ignore | ||
campaign_id=self.campaign_id, scheduled_for=scheduled_for | ||
) | ||
|
||
async def stop(self) -> StreamResponse: | ||
return await self.client.stop_campaign( # type: ignore | ||
campaign_id=self.campaign_id | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from typing import Dict, List, Optional | ||
|
||
from stream_chat.base.segment import SegmentInterface | ||
from stream_chat.types.base import SortParam | ||
from stream_chat.types.segment import QuerySegmentTargetsOptions, SegmentData | ||
from stream_chat.types.stream_response import StreamResponse | ||
|
||
|
||
class Segment(SegmentInterface): | ||
async def create( | ||
self, segment_id: Optional[str] = None, data: Optional[SegmentData] = None | ||
) -> StreamResponse: | ||
if segment_id is not None: | ||
self.segment_id = segment_id | ||
if data is not None: | ||
self.data = data | ||
|
||
state = await self.client.create_segment( # type: ignore | ||
segment_type=self.segment_type, segment_id=self.segment_id, data=self.data | ||
) | ||
|
||
if self.segment_id is None and state.is_ok() and "segment" in state: | ||
self.segment_id = state["segment"]["id"] | ||
return state | ||
|
||
async def get(self) -> StreamResponse: | ||
return await self.client.get_segment(segment_id=self.segment_id) # type: ignore | ||
|
||
async def update(self, data: SegmentData) -> StreamResponse: | ||
return await self.client.update_segment( # type: ignore | ||
segment_id=self.segment_id, data=data | ||
) | ||
|
||
async def delete(self) -> StreamResponse: | ||
return await self.client.delete_segment( # type: ignore | ||
segment_id=self.segment_id | ||
) | ||
|
||
async def target_exists(self, target_id: str) -> StreamResponse: | ||
return await self.client.segment_target_exists( # type: ignore | ||
segment_id=self.segment_id, target_id=target_id | ||
) | ||
|
||
async def add_targets(self, target_ids: list) -> StreamResponse: | ||
return await self.client.add_segment_targets( # type: ignore | ||
segment_id=self.segment_id, target_ids=target_ids | ||
) | ||
|
||
async def query_targets( | ||
self, | ||
filter_conditions: Optional[Dict] = None, | ||
sort: Optional[List[SortParam]] = None, | ||
options: Optional[QuerySegmentTargetsOptions] = None, | ||
) -> StreamResponse: | ||
return await self.client.query_segment_targets( # type: ignore | ||
segment_id=self.segment_id, | ||
filter_conditions=filter_conditions, | ||
sort=sort, | ||
options=options, | ||
) | ||
|
||
async def remove_targets(self, target_ids: list) -> StreamResponse: | ||
return await self.client.remove_segment_targets( # type: ignore | ||
segment_id=self.segment_id, target_ids=target_ids | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import abc | ||
import datetime | ||
from typing import Awaitable, Optional, Union | ||
|
||
from stream_chat.base.client import StreamChatInterface | ||
from stream_chat.types.campaign import CampaignData | ||
from stream_chat.types.stream_response import StreamResponse | ||
|
||
|
||
class CampaignInterface(abc.ABC): | ||
def __init__( | ||
self, | ||
client: StreamChatInterface, | ||
campaign_id: Optional[str] = None, | ||
data: CampaignData = None, | ||
): | ||
self.client = client | ||
self.campaign_id = campaign_id | ||
self.data = data | ||
|
||
@abc.abstractmethod | ||
def create( | ||
self, campaign_id: Optional[str], data: Optional[CampaignData] | ||
) -> Union[StreamResponse, Awaitable[StreamResponse]]: | ||
pass | ||
|
||
@abc.abstractmethod | ||
def get(self) -> Union[StreamResponse, Awaitable[StreamResponse]]: | ||
pass | ||
|
||
@abc.abstractmethod | ||
def update( | ||
self, data: CampaignData | ||
) -> Union[StreamResponse, Awaitable[StreamResponse]]: | ||
pass | ||
|
||
@abc.abstractmethod | ||
def delete(self) -> Union[StreamResponse, Awaitable[StreamResponse]]: | ||
pass | ||
|
||
@abc.abstractmethod | ||
def start( | ||
self, scheduled_for: Optional[Union[str, datetime.datetime]] = None | ||
) -> Union[StreamResponse, Awaitable[StreamResponse]]: | ||
pass | ||
|
||
@abc.abstractmethod | ||
def stop(self) -> Union[StreamResponse, Awaitable[StreamResponse]]: | ||
pass |
Oops, something went wrong.