Skip to content

Commit

Permalink
Support passing webhook or webhook_id to webhook methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jcwillox committed Apr 5, 2022
1 parent 077947c commit ed6d3d9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 36 deletions.
20 changes: 10 additions & 10 deletions upbankapi/client/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from ..models.categories import AsyncTag, AsyncCategory
from ..models.pagination import AsyncPaginatedList
from ..models.transactions import AsyncTransaction
from ..models.webhooks import AsyncWebhook, AsyncWebhookEvent, AsyncWebhookLog
from ..models.webhooks import AsyncWebhook, AsyncWebhookEvent, AsyncWebhookLog, Webhook

try:
import aiohttp
Expand Down Expand Up @@ -315,28 +315,28 @@ async def create(self, url: str, description: str = None) -> AsyncWebhook:
"""
return AsyncWebhook(self._client, await self._handle_create(url, description))

async def ping(self, webhook_id: str) -> AsyncWebhookEvent:
async def ping(self, webhook: Union[str, Webhook]) -> AsyncWebhookEvent:
"""Pings a webhook by its unique id.
Arguments:
webhook_id: The unique identifier for a webhook.
webhook: The webhook or webhook id to ping.
Returns:
The ping event response.
"""
return AsyncWebhookEvent(self._client, await self._handle_ping(webhook_id))
return AsyncWebhookEvent(self._client, await self._handle_ping(webhook))

async def logs(
self,
webhook_id: str,
webhook: Union[str, Webhook],
*,
limit: int = None,
page_size: int = DEFAULT_PAGE_SIZE,
) -> AsyncPaginatedList[AsyncWebhookLog]:
"""Retrieves the logs from a webhook by id.
Arguments:
webhook_id: The unique identifier for a webhook.
webhook: The webhook or webhook id to fetch logs from.
limit: The maximum number of records to return.
page_size: The number of records to return in each page. (max appears to be 100)
Expand All @@ -346,17 +346,17 @@ async def logs(
return AsyncPaginatedList(
self._client,
AsyncWebhookLog,
await self._handle_logs(webhook_id, limit, page_size),
await self._handle_logs(webhook, limit, page_size),
limit,
)

async def delete(self, webhook_id: str) -> bool:
async def delete(self, webhook: Union[str, Webhook]) -> bool:
"""Deletes a webhook by its unique id.
Arguments:
webhook_id: The unique identifier for a webhook.
webhook: The webhook or webhook id to delete.
Returns:
`True` if successful, otherwise raises exception.
"""
return await self._handle_delete(webhook_id)
return await self._handle_delete(webhook)
18 changes: 9 additions & 9 deletions upbankapi/client/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,28 +297,28 @@ def create(self, url: str, description: str = None) -> Webhook:
"""
return Webhook(self._client, self._handle_create(url, description))

def ping(self, webhook_id: str) -> WebhookEvent:
def ping(self, webhook: Union[str, Webhook]) -> WebhookEvent:
"""Pings a webhook by its unique id.
Arguments:
webhook_id: The unique identifier for a webhook.
webhook: The webhook or webhook id to ping.
Returns:
The ping event response.
"""
return WebhookEvent(self._client, self._handle_ping(webhook_id))
return WebhookEvent(self._client, self._handle_ping(webhook))

def logs(
self,
webhook_id: str,
webhook: Union[str, Webhook],
*,
limit: int = None,
page_size: int = DEFAULT_PAGE_SIZE,
) -> PaginatedList[WebhookLog]:
"""Retrieves the logs from a webhook by id.
Arguments:
webhook_id: The unique identifier for a webhook.
webhook: The webhook or webhook id to fetch logs from.
limit: The maximum number of records to return.
page_size: The number of records to return in each page. (max appears to be 100)
Expand All @@ -328,17 +328,17 @@ def logs(
return PaginatedList(
self._client,
WebhookLog,
self._handle_logs(webhook_id, limit, page_size),
self._handle_logs(webhook, limit, page_size),
limit,
)

def delete(self, webhook_id: str) -> bool:
def delete(self, webhook: Union[str, Webhook]) -> bool:
"""Deletes a webhook by its unique id.
Arguments:
webhook_id: The unique identifier for a webhook.
webhook: The webhook or webhook id to delete.
Returns:
`True` if successful, otherwise raises exception.
"""
return self._handle_delete(webhook_id)
return self._handle_delete(webhook)
26 changes: 17 additions & 9 deletions upbankapi/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,19 @@ def _handle_create(self, url: str, description: str = None):

@abstractmethod
def ping(
self, webhook_id: str
self, webhook: Union[str, Webhook]
) -> Union[WebhookEvent, Coroutine[Any, Any, AsyncWebhookEvent]]:
...

def _handle_ping(self, webhook_id: str):
return self._client.api(f"/webhooks/{webhook_id}/ping", method="POST")
def _handle_ping(self, webhook: Union[str, Webhook]):
if isinstance(webhook, Webhook):
webhook = webhook.id
return self._client.api(f"/webhooks/{webhook}/ping", method="POST")

@abstractmethod
def logs(
self,
webhook_id: str,
webhook: Union[str, Webhook],
*,
limit: int = None,
page_size: int = DEFAULT_PAGE_SIZE,
Expand All @@ -340,17 +342,23 @@ def logs(

def _handle_logs(
self,
webhook_id: str,
webhook: Union[str, Webhook],
limit: int = None,
page_size: int = DEFAULT_PAGE_SIZE,
):
if isinstance(webhook, Webhook):
webhook = webhook.id
return self._client.api(
f"/webhooks/{webhook_id}/logs", params=Filters(page_size, limit)
f"/webhooks/{webhook}/logs", params=Filters(page_size, limit)
)

@abstractmethod
def delete(self, webhook_id: str) -> Union[bool, Coroutine[Any, Any, bool]]:
def delete(
self, webhook: Union[str, Webhook]
) -> Union[bool, Coroutine[Any, Any, bool]]:
...

def _handle_delete(self, webhook_id: str):
return self._client.api(f"/webhooks/{webhook_id}", method="DELETE")
def _handle_delete(self, webhook: Union[str, Webhook]):
if isinstance(webhook, Webhook):
webhook = webhook.id
return self._client.api(f"/webhooks/{webhook}", method="DELETE")
14 changes: 6 additions & 8 deletions upbankapi/models/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def ping(self) -> WebhookEvent:
Returns:
The ping event response.
"""
return self._client.webhook.ping(self.id)
return self._client.webhook.ping(self)

def logs(
self,
Expand All @@ -74,15 +74,15 @@ def logs(
Returns:
A paginated list of the webhook logs.
"""
return self._client.webhook.logs(self.id, limit=limit, page_size=page_size)
return self._client.webhook.logs(self, limit=limit, page_size=page_size)

def delete(self) -> bool:
"""Deletes this webhook.
Returns:
`True` if successful, otherwise raises exception.
"""
return self._client.webhook.delete(self.id)
return self._client.webhook.delete(self)

def __repr__(self) -> str:
"""Return the representation of the webhook."""
Expand All @@ -98,7 +98,7 @@ async def ping(self) -> AsyncWebhookEvent:
Returns:
The ping event response.
"""
return await self._client.webhook.ping(self.id)
return await self._client.webhook.ping(self)

async def logs(
self,
Expand All @@ -115,17 +115,15 @@ async def logs(
Returns:
A paginated list of the webhook logs.
"""
return await self._client.webhook.logs(
self.id, limit=limit, page_size=page_size
)
return await self._client.webhook.logs(self, limit=limit, page_size=page_size)

async def delete(self) -> bool:
"""Deletes this webhook.
Returns:
`True` if successful, otherwise raises exception.
"""
return await self._client.webhook.delete(self.id)
return await self._client.webhook.delete(self)


class WebhookResponse:
Expand Down

0 comments on commit ed6d3d9

Please sign in to comment.