o
    .j                      @  s   d Z ddlmZ ddlmZmZ ddlmZmZm	Z	 zddl
Zddl
mZ W n ey5   ddlZdZY nw ddlmZ ddlmZmZ eG d	d
 d
ZdddZddddZddddZdS )u   
JMAP session establishment (RFC 8620 §2).

Fetches the Session object from /.well-known/jmap and extracts the
information needed to make subsequent API calls.
    )annotations)	dataclassfield)urljoinurlparse
urlunparseN)AsyncSession)CALENDAR_CAPABILITY)JMAPAuthErrorJMAPCapabilityErrorc                   @  s`   e Zd ZU dZded< ded< ded< eedZded< eedZded	< eedZ	ded
< dS )Sessionu7  Parsed JMAP Session object (RFC 8620 §2).

    Attributes:
        api_url: URL to POST method calls to.
        account_id: The accountId to use for calendar method calls.
            Chosen from ``primaryAccounts`` if available, otherwise the first
            account advertising the calendars capability.
        state: Current session state string.
        account_capabilities: Capabilities dict for the chosen account.
        server_capabilities: Server-level capabilities dict.
        raw: The full parsed Session JSON for anything not captured above.
    strapi_url
account_idstate)default_factorydictaccount_capabilitiesserver_capabilitiesrawN)
__name__
__module____qualname____doc____annotations__r   r   r   r   r    r   r   H/home/thesage/.local/lib/python3.10/site-packages/caldav/jmap/session.pyr      s   
 r   urlr   datar   returnc                 C  sN  | d}|st| ddt| |}t| }t|}|j|jkr7|j|jks,|j|jkr7t|j|j|j	d}| dd}| di }| di }d }i }	| d	i  t
}
|
rl| |
i }| d
i }t
|v rl|
}|}	|d u r| D ]\}}| d
i }t
|v r|}|}	 nqt|d u rt| dt
dt|  dt||||	||dS )NapiUrlz!Session response missing 'apiUrl'r   reason)schemenetlocr    capabilitiesaccountsprimaryAccountsaccountCapabilitiesz!No account found with capability z. Available accounts: )r   r   r   r   r   r   )getr   r   r   hostnameportr#   r   _replacer$   r	   itemslistkeysr   )r   r   r   session_parsed
api_parsedr   r   r'   r   r   primary_acct_id	acct_datacapsacct_idr   r   r   _parse_session_data0   sb   


r7      timeoutintc                 C  sL   t j| |ddi|d}|jdv rt| d|j dd|  t| | S )a  Fetch and parse the JMAP Session object.

    Performs a GET request to ``url`` (expected to be ``/.well-known/jmap``
    or equivalent), authenticates with ``auth``, and returns a parsed
    :class:`Session`.

    Args:
        url: Full URL to the JMAP session endpoint.
        auth: A requests-compatible auth object (e.g. HTTPBasicAuth,
              HTTPBearerAuth).

    Returns:
        Parsed :class:`Session` with ``api_url`` and ``account_id`` set.

    Raises:
        JMAPAuthError: If the server returns HTTP 401 or 403.
        JMAPCapabilityError: If no account advertises the calendars capability.
        requests.HTTPError: For other non-2xx responses.
    Acceptapplication/jsonauthheadersr9   i  i  HTTP  from session endpointr!   )requestsr*   status_coder
   raise_for_statusr7   json)r   r>   r9   responser   r   r   fetch_sessionq   s
   
rH   c              	     s   t  4 I dH }|j| |ddi|dI dH }W d  I dH  n1 I dH s'w   Y  |jdv r<t| d|j dd|  t| | S )	a  Async variant of :func:`fetch_session` using niquests.AsyncSession.

    Args:
        url: Full URL to the JMAP session endpoint.
        auth: A niquests-compatible auth object.

    Returns:
        Parsed :class:`Session` with ``api_url`` and ``account_id`` set.

    Raises:
        JMAPAuthError: If the server returns HTTP 401 or 403.
        JMAPCapabilityError: If no account advertises the calendars capability.
    Nr;   r<   r=   r@   rA   rB   r!   )r   r*   rD   r
   rE   r7   rF   )r   r>   r9   sessionrG   r   r   r   async_fetch_session   s   (
rJ   )r   r   r   r   r   r   )r8   )r   r   r9   r:   r   r   )r   
__future__r   dataclassesr   r   urllib.parser   r   r   niquestsrC   r   ImportErrorcaldav.jmap.constantsr	   caldav.jmap.errorr
   r   r   r7   rH   rJ   r   r   r   r   <module>   s$    
A