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

Use new events subscription API #23

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion kenar/__init__.py
Original file line number Diff line number Diff line change
@@ -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 *
52 changes: 32 additions & 20 deletions kenar/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
GetColorsResponse,
)
from kenar.chatmessage import (
SetNotifyChatPostConversationsRequest,
SendMessageV2Request,
SendMessageV2Response,
SetNotifyChatPostConversationsResponse,
)
from kenar.events import (
RegisterEventSubscriptionRequest,
RegisterEventSubscriptionResponse,
)
from kenar.finder import (
SearchPostRequest,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -526,13 +533,18 @@ 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)

@property
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):
Expand Down
13 changes: 1 addition & 12 deletions kenar/chatmessage.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from enum import Enum
from typing import Optional

from pydantic import BaseModel
from typing import Optional


class BotButton(BaseModel):
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions kenar/events.py
Original file line number Diff line number Diff line change
@@ -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
11 changes: 5 additions & 6 deletions samples/sample_chat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from kenar import (
SendMessageV2Request,
BotButton,
SetNotifyChatPostConversationsRequest,
RegisterEventSubscriptionRequest,
)
from samples.sample_app import app

Expand Down Expand Up @@ -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",
)