Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
nessshon committed Oct 21, 2024
1 parent b39b41e commit 13f678e
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pytonapi/async_tonapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ def utilities(self) -> methods.UtilitiesMethod:
def wallet(self) -> methods.WalletMethod:
return methods.WalletMethod(**self.__dict__)

@property
def webhooks(self) -> methods.WebhooksMethod:
return methods.WebhooksMethod(**self.__dict__)

@property
def websocket(self) -> methods.WebSocketMethod:
return methods.WebSocketMethod(**self.__dict__)
19 changes: 19 additions & 0 deletions pytonapi/async_tonapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,22 @@ async def _post(
if self.max_retries > 0:
request = self._request_retries
return await request("POST", method, headers, params=params, body=body)

async def _delete(
self,
method: str,
params: Optional[Dict[str, Any]] = None,
headers: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
"""
Make a DELETE request.
:param method: The API method.
:param params: Optional query parameters.
:param headers: Optional headers to include in the request.
:return: The response content as a dictionary.
"""
request = self._request
if self.max_retries > 0:
request = self._request_retries
return await request("DELETE", method, headers, params=params)
2 changes: 2 additions & 0 deletions pytonapi/async_tonapi/methods/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .traces import TracesMethod
from .utilites import UtilitiesMethod
from .wallet import WalletMethod
from .webhooks import WebhooksMethod
from .websocket import WebSocketMethod

__all__ = [
Expand All @@ -39,5 +40,6 @@
"TracesMethod",
"UtilitiesMethod",
"WalletMethod",
"WebhooksMethod",
"WebSocketMethod",
]
81 changes: 81 additions & 0 deletions pytonapi/async_tonapi/methods/webhooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from typing import List

from pytonapi.async_tonapi.client import AsyncTonapiClientBase
from pytonapi.schema.webhooks import WebhookCreate, WebhookList, AccountSubscriptions


class WebhooksMethod(AsyncTonapiClientBase):

def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.base_url = "https://rt.tonapi.io/"

async def create_webhook(self, endpoint: str) -> WebhookCreate:
"""
Create a new webhook and return its ID.
:param endpoint: The webhook endpoint URL to receive transaction events.
:return: An object containing the ID of the created webhook.
:rtype: WebhookCreate
"""
method = "webhooks"
body = {"endpoint": endpoint}
response = await self._post(method=method, body=body)
return WebhookCreate(**response)

async def list_webhooks(self) -> WebhookList:
"""
Retrieve a list of all available webhooks.
:return: A list containing all webhooks with their IDs and endpoints.
:rtype: WebhookList
"""
method = "webhooks"
response = await self._get(method=method)
return WebhookList(**response)

async def delete_webhook(self, webhook_id: int) -> None:
"""
Delete a webhook and all its subscriptions.
:param webhook_id: The ID of the webhook to delete.
"""
method = f"webhooks/{webhook_id}"
await self._delete(method=method)

async def subscribe_to_account(self, webhook_id: int, accounts: List[str]) -> None:
"""
Subscribe a webhook to specific account transactions.
:param webhook_id: The ID of the webhook to subscribe.
:param accounts: A list of account IDs to subscribe to.
"""
method = f"webhooks/{webhook_id}/account-tx/subscribe"
body = {"accounts": [{"account_id": account} for account in accounts]}
await self._post(method=method, body=body)

async def unsubscribe_from_account(self, webhook_id: int, accounts: List[str]) -> None:
"""
Unsubscribe a webhook from specific account transactions.
:param webhook_id: The ID of the webhook to unsubscribe.
:param accounts: A list of account IDs to unsubscribe from.
"""
method = f"webhooks/{webhook_id}/account-tx/unsubscribe"
body = {"accounts": accounts}
await self._post(method=method, body=body)

async def get_subscriptions(self, webhook_id: int, offset: int = 0, limit: int = 10) -> AccountSubscriptions:
"""
Retrieve the list of subscriptions for a given webhook.
:param webhook_id: The ID of the webhook.
:param offset: The offset for pagination. Default is 0.
:param limit: The maximum number of subscriptions to return. Default is 10.
:return: A list of account transaction subscriptions with details.
:rtype: AccountSubscriptions
"""
method = f"webhooks/{webhook_id}/account-tx/subscriptions"
params = {"offset": offset, "limit": limit}
response = await self._get(method=method, params=params)
return AccountSubscriptions(**response)
33 changes: 33 additions & 0 deletions pytonapi/schema/webhooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from typing import List, Optional

from pydantic import BaseModel


class WebhookCreate(BaseModel):
webhook_id: int


class Webhook(BaseModel):
id: int
endpoint: str


class WebhookList(BaseModel):
webhooks: List[Webhook]


class AccountSubscription(BaseModel):
accounts: List[dict]



class AccountTxSubscription(BaseModel):
account_id: str
last_delivered_lt: int
failed_at: Optional[str] = None
failed_lt: Optional[int] = None
failed_attempts: Optional[int] = None


class AccountSubscriptions(BaseModel):
account_tx_subscriptions: List[AccountTxSubscription]
4 changes: 4 additions & 0 deletions pytonapi/tonapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,7 @@ def utilities(self) -> methods.UtilitiesMethod:
@property
def wallet(self) -> methods.WalletMethod:
return methods.WalletMethod(**self.__dict__)

@property
def webhooks(self) -> methods.WebhooksMethod:
return methods.WebhooksMethod(**self.__dict__)
19 changes: 19 additions & 0 deletions pytonapi/tonapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,22 @@ def _post(
if self.max_retries > 0:
request = self._request_retries
return request("POST", method, headers, params=params, body=body)

def _delete(
self,
method: str,
params: Optional[Dict[str, Any]] = None,
headers: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
"""
Make a DELETE request.
:param method: The API method.
:param params: Optional query parameters.
:param headers: Optional headers to include in the request.
:return: The response content as a dictionary.
"""
request = self._request
if self.max_retries > 0:
request = self._request_retries
return request("DELETE", method, headers, params=params)
2 changes: 2 additions & 0 deletions pytonapi/tonapi/methods/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .traces import TracesMethod
from .utilites import UtilitiesMethod
from .wallet import WalletMethod
from .webhooks import WebhooksMethod

__all__ = [
"AccountsMethod",
Expand All @@ -38,4 +39,5 @@
"TracesMethod",
"UtilitiesMethod",
"WalletMethod",
"WebhooksMethod",
]
81 changes: 81 additions & 0 deletions pytonapi/tonapi/methods/webhooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from typing import List

from pytonapi.schema.webhooks import WebhookCreate, WebhookList, AccountSubscriptions
from pytonapi.tonapi.client import TonapiClientBase


class WebhooksMethod(TonapiClientBase):

def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.base_url = "https://rt.tonapi.io/"

def create_webhook(self, endpoint: str) -> WebhookCreate:
"""
Create a new webhook and return its ID.
:param endpoint: The webhook endpoint URL to receive transaction events.
:return: An object containing the ID of the created webhook.
:rtype: WebhookCreate
"""
method = "webhooks"
body = {"endpoint": endpoint}
response = self._post(method=method, body=body)
return WebhookCreate(**response)

def list_webhooks(self) -> WebhookList:
"""
Retrieve a list of all available webhooks.
:return: A list containing all webhooks with their IDs and endpoints.
:rtype: WebhookList
"""
method = "webhooks"
response = self._get(method=method)
return WebhookList(**response)

def delete_webhook(self, webhook_id: int) -> None:
"""
Delete a webhook and all its subscriptions.
:param webhook_id: The ID of the webhook to delete.
"""
method = f"webhooks/{webhook_id}"
self._delete(method=method)

def subscribe_to_account(self, webhook_id: int, accounts: List[str]) -> None:
"""
Subscribe a webhook to specific account transactions.
:param webhook_id: The ID of the webhook to subscribe.
:param accounts: A list of account IDs to subscribe to.
"""
method = f"webhooks/{webhook_id}/account-tx/subscribe"
body = {"accounts": [{"account_id": account} for account in accounts]}
self._post(method=method, body=body)

def unsubscribe_from_account(self, webhook_id: int, accounts: List[str]) -> None:
"""
Unsubscribe a webhook from specific account transactions.
:param webhook_id: The ID of the webhook to unsubscribe.
:param accounts: A list of account IDs to unsubscribe from.
"""
method = f"webhooks/{webhook_id}/account-tx/unsubscribe"
body = {"accounts": accounts}
self._post(method=method, body=body)

def get_subscriptions(self, webhook_id: int, offset: int = 0, limit: int = 10) -> AccountSubscriptions:
"""
Retrieve the list of subscriptions for a given webhook.
:param webhook_id: The ID of the webhook.
:param offset: The offset for pagination. Default is 0.
:param limit: The maximum number of subscriptions to return. Default is 10.
:return: A list of account transaction subscriptions with details.
:rtype: AccountSubscriptions
"""
method = f"webhooks/{webhook_id}/account-tx/subscriptions"
params = {"offset": offset, "limit": limit}
response = self._get(method=method, params=params)
return AccountSubscriptions(**response)

0 comments on commit 13f678e

Please sign in to comment.