Skip to content

Commit

Permalink
feat(webhooks): add types to the unwrap method (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Jan 12, 2024
1 parent a2f83bc commit 2952dfa
Show file tree
Hide file tree
Showing 14 changed files with 655 additions and 7 deletions.
19 changes: 18 additions & 1 deletion api.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,26 @@ Methods:

# Webhooks

Types:

```python
from finch.types import (
AccountUpdateEvent,
BaseWebhookEvent,
CompanyEvent,
DirectoryEvent,
EmploymentEvent,
IndividualEvent,
JobCompletionEvent,
PayStatementEvent,
PaymentEvent,
WebhookEvent,
)
```

Methods:

- <code>client.webhooks.<a href="./src/finch/resources/webhooks.py">unwrap</a>(\*args) -> object</code>
- <code>client.webhooks.<a href="./src/finch/resources/webhooks.py">unwrap</a>(\*args) -> WebhookEvent</code>
- <code>client.webhooks.<a href="./src/finch/resources/webhooks.py">verify_signature</a>(\*args) -> None</code>

# RequestForwarding
Expand Down
23 changes: 19 additions & 4 deletions src/finch/resources/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
import math
import base64
import hashlib
from typing import cast
from datetime import datetime, timezone, timedelta

from ..types import WebhookEvent
from .._types import (
HeadersLike,
)
from .._utils import (
get_required_header,
)
from .._models import construct_type
from .._resource import SyncAPIResource, AsyncAPIResource

__all__ = ["Webhooks", "AsyncWebhooks"]
Expand All @@ -27,10 +30,16 @@ def unwrap(
headers: HeadersLike,
*,
secret: str | None = None,
) -> object:
) -> WebhookEvent:
"""Validates that the given payload was sent by Finch and parses the payload."""
self.verify_signature(payload=payload, headers=headers, secret=secret)
return json.loads(payload)
return cast(
WebhookEvent,
construct_type(
value=json.loads(payload),
type_=WebhookEvent, # type: ignore[arg-type]
),
)

def verify_signature(
self,
Expand Down Expand Up @@ -120,10 +129,16 @@ def unwrap(
headers: HeadersLike,
*,
secret: str | None = None,
) -> object:
) -> WebhookEvent:
"""Validates that the given payload was sent by Finch and parses the payload."""
self.verify_signature(payload=payload, headers=headers, secret=secret)
return json.loads(payload)
return cast(
WebhookEvent,
construct_type(
value=json.loads(payload),
type_=WebhookEvent, # type: ignore[arg-type]
),
)

def verify_signature(
self,
Expand Down
10 changes: 10 additions & 0 deletions src/finch/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,19 @@
from .provider import Provider as Provider
from .money_param import MoneyParam as MoneyParam
from .income_param import IncomeParam as IncomeParam
from .company_event import CompanyEvent as CompanyEvent
from .introspection import Introspection as Introspection
from .payment_event import PaymentEvent as PaymentEvent
from .webhook_event import WebhookEvent as WebhookEvent
from .location_param import LocationParam as LocationParam
from .directory_event import DirectoryEvent as DirectoryEvent
from .employment_event import EmploymentEvent as EmploymentEvent
from .individual_event import IndividualEvent as IndividualEvent
from .base_webhook_event import BaseWebhookEvent as BaseWebhookEvent
from .disconnect_response import DisconnectResponse as DisconnectResponse
from .pay_statement_event import PayStatementEvent as PayStatementEvent
from .account_update_event import AccountUpdateEvent as AccountUpdateEvent
from .job_completion_event import JobCompletionEvent as JobCompletionEvent
from .access_token_create_params import AccessTokenCreateParams as AccessTokenCreateParams
from .create_access_token_response import CreateAccessTokenResponse as CreateAccessTokenResponse
from .request_forwarding_forward_params import RequestForwardingForwardParams as RequestForwardingForwardParams
Expand Down
Loading

0 comments on commit 2952dfa

Please sign in to comment.