o
    ߥi                     @   s@  d dl 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ejfd"d#Zd$d% Zd&ejfd'd(Zd)d* ZdS )+    N)
HTTPStatus)	HTTPError)MODELSCOPE_REQUEST_ID)
get_logger)	log_levelc                   @      e Zd ZdS )NotSupportErrorN__name__
__module____qualname__ r   r   I/home/ubuntu/.local/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   /   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_id3   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`.
    CodeSuccess)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_errorF   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_errorN   s   r/   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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}|| t	|| dd 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cookiesmodel_idhttp_error_msgr9   
request_idr   r   r   handle_http_responseY   s:   



rB   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Message)r   r    r   r!   r   r   r   raise_on_errory   s   rD   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   T	RequestIdzUrl = z, Request id=z Code = z Message = rC   zC,                Please specify correct dataset_name and namespace.)r%   r   r    r   )r,   r"   rE   rA   r   r   r   datahub_raise_on_error   s   $rG   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$   r0   r1   z7The request resource(model or dataset) does not exist!,zurl: %s, reason: %sr2   z/Authentication token does not exist or invalid.r3   r4   r5   r6   r7   POSTz%s, body: %sr   N)r8   r9   r:   r(   r;   r   r<   r,   r   methodbodyr   )r"   r@   r9   rA   reqr   r   r   raise_for_http_status   s8   


rL   )logginghttpr   requestsrequests.exceptionsr   modelscope.hub.constantsr   modelscope.utils.loggerr   WARNINGr=   	Exceptionr   r   r   r   r   r   r   r   r   Responser   r#   r*   r/   rB   rD   rG   rL   r   r   r   r   <module>   s0    