diff --git a/kenar/__init__.py b/kenar/__init__.py index c4df29f..097439d 100644 --- a/kenar/__init__.py +++ b/kenar/__init__.py @@ -1,9 +1,10 @@ from .addon import * from .app import ClientConfig, Client +from .asset import * from .chatmessage import * +from .events import * from .finder import * from .icons import * from .image import * from .oauth import * from .widgets import * -from .asset import * diff --git a/kenar/app.py b/kenar/app.py index 66779cb..6cd8f8d 100644 --- a/kenar/app.py +++ b/kenar/app.py @@ -30,10 +30,12 @@ GetColorsResponse, ) from kenar.chatmessage import ( - SetNotifyChatPostConversationsRequest, SendMessageV2Request, SendMessageV2Response, - SetNotifyChatPostConversationsResponse, +) +from kenar.events import ( + RegisterEventSubscriptionRequest, + RegisterEventSubscriptionResponse, ) from kenar.finder import ( SearchPostRequest, @@ -64,24 +66,6 @@ class ChatService: def __init__(self, client: httpx.Client): self._client = client - def set_notify_chat_post_conversations( - self, - access_token: str, - data: SetNotifyChatPostConversationsRequest, - max_retry=3, - retry_delay=1, - ) -> SetNotifyChatPostConversationsResponse: - @retry(max_retries=max_retry, delay=retry_delay) - def send_request(): - return self._client.post( - url="/v1/open-platform/notify/chat/post-conversations", - content=data.json(), - headers={ACCESS_TOKEN_HEADER_NAME: access_token}, - ) - - send_request() - return SetNotifyChatPostConversationsResponse() - def send_message( self, access_token: str, @@ -174,6 +158,29 @@ def send_request(): return GetUserPostsResponse(**rsp.json()) +class EventsService: + def __init__(self, client: httpx.Client): + self._client = client + + def register_event_subscription( + self, + access_token: str, + data: RegisterEventSubscriptionRequest, + max_retry=3, + retry_delay=1, + ) -> RegisterEventSubscriptionResponse: + @retry(max_retries=max_retry, delay=retry_delay) + def send_request(): + return self._client.post( + url="/v1/open-platform/events/subscriptions", + content=data.json(), + headers={ACCESS_TOKEN_HEADER_NAME: access_token}, + ) + + send_request() + return RegisterEventSubscriptionResponse() + + class AddonService: def __init__(self, client: httpx.Client): self._client = client @@ -526,6 +533,7 @@ def __init__(self, conf: ClientConfig): ) self._finder = FinderService(self._client) self._chat = ChatService(self._client) + self._events = EventsService(self._client) self._addon = AddonService(self._client) self._asset = AssetService(self._client) @@ -533,6 +541,10 @@ def __init__(self, conf: ClientConfig): def chat(self): return self._chat + @property + def events(self): + return self._events + @chat.setter def chat(self, service: ChatService): if not isinstance(service, ChatService): diff --git a/kenar/chatmessage.py b/kenar/chatmessage.py index 2e80bc9..fa8ccf5 100644 --- a/kenar/chatmessage.py +++ b/kenar/chatmessage.py @@ -1,7 +1,6 @@ from enum import Enum -from typing import Optional - from pydantic import BaseModel +from typing import Optional class BotButton(BaseModel): @@ -19,16 +18,6 @@ class ButtonData(BaseModel): action: Action -class SetNotifyChatPostConversationsRequest(BaseModel): - post_token: str - endpoint: str - identification_key: str - - -class SetNotifyChatPostConversationsResponse(BaseModel): - pass - - class SendMessageV2Request(BaseModel): user_id: str peer_id: str diff --git a/kenar/events.py b/kenar/events.py new file mode 100644 index 0000000..b8eaeee --- /dev/null +++ b/kenar/events.py @@ -0,0 +1,20 @@ +from dataclasses import field +from enum import Enum +from typing import Any, Dict +from typing import Optional + +from pydantic import BaseModel + + +class RegisterEventSubscriptionRequest(BaseModel): + class EventType(Enum): + UNKNOWN = 'UNKNOWN' + NEW_MESSAGE_ON_POST = 'NEW_MESSAGE_ON_POST' + + event_type: EventType + event_resource_id: Optional[str] = field(default="") + metadata: Optional[Dict[str, Any]] = field(default=None) + + +class RegisterEventSubscriptionResponse(BaseModel): + pass diff --git a/samples/sample_chat.py b/samples/sample_chat.py index e8fecef..b42ec91 100644 --- a/samples/sample_chat.py +++ b/samples/sample_chat.py @@ -1,7 +1,7 @@ from kenar import ( SendMessageV2Request, BotButton, - SetNotifyChatPostConversationsRequest, + RegisterEventSubscriptionRequest, ) from samples.sample_app import app @@ -33,11 +33,10 @@ access_token="ACCESS_TOKEN_HERE", ) - app.chat.set_notify_chat_post_conversations( - data=SetNotifyChatPostConversationsRequest( - post_token="gZ6QmeWD", - endpoint="https://test2.com", - identification_key="thest-identification-key", + app.events.register_event_subscription( + data=RegisterEventSubscriptionRequest( + event_type=RegisterEventSubscriptionRequest.EventType.NEW_MESSAGE_ON_POST, + event_resource_id="gZ6QmeWD", ), access_token="ACCESS_TOKEN_HERE", )