
    K:g4                         U d dl Z d dlmZ d dlmZ d dlmZ d dlmZmZm	Z	m
Z
mZmZmZmZ d dlZd dlmZ d dlmZ d dlmZmZmZ  eej2                  e	      Zeg ef   ed
<    eej2                  e	      Zeg ef   ed<    G d ded      Z G d de      Z  G d de      Z! G d de      Z" G d de      Z# G d de      Z$ G d de      Z% G d de      Z& G d de      Z' G d  d!e      Z( e e)              y)"    N)datetime)partial)HMAC)AnyCallableDictIteratorListLiteralOptionalUnion)Self)RecordId)AirtableModelCanDeleteModelrebuild_models)default_factoryFDFLc            	           e Zd ZU dZeed<   eed<   eed<   eed<   dZe	e
   ed<   dZe	e   ed<   dZe	d	   ed
<   dZe	e
   ed<   ded<   ddZddZddZ	 ddddede	e   ded   fdZy)Webhooka  
    A webhook that has been retrieved from the Airtable API.

    >>> spec = {
    ...     "options": {
    ...         "filters": {
    ...             "dataTypes": ["tableData"],
    ...         }
    ...     }
    ... }
    >>> base.add_webhook("https://example.com", spec)
    CreateWebhookResponse(
        id='ach00000000000001',
        mac_secret_base64='c3VwZXIgZHVwZXIgc2VjcmV0',
        expiration_time=datetime.datetime(...)
    )
    >>> webhooks = base.webhooks()
    >>> webhooks[0]
    Webhook(
        id='ach00000000000001',
        are_notifications_enabled=True,
        cursor_for_next_payload=1,
        is_hook_enabled=True,
        last_successful_notification_time=None,
        notification_url="https://example.com",
        last_notification_result=None,
        expiration_time=datetime.datetime(...),
        specification: WebhookSpecification(...)
    )
    >>> webhooks[0].disable_notifications()
    >>> webhooks[0].enable_notifications()
    >>> webhooks[0].extend_expiration()
    >>> webhooks[0].delete()
    idare_notifications_enabledcursor_for_next_payloadis_hook_enabledN!last_successful_notification_timenotification_urlWebhookNotificationResultlast_notification_resultexpiration_timeWebhookSpecificationspecificationreturnc                 ^    | j                   j                  d| j                   dddi       y)z
        Turn on notifications for this webhook.
        See `Enable/disable webhook notifications <https://airtable.com/developers/web/api/enable-disable-webhook-notifications>`_.
        POST/enableNotificationsenableTjsonN_apirequest_urlselfs    V/var/www/html/lionshead/venv/lib/python3.12/site-packages/pyairtable/models/webhook.pyenable_notificationszWebhook.enable_notifications@   s3    
 			tyyk!56h=M 	 	
    c                 ^    | j                   j                  d| j                   dddi       y)zo
        Turn off notifications for this webhook.
        See `Enable/disable webhook notifications`_.
        r%   r&   r'   Fr(   Nr*   r.   s    r0   disable_notificationszWebhook.disable_notificationsI   s3    
 			tyyk!56h=N 	 	
r2   c                     | j                   j                  d| j                   d      }|j                  d      | _        y)z
        Extend the life of a webhook by seven days.
        See `Refresh a webhook <https://airtable.com/developers/web/api/refresh-a-webhook>`_.
        r%   z/refreshexpirationTimeN)r+   r,   r-   getr    )r/   responses     r0   extend_expirationzWebhook.extend_expirationR   s7    
 99$$V		{(-CD'||,<=r2   )limitcursorr:   WebhookPayloadc             #     K   |dk  rt        d      ||dk  rt        d      | j                   d}d|i}d}| j                  j                  d||d	      D ]t  }|d
   }t	        |      D ]E  \  }}	t
        j                  |	| j                  |       }	||z   |	_        |	 |dz  }|>||k\  sD  y |r|j                  d      s y|d   }v yw)a  
        Iterate through all payloads on or after the given cursor.
        See :class:`~pyairtable.models.WebhookPayload`. Each payload will
        contain an extra attribute, ``cursor``, which you will need to store
        if you want to later resume retrieving payloads after that point.

        For more details on the mechanisms of retrieving webhook payloads,
        or to find more information about the data structures you'll get back,
        see `List webhook payloads <https://airtable.com/developers/web/api/list-webhook-payloads>`_.

        Args:
            cursor: The cursor of the first webhook payload to retrieve.
            limit: The number of payloads to yield before stopping.
                If not provided, will retrieve all remaining payloads.

        Usage:
            >>> webhook = Base.webhook("ach00000000000001")
            >>> iter_payloads = webhook.payloads()
            >>> next(iter_payloads)
            WebhookPayload(
                timestamp=datetime.datetime(...),
                base_transaction_number=4,
                payload_format="v0",
                action_metadata=ActionMetadata(
                    source="client",
                    source_metadata={
                        "user": {
                            "id": "usr00000000000000",
                            "email": "foo@bar.com",
                            "permissionLevel": "create"
                        }
                    }
                ),
                changed_tables_by_id={},
                created_tables_by_id={},
                destroyed_table_ids=["tbl20000000000000", "tbl20000000000001"],
                error=None,
                error_code=None,
                cursor=1
            )
           zcursor must be non-zeroNzlimit must be non-zeroz	/payloadsr;   r   GET)methodurloptionsoffset_fieldpayloads)contextmightHaveMore)	
ValueErrorr-   r+   iterate_requests	enumerater<   from_apir;   r7   )
r/   r;   r:   rA   rB   countpagerD   indexpayloads
             r0   rD   zWebhook.payloadsZ   s     X A:6775669%V$II..!	 / 
D J'H"+H"5w(11'499d1S!'%
$% #6 /!:(^F#
s   B'C*C0!C)r#   N)r>   )__name__
__module____qualname____doc__str__annotations__boolintr   r   r   r   r   r    r1   r4   r9   r	   rD    r2   r0   r   r      s    !F 	G##  <@%x'9@&*hsm*FJh'BCJ*.OXh'.))

> E$9=E$E$)1#E$	"	#E$r2   r   z"bases/{base.id}/webhooks/{self.id})rA   c                       e Zd ZU eed<   y)	_NestedIdr   NrO   rP   rQ   rS   rT   rW   r2   r0   rY   rY      s    Gr2   rY   c            	       \    e Zd ZU dZeed<   eed<   eed<   ededede	e
ef   defd	       Zy
)WebhookNotificationaa  
    Represents the value that Airtable will POST to the webhook's notification URL.

    This will not contain the full webhook payload; it will only contain the IDs
    of the base and the webhook which triggered the notification. You will need to
    use :meth:`Webhook.payloads <pyairtable.models.Webhook.payloads>` to retrieve
    the actual payloads describing the change(s) which triggered the webhook.

    See `Webhook notification delivery <https://airtable.com/developers/web/api/webhooks-overview#webhook-notification-delivery>`_
    for more information on how these payloads are structured.
    basewebhook	timestampbodyheadersecretr#   c                 
   t        |t              r$t        j                  |j	                  d            }t        ||j	                  d      d      }d|j                         z   }||k7  rt        d      | j                  |      S )a  
        Validate a request body and X-Airtable-Content-MAC header
        using the secret returned when the webhook was created.

        Args:
            body: The full request body sent over the wire.
            header: The request's X-Airtable-Content-MAC header.
            secret: The MAC secret provided when the webhook was created.
                If ``str``, it's assumed this is still base64-encoded;
                if ``bytes``, it's assumed that this has been decoded.

        Returns:
            :class:`~WebhookNotification`: An instance parsed from the provided request body.

        Raises:
            ValueError: if the header and body do not match the secret.
        asciisha256zhmac-sha256=z/X-Airtable-Content-MAC header failed validation)	
isinstancerS   base64decodebytesencoder   	hexdigestrG   model_validate_json)clsr`   ra   rb   hmacexpecteds         r0   from_requestz WebhookNotification.from_request   st    0 fc"''g(>?FFDKK0(;!DNN$44XNOO&&t,,r2   N)rO   rP   rQ   rR   rY   rT   r   classmethodrS   r   bytesSelfTypero   rW   r2   r0   r\   r\      s[    
 O-- - eSj!	-
 
- -r2   r\   c                   ^    e Zd ZU eed<   eed<   eed<   eed<   dZe	e   ed<   dZ
e	d   ed<   y)	r   successcompletion_timestampduration_msretry_numberNwill_be_retriedWebhookErrorerror)rO   rP   rQ   rU   rT   r   floatrV   rx   r   rz   rW   r2   r0   r   r      s6    M""&*OXd^*&*E8N#*r2   r   c                       e Zd ZU eed<   y)ry   messageNrZ   rW   r2   r0   ry   ry      s    Lr2   ry   c                   p    e Zd ZU ded<    G d de      Z G d de      Z G d de      Z G d	 d
e      Zy)r!   WebhookSpecification.OptionsrB   c                   ,    e Zd ZU ded<   dZed   ed<   y)r   WebhookSpecification.FiltersfiltersNWebhookSpecification.Includesincludes)rO   rP   rQ   rT   r   r   rW   r2   r0   OptionszWebhookSpecification.Options   s    //>B(:;Br2   r   c                       e Zd ZU ee   ed<   dZee   ed<    e       Z	ee   ed<    e       Z
ee   ed<   dZed   ed<    e       Zee   ed<    e       Zee   ed	<   y)
r   
data_typesNrecord_change_scopechange_typesfrom_sources"WebhookSpecification.SourceOptionssource_optionswatch_data_in_field_idswatch_schemas_of_field_ids)rO   rP   rQ   r
   rS   rT   r   r   r   r   r   r   r   r   rW   r2   r0   FilterszWebhookSpecification.Filters   sm    I-1Xc]1"$$d3i&"$$d3i&IM!EFM-/Tc102"DI4r2   r   c                   b    e Zd ZU dZed   ed<   dZed   ed<    G d de      Z G d de      Z	y)r   NFormSubmissionform_submissionFormPageSubmissionform_page_submissionc                       e Zd ZU eed<   y)1WebhookSpecification.SourceOptions.FormSubmissionview_idNrZ   rW   r2   r0   r   r          Lr2   c                       e Zd ZU eed<   y)5WebhookSpecification.SourceOptions.FormPageSubmissionpage_idNrZ   rW   r2   r0   r   r      r   r2   )
rO   rP   rQ   r   r   rT   r   r   r   r   rW   r2   r0   SourceOptionsz"WebhookSpecification.SourceOptions   s;    6:"23:?Ch';<C	] 		 	r2   r   c                   P    e Zd ZU dZedee   ed   f   ed<   dZ	e
ed<   dZe
ed<   y)r   Nall include_cell_values_in_field_idsFinclude_previous_cell_values"include_previous_field_definitions)rO   rP   rQ   r   r   r
   rS   r   rT   r   rU   r   rW   r2   r0   IncludeszWebhookSpecification.Includes   s7    SW(%d3i0O*PW-2$d238*D8r2   r   N)	rO   rP   rQ   rT   r   r   r   r   r   rW   r2   r0   r!   r!      s:    ++C- C5- 5 9= 9r2   r!   c                   ,    e Zd ZU dZee   ed<   eed<   y)CreateWebhookNr   r"   )rO   rP   rQ   r   r   rS   rT   r!   rW   r2   r0   r   r     s    &*hsm*''r2   r   c                   :    e Zd ZU dZeed<   eed<   dZee   ed<   y)CreateWebhookResponsez
    Payload returned by :meth:`Base.add_webhook <pyairtable.Base.add_webhook>`
    which includes the base64-encoded MAC secret that you'll need in order to
    verify the authenticity of future webhook requests.
    r   mac_secret_base64Nr    )	rO   rP   rQ   rR   rS   rT   r    r   r   rW   r2   r0   r   r   
  s'     	G  +/OXh'.r2   r   c                      e Zd ZU dZeed<   eed<   eed<   dZe	d   ed<    e
       Zeedf   ed	<    e
       Zeed
f   ed<    e       Zee   ed<   dZe	e   ed<    ej(                  dd      Ze	e   ed<   dZe	e   ed<    G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d  d!e      Z G d" d#e      Z  G d$ d%e      Z!y)&r<   z
    Payload returned by :meth:`Webhook.payloads`. See API docs:
    `Webhooks payload <https://airtable.com/developers/web/api/model/webhooks-payload>`_.
    r_   base_transaction_numberpayload_formatNWebhookPayload.ActionMetadataaction_metadataWebhookPayload.TableChangedchanged_tables_by_idWebhookPayload.TableCreatedcreated_tables_by_iddestroyed_table_idsrz   code)aliasdefault
error_coder;   c                   :    e Zd ZU eed<    e       Zeeef   ed<   y)r   sourcesource_metadataN)	rO   rP   rQ   rS   rT   r   r   r   r   rW   r2   r0   ActionMetadatazWebhookPayload.ActionMetadata4  s    *,$c3h.r2   r   c                   0    e Zd ZU dZeed<   dZee   ed<   y)WebhookPayload.TableInfo nameNdescription)rO   rP   rQ   r   rS   rT   r   r   rW   r2   r0   	TableInfor   8  s    c%)Xc])r2   r   c                   6    e Zd ZU dZee   ed<   dZee   ed<   y)WebhookPayload.FieldInfoNr   type)rO   rP   rQ   r   r   rS   rT   r   rW   r2   r0   	FieldInfor   <  s    "hsm""hsm"r2   r   c                   ,    e Zd ZU ded<   dZed   ed<   y)WebhookPayload.FieldChangedr   currentNprevious)rO   rP   rQ   rT   r   r   rW   r2   r0   FieldChangedr   @  s    ++9=(56=r2   r   c                      e Zd ZU  e       Zeedf   ed<    e       Zeedf   ed<    e       Z	ee
df   ed<    e       Zeedf   ed<    e       Zee
d	f   ed
<   dZed   ed<    e       Zee   ed<    e       Zee
   ed<    G d de      Zy)r   WebhookPayload.ViewChangedchanged_views_by_idr   changed_fields_by_idWebhookPayload.RecordChangedchanged_records_by_idr   created_fields_by_idWebhookPayload.RecordCreatedcreated_records_by_idN+WebhookPayload.TableChanged.ChangedMetadatachanged_metadatadestroyed_field_idsdestroyed_record_idsc                   "    e Zd ZU ded<   ded<   y)r   r   r   r   N)rO   rP   rQ   rT   rW   r2   r0   ChangedMetadataz+WebhookPayload.TableChanged.ChangedMetadataN  s    //00r2   r   )rO   rP   rQ   r   r   r   rS   rT   r   r   r   r   r   r   r   r   r   r
   r   r   r   rW   r2   r0   TableChangedzWebhookPayload.TableChangedD  s    GItT#'C"CDKIKd3(E#EFMPRPTtH.L$LMTFHdd3(B#BCJPRPTtH.L$LMTTX(#PQX)+T#Y-/1td8n3	1m 	1r2   r   c                   p    e Zd ZU  e       Zeedf   ed<    e       Zeedf   ed<    e	       Z
ee   ed<   y)r   r   r   r   r   r   N)rO   rP   rQ   r   r   r   r   rT   r   r   r   r
   rW   r2   r0   ViewChangedzWebhookPayload.ViewChangedR  sE    PRPTtH.L$LMTPRPTtH.L$LMT/1td8n3r2   r   c                   f    e Zd ZU dZed   ed<    e       Zee	df   ed<    e       Z
eedf   ed<   y)r   Nr   metadatar   fields_by_idr   records_by_id)rO   rP   rQ   r   r   rT   r   r   r   rS   r   r   rW   r2   r0   TableCreatedzWebhookPayload.TableCreatedW  s@    9=(56=>@dd3 ::;BHJtH&DDELr2   r   c                   @    e Zd ZU ded<   dZed   ed<   dZed   ed<   y)r   "WebhookPayload.CellValuesByFieldIdr   Nr   	unchanged)rO   rP   rQ   rT   r   r   r   rW   r2   r0   RecordChangedzWebhookPayload.RecordChanged\  s(    55CG(?@GDH	8@AHr2   r   c                   "    e Zd ZU eeef   ed<   y)r   cell_values_by_field_idN)rO   rP   rQ   r   rS   r   rT   rW   r2   r0   CellValuesByFieldIdz"WebhookPayload.CellValuesByFieldIda  s    !%c3h/r2   r   c                   ,    e Zd ZU eed<   eeef   ed<   y)r   created_timer   N)rO   rP   rQ   r   rT   r   rS   r   rW   r2   r0   RecordCreatedzWebhookPayload.RecordCreatedd  s    !%c3h/r2   r   )"rO   rP   rQ   rR   r   rT   rV   rS   r   r   r   r   r   r   r   r   r
   rz   rU   pydanticFieldr   r;   r   r   r   r   r   r   r   r   r   r   r   rW   r2   r0   r<   r<     s!   
   AEOX=>EEGT$s$AABIEGT$s$AABI%'Tc) E8D>  .VT JJJ !FHSM / /*M *#M #>} >1} 14m 4
M} M
I I
0m 00 0r2   r<   c                   2    e Zd ZU eed<   eed<   ee   ed<   y)WebhookPayloadsr;   might_have_morerD   N)rO   rP   rQ   rV   rT   rU   r
   r<   rW   r2   r0   r   r   i  s    K>""r2   r   )*rg   r   	functoolsr   rm   r   typingr   r   r   r	   r
   r   r   r   r   typing_extensionsr   rr   pyairtable.api.typesr   pyairtable.models._baser   r   r   r   dictr   rT   listr   r   rY   r\   r   ry   r!   r   r   r<   r   varsrW   r2   r0   <module>r      s        P P P  . ) Q Q  EHRW EEHRW EM$n"F M$` /-- /-d+ += 9= 9@(M (
/M /$J0] J0Z#m # tv r2   