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

feat(webhooks): add types to the unwrap method #258

Merged
merged 1 commit into from
Jan 11, 2024
Merged
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
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