§
    ¬˜ºh�  ã                   óh  — d dl mZ d dlmZ d dlmZmZ d dlZddlm	Z	 ddlm
Z
mZmZ  G d	„ d
ee¦  «        Z ed¬¦  «         G d„ d¦  «        ¦   «         Z ed¬¦  «         G d„ d¦  «        ¦   «         Z	 d"dej        dee         dee         fd„Zdeeef         dee         fd„Zededeeef         defd„¦   «         Zeddddœdedededee         deeef         dee         deeeef                  defd„¦   «         Ze	 d"d edeeef         deeeef                  defd!„¦   «         ZdS )#é    )Ú	dataclass)ÚEnum)ÚDictÚOptionalNé   )Ú	constantsé   )Úget_sessionÚhf_raise_for_statusÚvalidate_hf_hub_argsc                   ó   — e Zd ZdZdZdS )ÚXetTokenTypeÚreadÚwriteN)Ú__name__Ú
__module__Ú__qualname__ÚREADÚWRITE© ó    úb/var/www/html/services/rag-service/venv/lib/python3.11/site-packages/huggingface_hub/utils/_xet.pyr   r      s   € € € € € Ø€DØ€E€E€Er   r   T)Úfrozenc                   ó$   — e Zd ZU eed<   eed<   dS )ÚXetFileDataÚ	file_hashÚrefresh_routeN)r   r   r   ÚstrÚ__annotations__r   r   r   r   r      s'   € € € € € € à€N€N�NØÐÐÑÐÐr   r   c                   ó.   — e Zd ZU eed<   eed<   eed<   dS )ÚXetConnectionInfoÚaccess_tokenÚexpiration_unix_epochÚendpointN)r   r   r   r   r   Úintr   r   r   r!   r!      s1   € € € € € € àÐÐÑØÐÐÑØ€M€M�M€M€Mr   r!   Úresponser$   Úreturnc                 óú  — | €dS 	 | j         t          j                 }t          j        | j        v r| j        t          j                 d         }n| j         t          j                 }n# t          $ r Y dS w xY w|�|nt          j        }|                     t          j	        ¦  «        rF| 
                    t          j	                             d¦  «        |                     d¦  «        ¦  «        }t          ||¬¦  «        S )aa  
    Parse XET file metadata from an HTTP response.

    This function extracts XET file metadata from the HTTP headers or HTTP links
    of a given response object. If the required metadata is not found, it returns `None`.

    Args:
        response (`requests.Response`):
            The HTTP response object containing headers dict and links dict to extract the XET metadata from.
    Returns:
        `Optional[XetFileData]`:
            An instance of `XetFileData` containing the file hash and refresh route if the metadata
            is found. Returns `None` if the required metadata is missing.
    NÚurlú/)r   r   )Úheadersr   ÚHUGGINGFACE_HEADER_X_XET_HASHÚ$HUGGINGFACE_HEADER_LINK_XET_AUTH_KEYÚlinksÚ&HUGGINGFACE_HEADER_X_XET_REFRESH_ROUTEÚKeyErrorÚENDPOINTÚ
startswithÚHUGGINGFACE_CO_URL_HOMEÚreplaceÚrstripr   )r&   r$   r   r   s       r   Ú!parse_xet_file_data_from_responser6      sý   € ð" ÐØˆtðØÔ$¥YÔ%LÔMˆ	åÔ9¸X¼^ÐKÐKØ$œN­9Ô+YÔZÐ[`ÔaˆMˆMà$Ô,­YÔ-]Ô^ˆMøøÝð ð ð Øˆtˆtðøøøà#Ð/ˆxˆxµYÔ5G€HØ×Ò¥	Ô AÑBÔBð sØ%×-Ò-­iÔ.O×.VÒ.VÐWZÑ.[Ô.[Ð]e×]lÒ]lÐmpÑ]qÔ]qÑrÔrˆÝØØ#ðñ ô ð s   †AA& Á&
A4Á3A4r+   c                 óì   — 	 | t           j                 }| t           j                 }t          | t           j                 ¦  «        }n# t
          t          t          f$ r Y dS w xY wt          |||¬¦  «        S )a„  
    Parse XET connection info from the HTTP headers or return None if not found.
    Args:
        headers (`Dict`):
           HTTP headers to extract the XET metadata from.
    Returns:
        `XetConnectionInfo` or `None`:
            The information needed to connect to the XET storage service.
            Returns `None` if the headers do not contain the XET connection info.
    N)r$   r"   r#   )	r   Ú!HUGGINGFACE_HEADER_X_XET_ENDPOINTÚ%HUGGINGFACE_HEADER_X_XET_ACCESS_TOKENr%   Ú#HUGGINGFACE_HEADER_X_XET_EXPIRATIONr0   Ú
ValueErrorÚ	TypeErrorr!   )r+   r$   r"   r#   s       r   Ú&parse_xet_connection_info_from_headersr=   B   s…   € ðØ�9ÔFÔGˆØ�yÔNÔOˆÝ # G­IÔ,YÔ$ZÑ [Ô [ÐÐøÝ•j¥)Ð,ð ð ð Øˆtˆtðøøøõ ØØ!Ø3ðñ ô ð s   ‚AA ÁA!Á A!Ú	file_datac                 óX   — | j         €t          d¦  «        ‚t          | j         |¦  «        S )a  
    Utilizes the information in the parsed metadata to request the Hub xet connection information.
    This includes the access token, expiration, and XET service URL.
    Args:
        file_data: (`XetFileData`):
            The file data needed to refresh the xet connection information.
        headers (`Dict[str, str]`):
            Headers to use for the request, including authorization headers and user agent.
    Returns:
        `XetConnectionInfo`:
            The connection information needed to make the request to the xet storage service.
    Raises:
        [`~utils.HfHubHTTPError`]
            If the Hub API returned an error.
        [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
            If the Hub API response is improperly formatted.
    Nz>The provided xet metadata does not contain a refresh endpoint.)r   r;   Ú#_fetch_xet_connection_info_with_url)r>   r+   s     r   Úrefresh_xet_connection_inforA   [   s/   € ð. ÔÐ&ÝÐYÑZÔZÐZÝ.¨yÔ/FÈÑPÔPÐPr   )Úrevisionr$   ÚparamsÚ
token_typeÚrepo_idÚ	repo_typerB   rC   c           	      ón   — |�|nt           j        }|› d|› d|› d| j        › d|› �	}t          |||¦  «        S )aÅ  
    Uses the repo info to request a xet access token from Hub.
    Args:
        token_type (`XetTokenType`):
            Type of the token to request: `"read"` or `"write"`.
        repo_id (`str`):
            A namespace (user or an organization) and a repo name separated by a `/`.
        repo_type (`str`):
            Type of the repo to upload to: `"model"`, `"dataset"` or `"space"`.
        revision (`str`, `optional`):
            The revision of the repo to get the token for.
        headers (`Dict[str, str]`):
            Headers to use for the request, including authorization headers and user agent.
        endpoint (`str`, `optional`):
            The endpoint to use for the request. Defaults to the Hub endpoint.
        params (`Dict[str, str]`, `optional`):
            Additional parameters to pass with the request.
    Returns:
        `XetConnectionInfo`:
            The connection information needed to make the request to the xet storage service.
    Raises:
        [`~utils.HfHubHTTPError`]
            If the Hub API returned an error.
        [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
            If the Hub API response is improperly formatted.
    Nz/api/zs/z/xet-z-token/)r   r1   Úvaluer@   )rD   rE   rF   rB   r+   r$   rC   r)   s           r   Ú(fetch_xet_connection_info_from_repo_inforI   w   sW   € ðJ $Ð/ˆxˆxµYÔ5G€HØÐ
ZÐ
Z˜IÐ
ZÐ
Z¨Ð
ZÐ
Z°zÔ7GÐ
ZÐ
ZÐPXÐ
ZÐ
Z€CÝ.¨s°G¸VÑDÔDÐDr   r)   c                 ó¶   — t          ¦   «                              || |¬¦  «        }t          |¦  «         t          |j        ¦  «        }|€t          d¦  «        ‚|S )aS  
    Requests the xet connection info from the supplied URL. This includes the
    access token, expiration time, and endpoint to use for the xet storage service.
    Args:
        url: (`str`):
            The access token endpoint URL.
        headers (`Dict[str, str]`):
            Headers to use for the request, including authorization headers and user agent.
        params (`Dict[str, str]`, `optional`):
            Additional parameters to pass with the request.
    Returns:
        `XetConnectionInfo`:
            The connection information needed to make the request to the xet storage service.
    Raises:
        [`~utils.HfHubHTTPError`]
            If the Hub API returned an error.
        [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
            If the Hub API response is improperly formatted.
    )r+   r)   rC   Nz6Xet headers have not been correctly set by the server.)r
   Úgetr   r=   r+   r;   )r)   r+   rC   ÚrespÚmetadatas        r   r@   r@   ¡   sY   € õ2 ‰=Œ=×Ò W°#¸fÐÑEÔE€DÝ˜ÑÔÐå5°d´lÑCÔC€HØÐÝÐQÑRÔRÐRØ€Or   )N)Údataclassesr   Úenumr   Útypingr   r   ÚrequestsÚ r   r
   r   r   r   r   r   r!   ÚResponser6   r=   rA   rI   r@   r   r   r   ú<module>rT      s  ðØ !Ð !Ð !Ð !Ð !Ð !Ø Ð Ð Ð Ð Ð Ø !Ð !Ð !Ð !Ð !Ð !Ð !Ð !à €€€à Ð Ð Ð Ð Ð Ø DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ Dðð ð ð ð �3˜ñ ô ð ð
 €�$ÐÑÔðð ð ð ð ñ ô ñ Ôðð
 €�$ÐÑÔðð ð ð ð ñ ô ñ Ôðð <@ð"ð "ØÔð"Ø+3°C¬=ð"àˆkÔð"ð "ð "ð "ðJ°D¸¸c¸´Nð ÀxÐPaÔGbð ð ð ð ð2 ðQàðQð �#�s�(Œ^ðQð ð	Qð Qð Qñ ÔðQð6 ð #à"Ø'+ð&Eð &Eð &Eàð&Eð ð&Eð ð	&Eð
 �sŒmð&Eð �#�s�(Œ^ð&Eð �sŒmð&Eð �T˜#˜s˜(”^Ô$ð&Eð ð&Eð &Eð &Eñ Ôð&EðR ð (,ðð Ø	ðà�#�s�(Œ^ðð �T˜#˜s˜(”^Ô$ðð ð	ð ð ñ Ôðð ð r   