
    BTh 6                     Z   d Z ddlZddlmZmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ  ej                  e      Z G d	 d
      Zd Zd Z G d de      Z G d d      Zej,                  j/                         D ]  \  ZZ eee      s eeee         G d de      Z G d de      Zy)z*Stubs for patching HTTP and HTTPS requests    N)HTTPConnectionHTTPResponseHTTPSConnection)BytesIO)(CannotOverwriteExistingCassetteException)Request   )compatc                   "    e Zd ZdZd Zd Zd Zy)VCRFakeSocketz{
    A socket that doesn't do anything!
    Used when playing back cassettes, when there
    is no actual open socket.
    c                      y N selfs    d/var/www/catia.catastroantioquia-mas.com/valormas/lib/python3.12/site-packages/vcr/stubs/__init__.pyclosezVCRFakeSocket.close           c                      y r   r   r   argskwargss      r   
settimeoutzVCRFakeSocket.settimeout   r   r   c                      y)z
        This is kinda crappy.  requests will watch
        this descriptor and make sure it's not closed.
        Return file descriptor 0 since that's stdin.
        r   r   r   s    r   filenozVCRFakeSocket.fileno   s     r   N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r      s    r   r   c                     d}| j                         D ]8  \  }}|D ].  }||j                  d      dz   |j                  d      z   dz   z  }0 : t        j                  |      S )z[
    Convert headers from our serialized dict with lists for keys to a
    HTTPMessage
    r   zutf-8   :s   
)itemsencoder
   get_httpmessage)header_listheader_stringkeyvaluesvs        r   parse_headersr+   %   sv    
 M"((* VV 	VASZZ047!((7:KKgUUM	VV !!-00r   c                     | j                   | j                  n| j                   }i }t        j                  |      D ]+  \  }}|j	                  |g        ||   j                  |       - |S r   )msgheadersr
   get_headers
setdefaultextend)responser.   outr(   r)   s        r   serialize_headersr4   1   sd    "*,,"6hHLLG
C))'2  VsBC  Jr   c                       e Zd ZdZd Zed        Zd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd ZddZd Zed        Zd Zed        Zd ZddZy)VCRHTTPResponsezJ
    Stub response class that gets returned instead of a HTTPResponse
    c                    d | _         || _        |d   d   | _        |d   d   x| _        | _        d | _        d | _        t        | j                  d   d         | _        d| _	        | | _
        | j                  d   }|j                         D cg c]  }|j                         dk(  s| }}|r||d	   = t        |      x| _        | _        t!        j"                  | j                  d
      xs d | _        y c c}w )NstatusmessagecodebodystringFr.   zTRANSFER-ENCODINGr   zcontent-length)fprecorded_responsereasonr8   r:   versionversion_stringr   _content_closed_original_responsekeysupperr+   r.   r-   r
   
get_headerlength)r   r>   r.   hte_keys        r   __init__zVCRHTTPResponse.__init__?   s    !2'1)<"3H"=f"EEdi" 6 6v >x HI"&((3 %\\^Pqwwy<O/O!PPq	""/"88tx''2BCKt Qs   C5&C5c                     | j                   S r   rC   r   s    r   closedzVCRHTTPResponse.closedW   s    
 ||r   c                 :     | j                   j                  |i |S r   rB   readr   s      r   rQ   zVCRHTTPResponse.read^       !t}}!!42622r   c                 :     | j                   j                  |i |S r   )rB   read1r   s      r   rT   zVCRHTTPResponse.read1a   s    "t}}""D3F33r   c                 6    | j                   j                         S r   )rB   readallr   s    r   rV   zVCRHTTPResponse.readalld   s    }}$$&&r   c                 :     | j                   j                  |i |S r   )rB   readintor   s      r   rX   zVCRHTTPResponse.readintog       %t}}%%t6v66r   c                 :     | j                   j                  |i |S r   )rB   readliner   s      r   r[   zVCRHTTPResponse.readlinej   rY   r   c                 :     | j                   j                  |i |S r   )rB   	readlinesr   s      r   r]   zVCRHTTPResponse.readlinesm   s    &t}}&&777r   c                 6    | j                   j                         S r   )rB   seekabler   s    r   r_   zVCRHTTPResponse.seekablep       }}%%''r   c                 6    | j                   j                         S r   )rB   tellr   s    r   rb   zVCRHTTPResponse.tells   s    }}!!##r   c                 6    | j                   j                         S r   )rB   isattyr   s    r   rd   zVCRHTTPResponse.isattyv   s    }}##%%r   c                 :     | j                   j                  |i |S r   )rB   seekr   s      r   rf   zVCRHTTPResponse.seeky   rR   r   c                     d| _         y)NTrM   r   s    r   r   zVCRHTTPResponse.close|   s    r   c                     | j                   S r   )r8   r   s    r   getcodezVCRHTTPResponse.getcode       {{r   c                     | j                   S r   )rN   r   s    r   isclosedzVCRHTTPResponse.isclosed   rj   r   c                 2    t        | j                  d         S Nr.   )r+   r>   r   s    r   infozVCRHTTPResponse.info   s    T33I>??r   c                 n    t        | j                  d         }t        t        j                  |            S rn   )r+   r>   listr
   get_header_items)r   r9   s     r   
getheaderszVCRHTTPResponse.getheaders   s-     6 6y ABF++G455r   Nc                     | j                         D cg c])  \  }}|j                         |j                         k(  s(|+ }}}|rdj                  |      S |S c c}}w )Nz, )rs   lowerjoin)r   headerdefaultkr*   r)   s         r   	getheaderzVCRHTTPResponse.getheader   sP    "&//"3SAqwwyFLLN7R!SS99V$$N Ts
   )AAc                 6    | j                   j                         S r   )rB   readabler   s    r   r|   zVCRHTTPResponse.readable   r`   r   c                     | j                   j                         j                  | j                   j                         z
  S r   )rB   	getbuffernbytesrb   r   s    r   length_remainingz VCRHTTPResponse.length_remaining   s-    }}&&(//$--2D2D2FFFr   c                 T    d| j                   cxk  rdk  sy y| j                  d      S )a(  
        Returns (a) redirect location string if we got a redirect
        status code and valid location, (b) None if redirect status and
        no location, (c) False if not a redirect status code.
        See https://urllib3.readthedocs.io/en/stable/reference/urllib3.response.html .
        i,  i  FLocation)r8   rz   r   s    r   get_redirect_locationz%VCRHTTPResponse.get_redirect_location   s/     t{{)c) *~~j))r   c                 R    | j                   j                         j                         S r   )rB   r~   tobytesr   s    r   datazVCRHTTPResponse.data   s    }}&&(0022r   c                      y r   r   r   s    r   
drain_connzVCRHTTPResponse.drain_conn   r   r   c              #   R   K   	 | j                   j                  |      }| |sy #wr   rP   )r   amtdecode_contentbs       r   streamzVCRHTTPResponse.stream   s-     ""3'AG	 s   %'r   )i   N)r   r   r   r    rK   propertyrN   rQ   rT   rV   rX   r[   r]   r_   rb   rd   rf   r   ri   rl   ro   rs   rz   r|   r   r   r   r   r   r   r   r   r6   r6   :   s    L0  34'778($&3@6( G G	* 3 3r   r6   c                        e Zd ZdZd Zd Zd ZddZd Zd Z	d Z
d	 Zdd
ZddZd Zd Zed        Zej$                  d        Zd Z fdZ fdZ xZS )VCRConnectionNc                 j    | j                   j                  }ddd| j                     }||k7  rd| S dS )zQ
        Returns empty string for the default port and ':port' otherwise
        i  P   )httpshttp: )real_connectionport	_protocol)r   r   default_ports      r   _port_postfixzVCRConnection._port_postfix   s@     ##((!$b1$..A!\14&z9r9r   c                     |r|j                  d      s|S | j                   d| j                  j                   | j	                          | }t
        j                  d|       |S )zReturns request absolute URI/://zAbsolute URI: %s)
startswithr   r   hostr   logdebug)r   urluris      r   _urizVCRConnection._uri   s_    s~~c*J D$8$8$=$=#>t?Q?Q?S>TUXTYZ		$c*
r   c                     | j                    d| j                  j                   | j                          }|j	                  |dd      S )z.Returns request selector url from absolute URIr   r   r	   )r   r   r   r   replace)r   r   prefixs      r   _urlzVCRConnection._url   sE    NN#3t';';'@'@&A$BTBTBVAWX{{62q))r   c                     t        || j                  |      ||xs i       | _        t        j	                  d| j                          t               | _        y)z1Persist the request metadata in self._vcr_requestmethodr   r;   r.   Got N)r   r   _vcr_requestr   r   r   _sock)r   r   r   r;   r.   r   r   s          r   requestzVCRConnection.request   sI    #6tyy~DZaZgegh		D**+,- #_
r   c                     t        || j                  |      di       | _        t        j	                  d| j                          y)z
        httplib gives you more than one way to do it.  This is a way
        to start building up a request.  Usually followed by a bunch
        of putheader() calls.
        r   r   r   N)r   r   r   r   r   )r   r   r   r   r   s        r   
putrequestzVCRConnection.putrequest   s:     $6tyy~BXZ[		D**+,-r   c                 6    || j                   j                  |<   y r   )r   r.   )r   rw   r)   s      r   	putheaderzVCRConnection.putheader   s    ,2!!&)r   c                     | j                   j                  r)| j                   j                  |z   | j                   _        y|| j                   _        y)z
        This method is called after request(), to add additional data to the
        body of the request.  So if that happens, let's just append the data
        onto the most recent request in the cassette.
        Nr   r;   )r   r   s     r   sendzVCRConnection.send   sA     CGBSBSBXBX!2!2!7!7$!>^br   c                 8    | j                   j                          y r   )r   r   r   s    r   r   zVCRConnection.close   s     	""$r   c                 ,    ||| j                   _        yy)z
        Normally, this would actually send the request to the server.
        We are not sending the request until getting the response,
        so bypass this part and just append the message body, if any.
        Nr   )r   message_bodys     r   
endheaderszVCRConnection.endheaders   s     #%1D" $r   c                 d   | j                   j                  | j                        rSt        j	                  d| j                   d       | j                   j                  | j                        }t        |      S | j                   j                  rF| j                   j                  | j                        r!t        | j                   | j                        t        j	                  | j                   d       ddl
m}  |       5  | j                  j                  | j                  j                  | j                  | j                  j                         | j                  j"                  | j                  j$                         ddd       | j                  j'                         }t)        |d	      r|j*                  n|j-                         }|j.                  |j0                  d
t3        |      d|id}| j                   j5                  | j                  |       t        |      S # 1 sw Y   xY w)zRetrieve the responsezPlaying response for z from cassette)cassettefailed_requestz( not in cassette, sending to real serverr   force_reset)r   r   r;   r.   Nr   )r:   r9   r<   )r8   r.   r;   )r   can_play_response_forr   r   ro   play_responser6   write_protectedfilter_requestr   	vcr.patchr   r   r   r   r   r   r;   r.   getresponsehasattrr   rQ   r8   r?   r4   append)r   _r   r2   r   response_datas         r   r   zVCRConnection.getresponse   s    ==..t/@/@AHH,T->->,?~NO}}2243D3DEH"8,,}},,1M1MdN_N_1`>!]]#'#4#4  HH))**RST . $$,,,,33		$"3"3"7"78**// --55	 -  ++779H-4Xv-FHMMHMMOM $,??xO,X6!=1H
 MM  !2!2H=x((' s   A?H&&H/c                 <     | j                   j                  |i | y r   )r   set_debuglevelr   s      r   r   zVCRConnection.set_debuglevel*  s    +++T<V<r   c                 2   t        | d      r&| j                  j                  | j                        ry| j                  j                  ryddlm}  |       5   | j                  j                  |i |cddd       S # 1 sw Y   nxY wt               | _
        y)z
        httplib2 uses this.  Connects to the server I'm assuming.

        Only pass to the baseclass if we don't have a recorded response
        and are not write-protected.
        r   Nr   r   )r   r   r   r   r   r   r   r   connectr   r   r   r   r   r   s       r   r   zVCRConnection.connect-  s     4(T]]-P-PQUQbQb-c ==(()] 	A/4''//@@	A 	A 	A #_
s   A==Bc                 r    | j                   j                  r| j                   j                  S | j                  S r   )r   sockr   r   s    r   r   zVCRConnection.sockE  s-    $$'',,,zzr   c                 T    | j                   j                  r|| j                   _        y y r   )r   r   )r   values     r   r   zVCRConnection.sockK  s$    $$(-D  % %r   c                     |j                  dd        ddlm}  |       5   | j                  |i || _        d d d        d | _        y # 1 sw Y   d | _        y xY w)Nstrictr   r   )popr   r   
_baseclassr   r   r   s       r   rK   zVCRConnection.__init__P  sW    

8T"
 	*] 	D#24??D#CF#CD 	D 
	D 
s   AAc                 t    	 t        | j                  ||       t        |   ||       y# t        $ r Y w xY w)a  
        We need to define this because any attributes that are set on the
        VCRConnection need to be propagated to the real connection.

        For example, urllib3 will set certain attributes on the connection,
        such as 'ssl_version'. These attributes need to get set on the real
        connection to have the correct and expected behavior.

        TODO: Separately setting the attribute on the two instances is not
        ideal. We should switch to a proxying implementation.
        N)setattrr   AttributeErrorsuper__setattr__)r   namer   	__class__s      r   r   zVCRConnection.__setattr__]  sA    	D(($6 	D%(  	 	s   + 	77c                     | j                   j                  d      rt        | j                  |      S t        |   |      S )zw
        Send requests for weird attributes up to the real connection
        (counterpart to __setattr above)
        r   )__dict__getgetattrr   r   __getattr__)r   r   r   s     r   r   zVCRConnection.__getattr__r  s=    
 ==./ 4//66w"4((r   )NNr   )F)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   setterrK   r   r   __classcell__)r   s   @r   r   r      s    H:*

%.3c%
2*)X=%0  
 
[[. .)*
) 
)r   r   c                   H    e Zd ZdZeZdZej                  Zej                  Zy)VCRHTTPConnectionz A Mocked class for HTTP requestsr   N)	r   r   r   r    r   r   r   
debuglevel	_http_vsnr   r   r   r   r     s%    *JI&&J$$Ir   r   c                   L    e Zd ZdZeZdZdZej                  Zej                  Z	y)VCRHTTPSConnectionz!A Mocked class for HTTPS requestsr   TN)
r   r   r   r    r   r   r   is_verifiedr   r   r   r   r   r   r     s*    + JIK&&J$$Ir   r   )r    logginghttp.clientr   r   r   ior   
vcr.errorsr   vcr.requestr   r   r
   	getLoggerr   r   r   r+   r4   r6   r   r   r#   ry   r*   
isinstancestaticmethodr   r   r   r   r   r   <module>r      s    0  E E  ?  g! ,	1yl yxF) F)R ##))+ %DAq!\"q!$%
% %% %r   