o
    /ѹgW                     @   s\  d dl Z d dlmZ d dlmZ d dlZd dlmZ d dlm	Z	 d dl
mZ ee jdZG dd	 d	eZG d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdejfddZdd Zdejfdd Zd!d" Z	#d.dejd$ee d%efd&d'Zd(d$ Z d)ejfd*d+Z!d,d- Z"dS )/    N)
HTTPStatus)Optional)	HTTPError)MODELSCOPE_REQUEST_ID)
get_logger)	log_levelc                   @      e Zd ZdS )NotSupportErrorN__name__
__module____qualname__ r   r   Y/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/site-packages/modelscope/hub/errors.pyr	          r	   c                   @   r   )NoValidRevisionErrorNr
   r   r   r   r   r      r   r   c                   @   r   )NotExistErrorNr
   r   r   r   r   r      r   r   c                   @   r   )RequestErrorNr
   r   r   r   r   r      r   r   c                   @   r   )GitErrorNr
   r   r   r   r   r       r   r   c                   @   r   )InvalidParameterNr
   r   r   r   r   r   $   r   r   c                   @   r   )NotLoginExceptionNr
   r   r   r   r   r   (   r   r   c                   @   r   )FileIntegrityErrorNr
   r   r   r   r   r   ,   r   r   c                   @   r   )FileDownloadErrorNr
   r   r   r   r   r   0   r   r   responsec                 C   s   t | jjv r| jjt  S dS )N )r   requestheadersr   r   r   r   get_request_id4   s   r   c                 C   s   | d t jko
| d S )z Check the request is ok

    Args:
        rsp (Response): The request response body

    Returns:
       bool: `True` if success otherwise `False`.
    CodeZSuccessN)r   OKrspr   r   r   is_ok;   s   	r#   c                 C   s.   d| j ddv r|  }|S | jd}|S )Nzapplication/jsonzcontent-typer   utf-8)r   getjsoncontentdecode)r   messager   r   r   _decode_response_errorG   s
   r*   c              
   C   sJ   z|    W d S  ty$ } zt| }td|||t| f |d }~ww )NzHRequest %s with body: %s exception, Response details: %s, request id: %s)raise_for_statusr   r*   r   )r   urlrequest_bodyerrorr)   r   r   r   handle_http_post_errorO   s   r/   Traise_on_errorreturnc                 C   s  d}t | jtr z| jd}W n ty   | jd}Y nw | j}t| }d| jkr1d| }n?d| jkrA|d u r>d}	 n2d}n/d	| j  krLd
k rYn nd| j||| jf }nd
| j  krddk rpn n
d| j||| jf }|r|r|| t	|| d| jS )Nr   r$   
iso-8859-1  z%The request model: %s does not exist!  z%Authentication token does not exist, zGThe authentication token is invalid, failed to access model {model_id}.    /%s Client Error: %s, Request id: %s for url: %sX  0%s Server Error: %s, Request id: %s, for url: %sr   )

isinstancereasonbytesr(   UnicodeDecodeErrorr   status_coder,   r.   r   )r   loggercookiesZmodel_idr0   http_error_msgr;   
request_idr   r   r   handle_http_responseZ   s:   



rC   c                 C   s   | d t jkr	dS t| d )zIf response error, raise exception

    Args:
        rsp (_type_): The server response

    Raises:
        RequestError: the response error message.

    Returns:
        bool: True if request is OK, otherwise raise `RequestError` exception.
    r   TMessageN)r   r    r   r!   r   r   r   r0      s   http_responsec              
   C   sF   | dtjkr
dS |d }td|  d| d|d  d|d  d		)aQ  If response error, raise exception

    Args:
        url (str): The request url
        rsp (HTTPResponse): The server response.
        http_response: the origin http response.

    Raises:
        RequestError: the http request error.

    Returns:
        bool: `True` if request is OK, otherwise raise `RequestError` exception.
    r   TZ	RequestIdzUrl = z, Request id=z Code = z Message = rD   zC,                Please specify correct dataset_name and namespace.N)r%   r   r    r   )r,   r"   rE   rB   r   r   r   datahub_raise_on_error   s   $rF   c                 C   s  d}t | jtr z| jd}W n ty   | jd}Y nw | j}t| }d| jkr6d}d| j|f  n7d| jkr>d}n/d	| j  krId
k rVn nd| j||| jf }nd
| j  kradk rmn n
d| j||| jf }|r| j}|j	dkr~d||j
f }t|| ddS )zAttempt to decode utf-8 first since some servers
    localize reason strings, for invalid utf-8, fall back
    to decoding with iso-8859-1.

    Args:
        rsp: The http response.

    Raises:
        HTTPError: The http error info.
    r   r$   r2   r3   z7The request resource(model or dataset) does not exist!,zurl: %s, reason: %sr4   z/Authentication token does not exist or invalid.r5   r6   r7   r8   r9   POSTz%s, body: %sr   N)r:   r;   r<   r(   r=   r   r>   r,   r   methodbodyr   )r"   rA   r;   rB   reqr   r   r   raise_for_http_status   s8   


rK   )T)#logginghttpr   typingr   requestsZrequests.exceptionsr   Zmodelscope.hub.constantsr   Zmodelscope.utils.loggerr   WARNINGr?   	Exceptionr	   r   r   r   r   r   r   r   r   Responser   r#   r*   r/   boolintrC   r0   rF   rK   r   r   r   r   <module>   s<   

%