o
    e~!                     @   s   d Z ddlmZmZmZ ddlmZmZ ddlZddl	Z
ddlZddlZddlZddlZddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ eeZdd Zdd Zdd Z	dddZdS )z=
This module provide http request function for bce services.
    )	iteritemsiterkeys
itervalues)strbytesN)compat)utils)BceResponse)BceHttpClientError)BceClientError)http_headersc                 C   sb   t |}| jtjjjkrtjj|||d dS | jtjj	jkr+tjj
|||d dS td|  )z
    :param protocol
    :type protocol: baidubce.protocol.Protocol
    :param endpoint
    :type endpoint: str
    :param connection_timeout_in_millis
    :type connection_timeout_in_millis int
    i  )hostporttimeoutz7Invalid protocol: %s, either HTTP or HTTPS is expected.)r   convert_to_stringnamebaidubceprotocolHTTPhttpclientHTTPConnectionZHTTPSHTTPSConnection
ValueError)r   r   r   Zconnection_timeout_in_millis r   MD:\Projects\ConvertPro\env\Lib\site-packages\baidubce/http/bce_http_client.py_get_connection#   s   
	

r   c                 C   s   t |}t |}| j||ddd t|D ]\}}t|}t|}| || q|   |ryt|t	t
frB| | |  S t|tj }d}	|	|k ry||	 }
|
|krY|}
||
}|sjtd|	tj|f | | |	t|7 }	|	|k sO|  S )NT)	skip_hostskip_accept_encodingr   z9Insufficient data, only %d bytes available while %s is %d)r   r   
putrequestr   r   Zconvert_to_standard_string	putheader
endheaders
isinstancer   r   sendintr   CONTENT_LENGTHreadr   lengetresponse)connhttp_methoduriheadersbodysend_buf_sizekvtotalsentsizebufr   r   r   _send_http_request8   s:   






r5   c                 C   sB   t | D ]\}}t|ttfrdt|v rtd||f qdS )zW
    check value in headers, if 
 in value, raise
    :param headers:
    :return:
       
z-There should not be any "\n" in header[%s]:%sN)r   r"   r   r   r   convert_to_bytesr   )r,   r/   r0   r   r   r   check_headersZ   s   r8   Fc	              
   C   s|  t d||||| |pi }dttjtjtjf }	|		dd}	t
|	}	|	|tj< d}
tj|vr4d}
| j}|r<| j}||tj< t|trL|tj}|sTd|tj< nt|trat||tj< ntj|vrmtdtj d	}t|d
r}t|dr}| }t|| j\}}}||tj< || jjkr|tj  dt
| 7  < || j|||||tj < t!|d}t|dkr|d | }n|}t"| d}g }	 d	}z|
du rt# |tj< || j|||||tj < |dkr|d	ur|$| t%|||| j&}t d||||| t'|||||| j(}|) }tj*rBt|t+rBg }|D ] \}}|d,d}|d,d}|- }|.||f q|}t d|j/|f  t0 }|1t2| |D ]}|||rd |W S qX|W S  t3y } zC|d	urz|4  |.d5dd t67 8 D  | j9:||r| j9;||}t<=|d  nt>d|d5|f |W Y d	}~nd	}~ww |d7 }q)aS  
    Send request to BCE services.

    :param config
    :type config: baidubce.BceClientConfiguration

    :param sign_function:

    :param response_handler_functions:
    :type response_handler_functions: list

    :param request:
    :type request: baidubce.internal.InternalRequest

    :return:
    :rtype: baidubce.BceResponse
    s   %s request start: %s %s, %s, %szbce-sdk-python/%s/%s/%s
 FTr   s   No %s is specified.Ntellseek   :   ?z=request args:method=%s, uri=%s, headers=%s,patams=%s, body=%szlatin-1zutf-8z%request return: status=%d, headers=%sc                 s   s    | ]}d | V  qdS )z>>>>Nr   ).0liner   r   r   	<genexpr>   s    zsend_request.<locals>.<genexpr>g     @@zEUnable to execute HTTP request. Retried %d times. All trace backs:
%s   )?_loggerdebugr   r   r   ZSDK_VERSIONsysversionplatformreplacer7   r   Z
USER_AGENTZBCE_DATEZendpointZbackup_endpointZHOSTr"   r   encodeZDEFAULT_ENCODINGr%   r   r'   r   hasattrr;   r   Zparse_host_portr   default_portcredentialsZAUTHORIZATIONZget_canonical_querystringr8   Zget_canonical_timer<   r   Zconnection_timeout_in_millsr5   r.   
getheadersPY3listdecodelowerappendstatusr	   Zset_metadata_from_headersdict	Exceptionclosejoin	traceback
format_exc
splitlinesZretry_policyZshould_retryZ%get_delay_before_next_retry_in_millistimesleepr
   )configZsign_functionZresponse_handler_functionsr*   pathr-   r,   paramsZuse_backup_endpoint
user_agentZshould_get_new_dateZrequest_endpointoffsetr   r   r   Zencoded_paramsr+   Zretries_attemptederrorsr)   http_responseZheaders_listZ
temp_headsr/   r0   responseZhandler_functioneZdelay_in_millisr   r   r   send_requestf   s   













"rf   )F)__doc__Zfuture.utilsr   r   r   builtinsr   r   logginghttp.clientr   rE   r[   rX   r   r   r   Zbaidubce.bce_responser	   Zbaidubce.exceptionr
   r   Zbaidubce.httpr   	getLogger__name__rC   r   r5   r8   rf   r   r   r   r   <module>   s*   
"