|
13 | 13 | Optional, |
14 | 14 | Type, |
15 | 15 | Union, |
| 16 | + cast, |
16 | 17 | ) |
17 | 18 | from urllib.parse import urlparse |
18 | 19 |
|
| 20 | +from stream_chat.async_chat.campaign import Campaign |
| 21 | +from stream_chat.async_chat.segment import Segment |
| 22 | +from stream_chat.types.base import SortParam |
| 23 | +from stream_chat.types.campaign import CampaignData, QueryCampaignsOptions |
| 24 | +from stream_chat.types.segment import ( |
| 25 | + QuerySegmentsOptions, |
| 26 | + QuerySegmentTargetsOptions, |
| 27 | + SegmentData, |
| 28 | + SegmentType, |
| 29 | +) |
| 30 | + |
19 | 31 | if sys.version_info >= (3, 8): |
20 | 32 | from typing import Literal |
21 | 33 | else: |
@@ -537,45 +549,143 @@ async def delete_role(self, name: str) -> StreamResponse: |
537 | 549 | async def list_roles(self) -> StreamResponse: |
538 | 550 | return await self.get("roles") |
539 | 551 |
|
540 | | - async def create_segment(self, segment: Dict) -> StreamResponse: |
541 | | - return await self.post("segments", data={"segment": segment}) |
| 552 | + def segment( # type: ignore |
| 553 | + self, |
| 554 | + segment_type: SegmentType, |
| 555 | + segment_id: Optional[str] = None, |
| 556 | + data: Optional[SegmentData] = None, |
| 557 | + ) -> Segment: |
| 558 | + return Segment( |
| 559 | + client=self, segment_type=segment_type, segment_id=segment_id, data=data |
| 560 | + ) |
| 561 | + |
| 562 | + async def create_segment( |
| 563 | + self, |
| 564 | + segment_type: SegmentType, |
| 565 | + segment_id: Optional[str] = None, |
| 566 | + data: Optional[SegmentData] = None, |
| 567 | + ) -> StreamResponse: |
| 568 | + payload = {"type": segment_type.value} |
| 569 | + if segment_id is not None: |
| 570 | + payload["id"] = segment_id |
| 571 | + if data is not None: |
| 572 | + payload.update(cast(dict, data)) |
| 573 | + return await self.post("segments", data=payload) |
542 | 574 |
|
543 | | - async def query_segments(self, **params: Any) -> StreamResponse: |
544 | | - return await self.get("segments", params={"payload": json.dumps(params)}) |
| 575 | + async def get_segment(self, segment_id: str) -> StreamResponse: |
| 576 | + return await self.get(f"segments/{segment_id}") |
545 | 577 |
|
546 | | - async def update_segment(self, segment_id: str, data: Dict) -> StreamResponse: |
547 | | - return await self.put(f"segments/{segment_id}", data={"segment": data}) |
| 578 | + async def query_segments( |
| 579 | + self, |
| 580 | + filter_conditions: Optional[Dict[str, Any]] = None, |
| 581 | + sort: Optional[List[SortParam]] = None, |
| 582 | + options: Optional[QuerySegmentsOptions] = None, |
| 583 | + ) -> StreamResponse: |
| 584 | + payload = {} |
| 585 | + if filter_conditions is not None: |
| 586 | + payload["filter"] = filter_conditions |
| 587 | + if sort is not None: |
| 588 | + payload["sort"] = sort # type: ignore |
| 589 | + if options is not None: |
| 590 | + payload.update(cast(dict, options)) |
| 591 | + return await self.post("segments/query", data=payload) |
| 592 | + |
| 593 | + async def update_segment( |
| 594 | + self, segment_id: str, data: SegmentData |
| 595 | + ) -> StreamResponse: |
| 596 | + return await self.put(f"segments/{segment_id}", data=data) |
548 | 597 |
|
549 | 598 | async def delete_segment(self, segment_id: str) -> StreamResponse: |
550 | 599 | return await self.delete(f"segments/{segment_id}") |
551 | 600 |
|
552 | | - async def create_campaign(self, campaign: Dict) -> StreamResponse: |
553 | | - return await self.post("campaigns", data={"campaign": campaign}) |
| 601 | + async def segment_target_exists( |
| 602 | + self, segment_id: str, target_id: str |
| 603 | + ) -> StreamResponse: |
| 604 | + return await self.get(f"segments/{segment_id}/target/{target_id}") |
554 | 605 |
|
555 | | - async def query_campaigns(self, **params: Any) -> StreamResponse: |
556 | | - return await self.get("campaigns", params={"payload": json.dumps(params)}) |
| 606 | + async def add_segment_targets( |
| 607 | + self, segment_id: str, target_ids: List[str] |
| 608 | + ) -> StreamResponse: |
| 609 | + return await self.post( |
| 610 | + f"segments/{segment_id}/addtargets", data={"target_ids": target_ids} |
| 611 | + ) |
557 | 612 |
|
558 | | - async def update_campaign(self, campaign_id: str, data: Dict) -> StreamResponse: |
559 | | - return await self.put(f"campaigns/{campaign_id}", data={"campaign": data}) |
| 613 | + async def query_segment_targets( |
| 614 | + self, |
| 615 | + segment_id: str, |
| 616 | + filter_conditions: Optional[Dict[str, Any]] = None, |
| 617 | + sort: Optional[List[SortParam]] = None, |
| 618 | + options: Optional[QuerySegmentTargetsOptions] = None, |
| 619 | + ) -> StreamResponse: |
| 620 | + payload = {} |
| 621 | + if filter_conditions is not None: |
| 622 | + payload["filter"] = filter_conditions |
| 623 | + if sort is not None: |
| 624 | + payload["sort"] = sort # type: ignore |
| 625 | + if options is not None: |
| 626 | + payload.update(cast(dict, options)) |
| 627 | + return await self.post(f"segments/{segment_id}/targets/query", data=payload) |
| 628 | + |
| 629 | + async def remove_segment_targets( |
| 630 | + self, segment_id: str, target_ids: List[str] |
| 631 | + ) -> StreamResponse: |
| 632 | + return await self.post( |
| 633 | + f"segments/{segment_id}/deletetargets", data={"target_ids": target_ids} |
| 634 | + ) |
560 | 635 |
|
561 | | - async def delete_campaign(self, campaign_id: str, **options: Any) -> StreamResponse: |
562 | | - return await self.delete(f"campaigns/{campaign_id}", params=options) |
| 636 | + def campaign( # type: ignore |
| 637 | + self, campaign_id: Optional[str] = None, data: Optional[CampaignData] = None |
| 638 | + ) -> Campaign: |
| 639 | + return Campaign(client=self, campaign_id=campaign_id, data=data) |
563 | 640 |
|
564 | | - async def schedule_campaign( |
565 | | - self, campaign_id: str, scheduled_for: int = None |
| 641 | + async def create_campaign( |
| 642 | + self, campaign_id: Optional[str] = None, data: Optional[CampaignData] = None |
566 | 643 | ) -> StreamResponse: |
567 | | - return await self.patch( |
568 | | - f"campaigns/{campaign_id}/schedule", data={"scheduled_for": scheduled_for} |
569 | | - ) |
| 644 | + payload = {"id": campaign_id} |
| 645 | + if data is not None: |
| 646 | + payload.update(cast(dict, data)) |
| 647 | + return await self.post("campaigns", data=payload) |
570 | 648 |
|
571 | | - async def query_recipients(self, **params: Any) -> StreamResponse: |
572 | | - return await self.get("recipients", params={"payload": json.dumps(params)}) |
| 649 | + async def get_campaign(self, campaign_id: str) -> StreamResponse: |
| 650 | + return await self.get(f"campaigns/{campaign_id}") |
573 | 651 |
|
574 | | - async def stop_campaign(self, campaign_id: str) -> StreamResponse: |
575 | | - return await self.patch(f"campaigns/{campaign_id}/stop") |
| 652 | + async def query_campaigns( |
| 653 | + self, |
| 654 | + filter_conditions: Optional[Dict[str, Any]] = None, |
| 655 | + sort: Optional[List[SortParam]] = None, |
| 656 | + options: QueryCampaignsOptions = None, |
| 657 | + ) -> StreamResponse: |
| 658 | + payload = {} |
| 659 | + if filter_conditions is not None: |
| 660 | + payload["filter"] = filter_conditions |
| 661 | + if sort is not None: |
| 662 | + payload["sort"] = sort # type: ignore |
| 663 | + if options is not None: |
| 664 | + payload.update(cast(dict, options)) |
| 665 | + return await self.post("campaigns/query", data=payload) |
| 666 | + |
| 667 | + async def update_campaign( |
| 668 | + self, campaign_id: str, data: CampaignData |
| 669 | + ) -> StreamResponse: |
| 670 | + return await self.put(f"campaigns/{campaign_id}", data=data) |
| 671 | + |
| 672 | + async def delete_campaign(self, campaign_id: str, **options: Any) -> StreamResponse: |
| 673 | + return await self.delete(f"campaigns/{campaign_id}", options) |
576 | 674 |
|
577 | | - async def resume_campaign(self, campaign_id: str) -> StreamResponse: |
578 | | - return await self.patch(f"campaigns/{campaign_id}/resume") |
| 675 | + async def start_campaign( |
| 676 | + self, |
| 677 | + campaign_id: str, |
| 678 | + scheduled_for: Optional[Union[str, datetime.datetime]] = None, |
| 679 | + ) -> StreamResponse: |
| 680 | + payload = {} |
| 681 | + if scheduled_for is not None: |
| 682 | + if isinstance(scheduled_for, datetime.datetime): |
| 683 | + scheduled_for = scheduled_for.isoformat() |
| 684 | + payload["scheduled_for"] = scheduled_for |
| 685 | + return await self.post(f"campaigns/{campaign_id}/start", data=payload) |
| 686 | + |
| 687 | + async def stop_campaign(self, campaign_id: str) -> StreamResponse: |
| 688 | + return await self.post(f"campaigns/{campaign_id}/stop") |
579 | 689 |
|
580 | 690 | async def test_campaign( |
581 | 691 | self, campaign_id: str, users: Iterable[str] |
|
0 commit comments