|
| 1 | +import datetime |
| 2 | +from typing import Dict, Optional, Union |
| 3 | +import json |
| 4 | + |
| 5 | +from urbanairship import Airship |
| 6 | + |
| 7 | + |
| 8 | +class CustomEvent: |
| 9 | + def __init__( |
| 10 | + self, |
| 11 | + airship: Airship, |
| 12 | + name: Optional[str] = None, |
| 13 | + user: Optional[Dict] = None, |
| 14 | + interaction_type: Optional[str] = None, |
| 15 | + interaction_id: Optional[str] = None, |
| 16 | + properties: Optional[Dict] = None, |
| 17 | + session_id: Optional[str] = None, |
| 18 | + transaction: Optional[str] = None, |
| 19 | + value: Optional[Union[int, float]] = None, |
| 20 | + occurred: Optional[datetime.datetime] = None, |
| 21 | + ) -> None: |
| 22 | + """ |
| 23 | + A class representing an Airship custom event. Please see the |
| 24 | + documentation at https://docs.airship.com/api/ua/?http#tag-custom-events for |
| 25 | + details on Custom Event usage. |
| 26 | +
|
| 27 | + :param Airship: [required] An urbanairship.Airship instance initialized with |
| 28 | + bearer token authentication. |
| 29 | + :param name: [required] A plain-text name for the event. Airship's analytics |
| 30 | + systems will roll up events with the same name, providing counts and total |
| 31 | + value associated with the event. This value cannot contain upper-case |
| 32 | + characters. If the name contains upper-case characters, you will receive a |
| 33 | + 400 response. |
| 34 | + :param user: [required] An Airship channel identifier or named user |
| 35 | + for the user who triggered the event. |
| 36 | + :param interaction_id: [optional] The identifier defining where the event |
| 37 | + occurred. |
| 38 | + :param interaction_type: [optional] Describes the type of interaction that |
| 39 | + triggered the event |
| 40 | + :param properties: [optional] A dict containing custom event properties. |
| 41 | + :param session_id: [optional] The user session during which the event occurred. |
| 42 | + You must supply and maintain session identifiers. |
| 43 | + :param transaction: [optional] If the event is one in a series representing a |
| 44 | + single transaction, use the transaction field to tie events together. |
| 45 | + :param value: [optional] If the event is associated with a count or amount, |
| 46 | + the 'value' field carries that information. |
| 47 | + :param occurred: [optional The date and time when the event occurred. Events |
| 48 | + must have occurred within the past 90 days. You cannot provide |
| 49 | + a future datetime. |
| 50 | + """ |
| 51 | + self.airship = airship |
| 52 | + self.name = name |
| 53 | + self.user = user |
| 54 | + self.interaction_type = interaction_type |
| 55 | + self.interaction_id = interaction_id |
| 56 | + self.properties = properties |
| 57 | + self.session_id = session_id |
| 58 | + self.transaction = transaction |
| 59 | + self.value = value |
| 60 | + self.occurred = occurred |
| 61 | + |
| 62 | + @property |
| 63 | + def name(self) -> str: |
| 64 | + return self._name |
| 65 | + |
| 66 | + @name.setter |
| 67 | + def name(self, value: str) -> None: |
| 68 | + self._name = value |
| 69 | + |
| 70 | + @property |
| 71 | + def user(self) -> Dict: |
| 72 | + if "named_user" in self._user.keys(): |
| 73 | + return {"named_user_id": self._user["named_user"]} |
| 74 | + |
| 75 | + return self._user |
| 76 | + |
| 77 | + @user.setter |
| 78 | + def user(self, value: Dict) -> None: |
| 79 | + self._user = value |
| 80 | + |
| 81 | + @property |
| 82 | + def interaction_id(self) -> Optional[str]: |
| 83 | + return self._interaction_id |
| 84 | + |
| 85 | + @interaction_id.setter |
| 86 | + def interaction_id(self, value: str) -> None: |
| 87 | + self._interaction_id = value |
| 88 | + |
| 89 | + @property |
| 90 | + def interaction_type(self) -> Optional[str]: |
| 91 | + return self._interaction_type |
| 92 | + |
| 93 | + @interaction_type.setter |
| 94 | + def interaction_type(self, value: str) -> None: |
| 95 | + self._interaction_type = value |
| 96 | + |
| 97 | + @property |
| 98 | + def properties(self) -> Optional[Dict]: |
| 99 | + return self._properties |
| 100 | + |
| 101 | + @properties.setter |
| 102 | + def properties(self, value: Optional[Dict]) -> None: |
| 103 | + self._properties = value |
| 104 | + |
| 105 | + @property |
| 106 | + def session_id(self) -> Optional[str]: |
| 107 | + return self._session_id |
| 108 | + |
| 109 | + @session_id.setter |
| 110 | + def session_id(self, value: Optional[str]) -> None: |
| 111 | + self._session_id = value |
| 112 | + |
| 113 | + @property |
| 114 | + def transaciton(self) -> Optional[str]: |
| 115 | + return self._transaction |
| 116 | + |
| 117 | + @transaciton.setter |
| 118 | + def transaction(self, value: Optional[str]) -> None: |
| 119 | + self._transaction = value |
| 120 | + |
| 121 | + @property |
| 122 | + def value(self) -> Optional[Union[int, float]]: |
| 123 | + return self._value |
| 124 | + |
| 125 | + @value.setter |
| 126 | + def value(self, value: Optional[Union[int, float]]): |
| 127 | + self._value = value |
| 128 | + |
| 129 | + @property |
| 130 | + def occurred(self) -> Optional[str]: |
| 131 | + if not isinstance(self._occurred, datetime.datetime): |
| 132 | + return self._occurred |
| 133 | + return self._occurred.strftime("%Y-%m-%dT%H:%M:%S") |
| 134 | + |
| 135 | + @occurred.setter |
| 136 | + def occurred(self, value: Optional[datetime.datetime]) -> None: |
| 137 | + self._occurred = value |
| 138 | + |
| 139 | + @property |
| 140 | + def _payload(self) -> Dict: |
| 141 | + event_payload = {"user": self.user} |
| 142 | + body = {"name": self.name} |
| 143 | + |
| 144 | + for payload_attr in ["occurred"]: |
| 145 | + if getattr(self, payload_attr) is not None: |
| 146 | + event_payload[payload_attr] = getattr(self, payload_attr) |
| 147 | + |
| 148 | + for body_attr in [ |
| 149 | + "value", |
| 150 | + "transaction", |
| 151 | + "interaction_id", |
| 152 | + "interaction_type", |
| 153 | + "properties", |
| 154 | + "session_id", |
| 155 | + ]: |
| 156 | + if getattr(self, body_attr) is not None: |
| 157 | + body[body_attr] = getattr(self, body_attr) |
| 158 | + |
| 159 | + event_payload["body"] = body |
| 160 | + |
| 161 | + return event_payload |
| 162 | + |
| 163 | + def send(self) -> Dict: |
| 164 | + """Send the Custom Event to Airship systems |
| 165 | +
|
| 166 | + :returns: API response dict |
| 167 | + """ |
| 168 | + response = self.airship.request( |
| 169 | + method="POST", |
| 170 | + body=json.dumps(self._payload), |
| 171 | + url=self.airship.urls.get("custom_events_url"), |
| 172 | + content_type="application/json", |
| 173 | + version=3, |
| 174 | + ) |
| 175 | + |
| 176 | + return response.json() |
0 commit comments