| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
import json |
| 4 |
from typing import Any, ClassVar, Dict, Optional, cast |
| 5 |
|
| 6 |
from typing_extensions import Literal, TYPE_CHECKING |
| 7 |
|
| 8 |
from stripe._stripe_object import StripeObject |
| 9 |
from stripe._util import get_api_mode |
| 10 |
from stripe._stripe_context import StripeContext |
| 11 |
|
| 12 |
if TYPE_CHECKING: |
| 13 |
from stripe._stripe_client import StripeClient |
| 14 |
|
| 15 |
|
| 16 |
# The beginning of the section generated from our OpenAPI spec |
| 17 |
class Event(StripeObject): |
| 18 |
""" |
| 19 |
Events are generated to keep you informed of activity in your business account. APIs in the /v2 namespace generate [thin events](https://docs.stripe.com/event-destinations#benefits-of-thin-events) which have small, unversioned payloads that include a reference to the ID of the object that has changed. The Events v2 API returns these new thin events. [Retrieve the event object](https://docs.stripe.com/event-destinations#fetch-data) for additional data about the event. Use the related object ID in the event payload to [fetch the API resource](https://docs.stripe.com/event-destinations#retrieve-the-object-associated-with-thin-events) of the object associated with the event. Comparatively, events generated by most API v1 include a versioned snapshot of an API object in their payload. |
| 20 |
""" |
| 21 |
|
| 22 |
OBJECT_NAME: ClassVar[Literal["v2.core.event"]] = "v2.core.event" |
| 23 |
|
| 24 |
class Reason(StripeObject): |
| 25 |
class Request(StripeObject): |
| 26 |
id: str |
| 27 |
""" |
| 28 |
ID of the API request that caused the event. |
| 29 |
""" |
| 30 |
idempotency_key: str |
| 31 |
""" |
| 32 |
The idempotency key transmitted during the request. |
| 33 |
""" |
| 34 |
|
| 35 |
request: Optional[Request] |
| 36 |
""" |
| 37 |
Information on the API request that instigated the event. |
| 38 |
""" |
| 39 |
type: Literal["request"] |
| 40 |
""" |
| 41 |
Event reason type. |
| 42 |
""" |
| 43 |
_inner_class_types = {"request": Request} |
| 44 |
|
| 45 |
changes: Optional[Dict[str, Any]] |
| 46 |
""" |
| 47 |
Before and after changes for the primary related object. |
| 48 |
""" |
| 49 |
context: Optional[str] |
| 50 |
""" |
| 51 |
Authentication context needed to fetch the event or related object. |
| 52 |
""" |
| 53 |
created: str |
| 54 |
""" |
| 55 |
Time at which the object was created. |
| 56 |
""" |
| 57 |
id: str |
| 58 |
""" |
| 59 |
Unique identifier for the event. |
| 60 |
""" |
| 61 |
livemode: bool |
| 62 |
""" |
| 63 |
Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode. |
| 64 |
""" |
| 65 |
object: Literal["v2.core.event"] |
| 66 |
""" |
| 67 |
String representing the object's type. Objects of the same type share the same value of the object field. |
| 68 |
""" |
| 69 |
reason: Optional[Reason] |
| 70 |
""" |
| 71 |
Reason for the event. |
| 72 |
""" |
| 73 |
type: str |
| 74 |
""" |
| 75 |
The type of the event. |
| 76 |
""" |
| 77 |
_inner_class_types = {"reason": Reason} |
| 78 |
|
| 79 |
|
| 80 |
# The end of the section generated from our OpenAPI spec |
| 81 |
|
| 82 |
|
| 83 |
class ReasonRequest: |
| 84 |
id: str |
| 85 |
idempotency_key: str |
| 86 |
|
| 87 |
def __init__(self, d) -> None: |
| 88 |
self.id = d["id"] |
| 89 |
self.idempotency_key = d["idempotency_key"] |
| 90 |
|
| 91 |
def __repr__(self) -> str: |
| 92 |
return f"<ReasonRequest id={self.id} idempotency_key={self.idempotency_key}>" |
| 93 |
|
| 94 |
|
| 95 |
class Reason: |
| 96 |
type: Literal["request"] |
| 97 |
request: Optional[ReasonRequest] = None |
| 98 |
|
| 99 |
def __init__(self, d) -> None: |
| 100 |
self.type = d["type"] |
| 101 |
if self.type == "request": |
| 102 |
self.request = ReasonRequest(d["request"]) |
| 103 |
|
| 104 |
def __repr__(self) -> str: |
| 105 |
return f"<Reason type={self.type} request={self.request}>" |
| 106 |
|
| 107 |
|
| 108 |
class RelatedObject: |
| 109 |
id: str |
| 110 |
type: str |
| 111 |
url: str |
| 112 |
|
| 113 |
def __init__(self, d) -> None: |
| 114 |
self.id = d["id"] |
| 115 |
self.type = d["type"] |
| 116 |
self.url = d["url"] |
| 117 |
|
| 118 |
def __repr__(self) -> str: |
| 119 |
return f"<RelatedObject id={self.id} type={self.type} url={self.url}>" |
| 120 |
|
| 121 |
|
| 122 |
class EventNotification: |
| 123 |
""" |
| 124 |
EventNotification represents the json that's delivered from an Event Destination. It's a basic struct-like object with a few convenience methods. Use `fetch_event()` to get the full event object. |
| 125 |
""" |
| 126 |
|
| 127 |
id: str |
| 128 |
""" |
| 129 |
Unique identifier for the event. |
| 130 |
""" |
| 131 |
type: str |
| 132 |
""" |
| 133 |
The type of the event. |
| 134 |
""" |
| 135 |
created: str |
| 136 |
""" |
| 137 |
Time at which the object was created. |
| 138 |
""" |
| 139 |
livemode: bool |
| 140 |
""" |
| 141 |
Livemode indicates if the event is from a production(true) or test(false) account. |
| 142 |
""" |
| 143 |
context: Optional[StripeContext] = None |
| 144 |
""" |
| 145 |
[Optional] Authentication context needed to fetch the event or related object. |
| 146 |
""" |
| 147 |
reason: Optional[Reason] = None |
| 148 |
""" |
| 149 |
[Optional] Reason for the event. |
| 150 |
""" |
| 151 |
|
| 152 |
def __init__( |
| 153 |
self, parsed_body: Dict[str, Any], client: "StripeClient" |
| 154 |
) -> None: |
| 155 |
self.id = parsed_body["id"] |
| 156 |
self.type = parsed_body["type"] |
| 157 |
self.created = parsed_body["created"] |
| 158 |
self.livemode = bool(parsed_body.get("livemode")) |
| 159 |
context_value = parsed_body.get("context") |
| 160 |
if context_value: |
| 161 |
self.context = StripeContext.parse(context_value) |
| 162 |
|
| 163 |
if parsed_body.get("reason"): |
| 164 |
self.reason = Reason(parsed_body["reason"]) |
| 165 |
|
| 166 |
self._client = client |
| 167 |
|
| 168 |
@staticmethod |
| 169 |
def from_json(payload: str, client: "StripeClient") -> "EventNotification": |
| 170 |
""" |
| 171 |
Helper for constructing an Event Notification. Doesn't perform signature validation, so you |
| 172 |
should use StripeClient.parseEventNotification() instead for initial handling. |
| 173 |
This is useful in unit tests and working with EventNotifications that you've already validated the authenticity of. |
| 174 |
""" |
| 175 |
parsed_body = json.loads(payload) |
| 176 |
|
| 177 |
# circular import busting |
| 178 |
from stripe.events._event_classes import ( |
| 179 |
get_v2_event_notification_class, |
| 180 |
) |
| 181 |
|
| 182 |
event_class = get_v2_event_notification_class(parsed_body["type"]) |
| 183 |
|
| 184 |
return event_class(parsed_body, client) |
| 185 |
|
| 186 |
def __repr__(self) -> str: |
| 187 |
return f"<EventNotification id={self.id} type={self.type} created={self.created} context={self.context} reason={self.reason}>" |
| 188 |
|
| 189 |
def fetch_event(self) -> Event: |
| 190 |
response = self._client.raw_request( |
| 191 |
"get", |
| 192 |
f"/v2/core/events/{self.id}", |
| 193 |
stripe_context=self.context, |
| 194 |
headers={"Stripe-Request-Trigger": f"event={self.id}"}, |
| 195 |
usage=["pushed_event_pull"], |
| 196 |
) |
| 197 |
return cast(Event, self._client.deserialize(response, api_mode="V2")) |
| 198 |
|
| 199 |
async def fetch_event_async(self) -> Event: |
| 200 |
response = await self._client.raw_request_async( |
| 201 |
"get", |
| 202 |
f"/v2/core/events/{self.id}", |
| 203 |
stripe_context=self.context, |
| 204 |
headers={"Stripe-Request-Trigger": f"event={self.id}"}, |
| 205 |
usage=["pushed_event_pull", "pushed_event_pull_async"], |
| 206 |
) |
| 207 |
return cast(Event, self._client.deserialize(response, api_mode="V2")) |
| 208 |
|
| 209 |
|
| 210 |
class UnknownEventNotification(EventNotification): |
| 211 |
""" |
| 212 |
Represents an EventNotification payload that the SDK doesn't have types for. May have a related object. |
| 213 |
""" |
| 214 |
|
| 215 |
related_object: Optional[RelatedObject] = None |
| 216 |
""" |
| 217 |
[Optional] Object containing the reference to API resource relevant to the event. |
| 218 |
""" |
| 219 |
|
| 220 |
def __init__( |
| 221 |
self, parsed_body: Dict[str, Any], client: "StripeClient" |
| 222 |
) -> None: |
| 223 |
super().__init__(parsed_body, client) |
| 224 |
|
| 225 |
if parsed_body.get("related_object"): |
| 226 |
self.related_object = RelatedObject(parsed_body["related_object"]) |
| 227 |
|
| 228 |
def fetch_related_object(self) -> Optional[StripeObject]: |
| 229 |
if self.related_object is None: |
| 230 |
return None |
| 231 |
|
| 232 |
response = self._client.raw_request( |
| 233 |
"get", |
| 234 |
self.related_object.url, |
| 235 |
stripe_context=self.context, |
| 236 |
headers={"Stripe-Request-Trigger": f"event={self.id}"}, |
| 237 |
usage=["fetch_related_object", "unknown_event"], |
| 238 |
) |
| 239 |
return self._client.deserialize( |
| 240 |
response, |
| 241 |
api_mode=get_api_mode(self.related_object.url), |
| 242 |
) |
| 243 |
|
| 244 |
async def fetch_related_object_async(self) -> Optional[StripeObject]: |
| 245 |
if self.related_object is None: |
| 246 |
return None |
| 247 |
|
| 248 |
response = await self._client.raw_request_async( |
| 249 |
"get", |
| 250 |
self.related_object.url, |
| 251 |
stripe_context=self.context, |
| 252 |
headers={"Stripe-Request-Trigger": f"event={self.id}"}, |
| 253 |
usage=["fetch_related_object", "unknown_event"], |
| 254 |
) |
| 255 |
return self._client.deserialize( |
| 256 |
response, |
| 257 |
api_mode=get_api_mode(self.related_object.url), |
| 258 |
) |