|
| 1 | +from seamapi.types import ( |
| 2 | + AbstractSeam as Seam, |
| 3 | + AbstractWebhooks, |
| 4 | + Webhook, |
| 5 | + WebhookId, |
| 6 | +) |
| 7 | +import time |
| 8 | +from typing import List, Union, Optional, cast |
| 9 | +import requests |
| 10 | +from seamapi.utils.convert_to_id import ( |
| 11 | + to_connect_webview_id, |
| 12 | + to_connected_account_id, |
| 13 | + to_device_id, |
| 14 | + to_webhook_id, |
| 15 | +) |
| 16 | +from seamapi.utils.report_error import report_error |
| 17 | + |
| 18 | + |
| 19 | +class Webhooks(AbstractWebhooks): |
| 20 | + """ |
| 21 | + A class used to interact with webhooks API |
| 22 | +
|
| 23 | + ... |
| 24 | +
|
| 25 | + Attributes |
| 26 | + ---------- |
| 27 | + seam : Seam |
| 28 | + Initial seam class |
| 29 | +
|
| 30 | + Methods |
| 31 | + ------- |
| 32 | + create(url, event_types=None) |
| 33 | + Creates a new webhook |
| 34 | + delete(webhook_id) |
| 35 | + Deletes a webhook |
| 36 | + get(webhook_id) |
| 37 | + Fetches a webhook |
| 38 | + list() |
| 39 | + Lists webhooks |
| 40 | + """ |
| 41 | + |
| 42 | + seam: Seam |
| 43 | + |
| 44 | + def __init__(self, seam: Seam): |
| 45 | + """ |
| 46 | + Parameters |
| 47 | + ---------- |
| 48 | + seam : Seam |
| 49 | + Initial seam class |
| 50 | + """ |
| 51 | + |
| 52 | + self.seam = seam |
| 53 | + |
| 54 | + @report_error |
| 55 | + def create( |
| 56 | + self, |
| 57 | + url: str, |
| 58 | + event_types: Optional[list] = None, |
| 59 | + ) -> Webhook: |
| 60 | + """Creates a new webhook. |
| 61 | +
|
| 62 | + Parameters |
| 63 | + ---------- |
| 64 | + url : str |
| 65 | + URL to send webhook events to |
| 66 | + event_types : Optional[List[str]] |
| 67 | + List of event types to send to webhook eg. ["connected_account.connected"]. Defaults to ["*"] |
| 68 | +
|
| 69 | + Raises |
| 70 | + ------ |
| 71 | + Exception |
| 72 | + If the API request wasn't successful. |
| 73 | +
|
| 74 | + Returns |
| 75 | + ------ |
| 76 | + A webhook. |
| 77 | + """ |
| 78 | + create_payload = {"url": url} |
| 79 | + if event_types is not None: |
| 80 | + create_payload["event_types"] = event_types |
| 81 | + |
| 82 | + res = self.seam.make_request( |
| 83 | + "POST", |
| 84 | + "/webhooks/create", |
| 85 | + json=create_payload, |
| 86 | + ) |
| 87 | + |
| 88 | + return Webhook.from_dict(res["webhook"]) |
| 89 | + |
| 90 | + @report_error |
| 91 | + def delete( |
| 92 | + self, |
| 93 | + webhook: Union[WebhookId, Webhook], |
| 94 | + ) -> bool: |
| 95 | + """Deletes a webhook. |
| 96 | +
|
| 97 | + Parameters |
| 98 | + ---------- |
| 99 | + webhook : Union[WebhookId, Webhook] |
| 100 | + Webhook ID or Webhook |
| 101 | +
|
| 102 | + Raises |
| 103 | + ------ |
| 104 | + Exception |
| 105 | + If the API request wasn't successful. |
| 106 | +
|
| 107 | + Returns |
| 108 | + ------ |
| 109 | + Boolean. |
| 110 | + """ |
| 111 | + |
| 112 | + res = self.seam.make_request( |
| 113 | + "DELETE", |
| 114 | + "/webhooks/delete", |
| 115 | + json={"webhook_id": to_webhook_id(webhook)}, |
| 116 | + ) |
| 117 | + |
| 118 | + return True |
| 119 | + |
| 120 | + @report_error |
| 121 | + def get( |
| 122 | + self, |
| 123 | + webhook: Union[WebhookId, Webhook], |
| 124 | + ) -> Webhook: |
| 125 | + """Fetches a webhook. |
| 126 | +
|
| 127 | + Parameters |
| 128 | + ---------- |
| 129 | + webhook : Union[WebhookId, Webhook] |
| 130 | + Webhook ID or Webhook |
| 131 | +
|
| 132 | + Raises |
| 133 | + ------ |
| 134 | + Exception |
| 135 | + If the API request wasn't successful. |
| 136 | +
|
| 137 | + Returns |
| 138 | + ------ |
| 139 | + A webhook. |
| 140 | + """ |
| 141 | + |
| 142 | + res = self.seam.make_request( |
| 143 | + "GET", |
| 144 | + "/webhooks/get", |
| 145 | + params={"webhook_id": to_webhook_id(webhook)}, |
| 146 | + ) |
| 147 | + |
| 148 | + return Webhook.from_dict(res["webhook"]) |
| 149 | + |
| 150 | + @report_error |
| 151 | + def list( |
| 152 | + self, |
| 153 | + ) -> List[Webhook]: |
| 154 | + """Lists webhooks. |
| 155 | +
|
| 156 | + Raises |
| 157 | + ------ |
| 158 | + Exception |
| 159 | + If the API request wasn't successful. |
| 160 | +
|
| 161 | + Returns |
| 162 | + ------ |
| 163 | + A list of webhooks. |
| 164 | + """ |
| 165 | + |
| 166 | + res = self.seam.make_request( |
| 167 | + "GET", |
| 168 | + "/webhooks/list", |
| 169 | + ) |
| 170 | + |
| 171 | + return [Webhook.from_dict(w) for w in res["webhooks"]] |
0 commit comments