Ë
    Q^(hl  ã                   óØ  — 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ej"                  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y)#é    )Ú	dataclass)ÚEnum)ÚDictÚOptionalNé   )Ú	constantsé   )Úget_sessionÚhf_raise_for_statusÚvalidate_hf_hub_argsc                   ó   — e Zd ZdZdZy)ÚXetTokenTypeÚreadÚwriteN)Ú__name__Ú
__module__Ú__qualname__ÚREADÚWRITE© ó    úX/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/huggingface_hub/utils/_xet.pyr   r      s   „ Ø€DØ�Er   r   T)Úfrozenc                   ó"   — e Zd ZU eed<   eed<   y)ÚXetFileDataÚ	file_hashÚrefresh_routeN)r   r   r   ÚstrÚ__annotations__r   r   r   r   r      s   … àƒNØÔr   r   c                   ó,   — e Zd ZU eed<   eed<   eed<   y)ÚXetConnectionInfoÚaccess_tokenÚexpiration_unix_epochÚendpointN)r   r   r   r   r   Úintr   r   r   r!   r!      s   … àÓØÓØ„Mr   r!   ÚresponseÚreturnc                 ó0  — | €y	 | j                   t        j                     }t        j                  | j                  v r!| j                  t        j                     d   }n| j                   t        j
                     }t        ||¬«      S # t        $ r Y yw xY w)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ÚKeyErrorr   )r&   r   r   s      r   Ú!parse_xet_file_data_from_responser0      s“   € ð ÐØðØ×$Ñ$¤Y×%LÑ%LÑMˆ	ä×9Ñ9¸X¿^¹^ÑKØ$ŸN™N¬9×+YÑ+YÑZÐ[`Ña‰Mà$×,Ñ,¬Y×-]Ñ-]Ñ^ˆMô ØØ#ôð øô ò Ùðús   …A7B	 Â		BÂBr*   c                 óØ   — 	 | t         j                     }| t         j                     }t        | t         j                     «      }t        |||¬«      S # t
        t        t        f$ r Y yw xY w)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_EXPIRATIONr/   Ú
ValueErrorÚ	TypeErrorr!   )r*   r$   r"   r#   s       r   Ú&parse_xet_connection_info_from_headersr7   >   sp   € ðØœ9×FÑFÑGˆØœy×NÑNÑOˆÜ # G¬I×,YÑ,YÑ$ZÓ [Ðô ØØ!Ø3ôð øô ”j¤)Ð,ò Ùðús   ‚AA ÁA)Á(A)Ú	file_datac                 ó\   — | 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.
    z>The provided xet metadata does not contain a refresh endpoint.)r   r5   Ú#_fetch_xet_connection_info_with_url)r8   r*   s     r   Úrefresh_xet_connection_infor;   W   s/   € ð. ×ÑÐ&ÜÐYÓZÐZÜ.¨y×/FÑ/FÈÓPÐPr   )Úrevisionr$   ÚparamsÚ
token_typeÚrepo_idÚ	repo_typer<   r$   r=   c           	      óx   — |�|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.
    z/api/zs/z/xet-z-token/)r   ÚENDPOINTÚvaluer:   )r>   r?   r@   r<   r*   r$   r=   r)   s           r   Ú(fetch_xet_connection_info_from_repo_inforD   s   sP   € ðJ $Ð/‰x´Y×5GÑ5G€HØˆJ�e˜I˜; b¨¨	°°z×7GÑ7GÐ6HÈÐPXÈzÐ
Z€CÜ.¨s°G¸VÓDÐDr   r)   c                 ó˜   — t        «       j                  || |¬«      }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)   r=   z6Xet headers have not been correctly set by the server.)r
   Úgetr   r7   r*   r5   )r)   r*   r=   ÚrespÚmetadatas        r   r:   r:   �   sJ   € ô2 ‹=×Ñ W°#¸fÐÓE€DÜ˜Ôä5°d·l±lÓC€HØÐÜÐQÓRÐRØ€Or   )N)Údataclassesr   Úenumr   Útypingr   r   ÚrequestsÚ r   r
   r   r   r   r   r   r!   ÚResponser0   r7   r;   rD   r:   r   r   r   ú<module>rO      sÈ  ðÝ !Ý ß !ã å ß DÑ Dô�3˜ô ñ
 �$Ô÷ð ó ðñ
 �$Ô÷ð ó ðð°×0AÑ0Að ÀhÈ{ÑF[ó ðB°D¸¸c¸±Nð ÀxÐPaÑGbó ð2 ðQàðQð �#�s�(‰^ðQð ò	Qó ðQð6 ð #à"Ø'+ò&Eàð&Eð ð&Eð ð	&Eð
 �s‰mð&Eð �#�s�(‰^ð&Eð �s‰mð&Eð �T˜#˜s˜(‘^Ñ$ð&Eð ò&Eó ð&EðR ð (,ñØ	ðà�#�s�(‰^ðð �T˜#˜s˜(‘^Ñ$ðð ò	ó ñr   