
    K:g3-                         d dl Z d dlmZ d dlmZmZmZmZmZm	Z	m
Z
 d dlZd dlmZmZmZ d dlmZmZmZmZ d dlmZmZmZmZ erd dlmZ  G d d	      Zy)
    N)cached_property)TYPE_CHECKINGAnyDictListOptionalSequenceUnion)BaseCollaborators
BaseSchema
BaseShares)CreateWebhookCreateWebhookResponseWebhookWebhookSpecification)Url
UrlBuildercache_unless_forcedenterprise_onlyApic            
       F   e Zd ZU dZded<   eed<   ee   ed<   dZee   ed<   dZ	ee
   ed<   dZeeej                        ed	<    G d
 de      Z ee      Zddddedef   dedee   dee   fdZedee   fd       ZdefdZddddedededdfdZdddeded   fdZ	 d)dedeeeef      dee   ddfdZede
fd       Z dee!   fd Z"d!ede!fd"Z#d#ed$ee$eeef   f   de%fd%Z&e'ed*d&              Z(e'edeej                     fd'              Z)e'd+d(       Z*y),Basez
    Represents an Airtable base.

    Usage:
        >>> base = api.base("appNxslc6jG0XedVM")
        >>> table = base.table("Table Name")
        >>> records = table.all()
    r   apiidpermission_levelN_collaborators_schema_sharesc                   d    e Zd Z ed      Zedz  Zedz  Zedz  Zedz  Z ed      Z	de
defd	Zy
)
Base._urlszmeta/bases/{id}
interfacessharestablescollaboratorszbases/{id}/webhooksinterface_idreturnc                      | j                   |z  S )z`
            URL for retrieving information about a specific interface on the base.
            )r"   )selfr&   s     P/var/www/html/lionshead/venv/lib/python3.12/site-packages/pyairtable/api/base.py	interfacezBase._urls.interface>   s     ??\11    N)__name__
__module____qualname__r   metar"   r#   r$   r%   webhooksstrr+    r,   r*   _urlsr!   +   s[    $% L(
   . ,-	2# 	2# 	2r,   r4   )namer   base_idr5   c                    t        |t              r*t        j                  dt        d       ddlm}  ||      }|| _        || _        || _	        || _
        y)a  
        Old style constructor takes ``str`` arguments, and will create its own
        instance of :class:`Api`.

        This approach is deprecated, and will likely be removed in the future.

            >>> Base("access_token", "base_id")

        New style constructor takes an instance of :class:`Api`:

            >>> Base(api, "table_name")

        Args:
            api: An instance of :class:`Api` or an Airtable access token.
            base_id: An Airtable base ID.
            name: The name of the Airtable base, if known.
            permission_level: The permission level the current authenticated user
                has upon the Airtable base, if known.
        zPassing API keys to pyairtable.Base is deprecated; use Api.base() instead. See https://pyairtable.rtfd.org/en/latest/migrations.html for details.   )category
stacklevelr   r   N)
isinstancer2   warningswarnDeprecationWarning
pyairtabler   r   r   r   _name)r)   r   r6   r5   r   r   s         r*   __init__zBase.__init__F   sQ    6 c3MMZ+	 'c(C 0
r,   r'   c                 ^    | j                   r| j                   j                  S | j                  S )zw
        The name of the base, if provided to the constructor
        or available in cached base information.
        )r   r5   r@   r)   s    r*   r5   z	Base.namer   s)     &&+++zzr,   c                     d| j                   }| j                  x}r|d|z  }| j                  x}r|d|z  }|dz   S )Nz	<Base id=z name=z permission_level=>)r   r5   r   )r)   reprr5   r   s       r*   __repr__zBase.__repr__|   sa    477+&994gwKD#4444)(*++Dczr,   Fvalidateforce
id_or_namerI   rJ   zpyairtable.api.table.Tablec                    |rL| j                  |      j                  |      }t        j                  j                  j	                  d| |      S t        j                  j                  j	                  d| |      S )ak  
        Build a new :class:`Table` instance using this instance of :class:`Base`.

        Args:
            id_or_name: |arg_table_id_or_name|
            validate: |kwarg_validate_metadata|
            force: |kwarg_force_metadata|

        Usage:
            >>> base.table('Apartments')
            <Table base='appLkNDICXNqxSDhG' name='Apartments'>
        rJ   N)schematabler?   r   Table)r)   rK   rI   rJ   rN   s        r*   rO   z
Base.table   s`    & [[u[-33J?F>>''--dD&AA~~##))$jAAr,   rM   c                    | j                  |      j                  D cg c]-  }t        j                  j                  j                  d| |      / c}S c c}w )a  
        Retrieve the base's schema and returns a list of :class:`Table` instances.

        Args:
            force: |kwarg_force_metadata|

        Usage:
            >>> base.tables()
            [
                <Table base='appLkN...' id='tbltp8DGLhqbUmjK1' name='Apartments'>,
                <Table base='appLkN...' id='tblK6MZHez0ZvBChZ' name='Districts'>
            ]
        rM   N)rN   r$   r?   r   rO   rP   )r)   rJ   table_schemas      r*   r$   zBase.tables   sS      !%% 8 ? ?
 ? NN  &&tT<@ ?
 	
 
s   2Afieldsdescriptionc                     | j                   j                  }||d}|r||d<   | j                  j                  ||      }| j	                  |d   dd      S )ae  
        Create a table in the given base.

        Args:
            name: The unique table name.
            fields: A list of ``dict`` objects that conform to the
                `Airtable field model <https://airtable.com/developers/web/api/field-model>`__.
            description: The table description. Must be no longer than 20k characters.
        )r5   rS   rT   jsonr   TrH   )urlsr$   r   postrO   )r)   r5   rS   rT   urlpayloadresponses          r*   create_tablezBase.create_table   sY     ii62%0GM"88==7=3zz(4.4tzDDr,   c                     | j                   j                  }ddgi}| j                  j                  ||      }t	        j
                  || j                  |       S )a  
        Retrieve the schema of all tables in the base and caches it.

        Usage:
            >>> base.schema().tables
            [TableSchema(...), TableSchema(...), ...]
            >>> base.schema().table("tblXXXXXXXXXXXXXX")
            TableSchema(id="tblXXXXXXXXXXXXXX", ...)
            >>> base.schema().table("My Table")
            TableSchema(id="...", name="My Table", ...)
        includevisibleFieldIdsparamscontext)rX   r$   r   getr   from_api)r)   rZ   rb   datas       r*   rN   zBase.schema   sP     ii/01xx||C|/""44@@r,   c                     | j                   j                  | j                  j                        }|d   D cg c]$  }t	        j
                  || j                   |       & c}S c c}w )a  
        Retrieve all the base's webhooks
        (see: `List webhooks <https://airtable.com/developers/web/api/list-webhooks>`_).

        Usage:
            >>> base.webhooks()
            [
                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(...)
                )
            ]
        r1   rc   )r   re   rX   r1   r   rf   )r)   r\   rg   s      r*   r1   zBase.webhooks   s_    * 88<<		 2 23 !,
, T488T:,
 	
 
s   )A#
webhook_idc                 n    | j                         D ]  }|j                  |k(  s|c S  t        d|      )z
        Build a single webhook or raises ``KeyError`` if the given ID is invalid.

        Airtable's API does not permit retrieving a single webhook, so this function
        will call :meth:`~webhooks` and simply return one item from the list.
        zwebhook not found: )r1   r   KeyError)r)   ri   webhooks      r*   rl   zBase.webhook   s:     }}GzzZ' ' ,ZN;<<r,   
notify_urlspecc                 D   t        |t              r t        j                  || j                        }t        ||      }|j                  dd      }| j                  j                  | j                  j                  |      }t        j                  || j                        S )aX  
        Create a webhook on the base with the given
        `webhooks specification <https://airtable.com/developers/web/api/model/webhooks-specification>`_.

        The return value will contain a unique secret that must be saved
        in order to validate payloads as they are sent to your notification
        endpoint. If you do not save this, you will have no way of confirming
        that payloads you receive did, in fact, come from Airtable.

        For more on how to validate notifications to your webhook, see
        :meth:`WebhookNotification.from_request() <pyairtable.models.WebhookNotification.from_request>`.

        Usage:
            >>> base.add_webhook(
            ...     "https://example.com",
            ...     {
            ...         "options": {
            ...             "filters": {
            ...                 "dataTypes": ["tableData"],
            ...             }
            ...         }
            ...     }
            ... )
            CreateWebhookResponse(
                id='ach00000000000001',
                mac_secret_base64='c3VwZXIgZHVwZXIgc2VjcmV0',
                expiration_time=datetime.datetime(...)
            )

        Raises:
            pydantic.ValidationError: If the dict provided is invalid.

        Args:
            notify_url: The URL where Airtable will POST all event notifications.
            spec: The configuration for the webhook. It is easiest to pass a dict
                that conforms to the `webhooks specification`_ but you
                can also provide :class:`~pyairtable.models.webhook.WebhookSpecification`.
        )notification_urlspecificationT)by_aliasexclude_unsetrV   )r;   dictr   rf   r   r   
model_dumprY   rX   r1   r   )r)   rm   rn   createrequestr\   s         r*   add_webhookzBase.add_webhook   s}    V dD!'00txx@D
$O##T#F88==!3!3'=B$--hAAr,   c                     dg di}| j                   j                  | j                  j                  |      }t	        j
                  || j                   |       S )zs
        Retrieve `base collaborators <https://airtable.com/developers/web/api/get-base-collaborators>`__.
        r_   )r%   inviteLinksr"   ra   rc   )r   re   rX   r0   r   rf   )r)   rb   rg   s      r*   r%   zBase.collaborators1  sF     KLxx||DIINN6|: ))$$GGr,   c                     | j                   j                  | j                  j                        }t	        j
                  || j                   |       }|j                  S )za
        Retrieve `base shares <https://airtable.com/developers/web/api/list-shares>`__.
        rc   )r   re   rX   r#   r   rf   )r)   rg   
shares_objs      r*   r#   zBase.shares;  sD     xx||DII,,-((txxF
   r,   c                 b    | j                   j                  | j                  j                         y)z
        Delete the base.

        Usage:
            >>> base = api.base("appMxESAta6clCCwF")
            >>> base.delete()
        N)r   deleterX   r0   rC   s    r*   r~   zBase.deleteE  s     			'r,   )N)r'   r   )r'   N)+r-   r.   r/   __doc____annotations__r2   r   r   r   r   r   r   r   r   Infor   r4   r   rX   r
   rA   propertyr5   rG   boolrO   r$   r	   r   r   r]   r   rN   r   r1   rl   r   r   rx   r   r%   r#   r~   r3   r,   r*   r   r      sV    
J 	G sm# 37NH./6$(GXj!(/3GXd:??+,32
 22 5!D #*.*5#:* *
 sm* #3-*X hsm  #  BB 	B
 B 
&B0 ', 
t 
5Q0R 
. &*	EE c3h(E c]	E
 
&E, A
 A A"
$w- 
6
=# 
=' 
=1B1B ($sCx.891B 
	1Bf H  H !Z__- !  ! ( (r,   r   )r<   	functoolsr   typingr   r   r   r   r   r	   r
   pyairtable.api.tabler?   pyairtable.models.schemar   r   r   pyairtable.models.webhookr   r   r   r   pyairtable.utilsr   r   r   r   pyairtable.api.apir   r   r3   r,   r*   <module>r      sB     % L L L  N N  S R&{( {(r,   