
    Dg*                     d    d dl Z d dlZd dlmZmZmZmZ d dlmZ d dl	m
Z
 d dlmZ  G d d      Zy)    N)IteratorListOptionalUnion)TimeoutException)har)Requestc                      e Zd ZdZedee   fd       Zej                  d        Zde	e   fdZ
edee   fd       Zd'dedeeef   defd	Zedefd
       Zed        Zej(                  d        Zd Zej                  d        Zed        Zej(                  d        Zej                  d        Zed        Zej(                  d        Zej                  d        Zed        Zej(                  d        Zej                  d        Zed        Zej(                  d        Zej                  d        Zedee   fd       Zej(                  dee   fd       Zej                  d        Zedefd       Zej(                  d efd!       Zej                  d"        Zedefd#       Zej(                  d efd$       Zej                  d%        Zy&)(InspectRequestsMixinzKMixin class that provides functions to inspect and modify browser requests.returnc                 J    | j                   j                  j                         S )a&  Retrieves the requests made between the browser and server.

        Captured requests can be cleared with 'del', e.g:

            del firefox.requests

        Returns:
            A list of Request instances representing the requests made
            between the browser and server.
        )backendstorageload_requestsselfs    T/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/seleniumwire/inspect.pyrequestszInspectRequestsMixin.requests   s     ||##1133    c                 L    | j                   j                  j                          y N)r   r   clear_requestsr   s    r   r   zInspectRequestsMixin.requests   s    ++-r   c              #   h   K   | j                   j                  j                         E d{    y7 w)zGReturn an iterator of requests.

        Returns: An iterator.
        N)r   r   iter_requestsr   s    r   r   z"InspectRequestsMixin.iter_requests    s$     
 <<''55777s   (202c                 J    | j                   j                  j                         S )a  Retrieve the last request made between the browser and server.

        Note that this is more efficient than running requests[-1]

        Returns:
            A Request instance representing the last request made, or
            None if no requests have been made.
        )r   r   load_last_requestr   s    r   last_requestz!InspectRequestsMixin.last_request'   s     ||##5577r   pattimeoutc                 H   t        j                          }t        j                          |z
  |k  rZ| j                  j                  j                  |      }|t        j                  d       n|S t        j                          |z
  |k  rZt        dj                  ||            )a5  Wait up to the timeout period for a request matching the specified
        pattern to be seen.

        The pat attribute can be can be a simple substring or a regex that will
        be searched in the full request URL. If a request is not seen before the
        timeout then a TimeoutException is raised. Only requests with corresponding
        responses are considered.

        Given that pat can be a regex, ensure that any special characters
        (e.g. question marks) are escaped.

        Args:
            pat: The pat of the request to look for. A regex can be supplied.
            timeout: The maximum time to wait in seconds. Default 10s.

        Returns:
            The request.
        Raises:
            TimeoutException if a request is not seen within the timeout
                period.
        g?z3Timed out after {}s waiting for request matching {})timer   r   findsleepr   format)r   r   r   startrequests        r   wait_for_requestz%InspectRequestsMixin.wait_for_request3   s    , 		iikE!G+ll**//4G

5! iikE!G+ T[[\cehijjr   c                 p    t        j                  | j                  j                  j	                               S )zGet a HAR archive of HTTP transactions that have taken place.

        Note that the enable_har option needs to be set before HAR
        data will be captured.

        Returns: A JSON string of HAR data.
        )r   generate_harr   r   load_har_entriesr   s    r   r   zInspectRequestsMixin.harU   s(      4 4 E E GHHr   c                 B    | j                   j                  j                  S )a\  The header overrides for outgoing browser requests.

        DEPRECATED. Use request_interceptor and response_interceptor.

        The value of the headers can be a dictionary or list of sublists,
        with each sublist having two elements - a URL pattern and headers.
        Where a header in the dictionary exists in the request, the dictionary
        value will overwrite the one in the request. Where a header in the dictionary
        does not exist in the request, it will be added to the request as a
        new header. To filter out a header from the request, set that header
        in the dictionary to None. Header names are case insensitive.
        For response headers, prefix the header name with 'response:'.

        For example:

            header_overrides = {
                'User-Agent': 'Firefox',
                'response:Cache-Control': 'none'
            }
            header_overrides = [
                ('.*somewhere.com.*', {'User-Agent': 'Firefox', 'response:Cache-Control': 'none'}),
                ('*.somewhere-else.com.*', {'User-Agent': 'Chrome'})
            ]
        r   modifierheadersr   s    r   header_overridesz%InspectRequestsMixin.header_overrides`   s    4 ||$$,,,r   c                     t        |t              r|D ]  \  }}| j                  |        n| j                  |       || j                  j                  _        y r   )
isinstancelist_validate_headersr   r-   r.   )r   r.   _hs       r   r/   z%InspectRequestsMixin.header_overrides|   sO    gt$ *1&&q)* ""7+(/%r   c                 b    |j                         D ]  }|t        |t              rJ d        y )NzHeader values must be strings)valuesr1   str)r   r.   vs      r   r3   z&InspectRequestsMixin._validate_headers   s5    ! 	KA}!!S)J+JJ)	Kr   c                 0    | j                   j                  `y r   r,   r   s    r   r/   z%InspectRequestsMixin.header_overrides   s    LL!!)r   c                 B    | j                   j                  j                  S )a  The parameter overrides for outgoing browser requests.

        DEPRECATED. Use request_interceptor.

        For POST requests, the parameters are assumed to be encoded in the
        request body.

        The value of the params can be a dictionary or list of sublists,
        with each sublist having two elements - a URL pattern and params.
        Where a param in the dictionary exists in the request, the dictionary
        value will overwrite the one in the request. Where a param in the dictionary
        does not exist in the request, it will be added to the request as a
        new param. To filter out a param from the request, set that param
        in the dictionary to None.

        For example:
            param_overrides = {'foo': 'bar'}
            param_overrides = [
                ('.*somewhere.com.*', {'foo': 'bar'}),
                ('*.somewhere-else.com.*', {'x': 'y'}),
            ]
        r   r-   paramsr   s    r   param_overridesz$InspectRequestsMixin.param_overrides   s    0 ||$$+++r   c                 :    || j                   j                  _        y r   r<   )r   r=   s     r   r>   z$InspectRequestsMixin.param_overrides       '-$r   c                 0    | j                   j                  `y r   r<   r   s    r   r>   z$InspectRequestsMixin.param_overrides       LL!!(r   c                 B    | j                   j                  j                  S )a  The body overrides for outgoing browser requests.

        DEPRECATED. Use request_interceptor and response_interceptor.

        For 'not GET' requests, the parameters are assumed to be encoded in the
        request body.

        The value of the body can be a string value or list of sublists,
        with each sublist having two elements - a URL pattern and string value.
        The string value will be encoded, then replace whole http body.
        And body_overrides has higher priority than param_overrides When they conflict.
        For example:
            body_overrides = '{"foo":"bar"}'
            body_overrides = [
                ('.*somewhere.com.*', '{"foo":"bar"}'),
                ('*.somewhere-else.com.*', '{"x":"y"}'),
            ]
        r   r-   bodiesr   s    r   body_overridesz#InspectRequestsMixin.body_overrides   s    ( ||$$+++r   c                 :    || j                   j                  _        y r   rD   )r   rE   s     r   rF   z#InspectRequestsMixin.body_overrides   r@   r   c                 0    | j                   j                  `y r   rD   r   s    r   rF   z#InspectRequestsMixin.body_overrides   rB   r   c                 B    | j                   j                  j                  S )a  The querystring overrides for outgoing browser requests.

        DEPRECATED. Use request_interceptor.

        The value of the querystring override can be a string or a list of sublists,
        with each sublist having two elements, a URL pattern and the querystring.
        The querystring override will overwrite the querystring in the request
        or will be added to the request if the request has no querystring. To
        remove a querystring from the request, set the value to empty string.

        For example:
            querystring_overrides = 'foo=bar&x=y'
            querystring_overrides = [
                ('.*somewhere.com.*', 'foo=bar&x=y'),
                ('*.somewhere-else.com.*', 'a=b&c=d'),
            ]
        r   r-   querystringr   s    r   querystring_overridesz*InspectRequestsMixin.querystring_overrides   s    & ||$$000r   c                 :    || j                   j                  _        y r   rJ   )r   querystringss     r   rL   z*InspectRequestsMixin.querystring_overrides   s    ,8)r   c                 0    | j                   j                  `y r   rJ   r   s    r   rL   z*InspectRequestsMixin.querystring_overrides   s    LL!!-r   c                 B    | j                   j                  j                  S )a  The rules used to rewrite request URLs.

        DEPRECATED. Use request_interceptor.

        The value of the rewrite rules should be a list of sublists (or tuples)
        with each sublist containing the pattern and replacement.

        For example:
            rewrite_rules = [
                (r'(https?://)www.google.com/', r'www.bing.com/'),
                (r'https://docs.python.org/2/', r'https://docs.python.org/3/'),
            ]
        r   r-   rewrite_rulesr   s    r   rR   z"InspectRequestsMixin.rewrite_rules   s     ||$$222r   c                 :    || j                   j                  _        y r   rQ   )r   rR   s     r   rR   z"InspectRequestsMixin.rewrite_rules   s    .;+r   c                 0    | j                   j                  `y r   rQ   r   s    r   rR   z"InspectRequestsMixin.rewrite_rules  s    LL!!/r   c                 .    | j                   j                  S )a  The URL patterns used to scope request capture.

        The value of the scopes should be a list (or tuple) of
        regular expressions.

        For example:
            scopes = [
                '.*stackoverflow.*',
                '.*github.*'
            ]
        r   scopesr   s    r   rW   zInspectRequestsMixin.scopes  s     ||"""r   rW   c                 &    || j                   _        y r   rV   )r   rW   s     r   rW   zInspectRequestsMixin.scopes  s    $r   c                 &    g | j                   _        y r   rV   r   s    r   rW   zInspectRequestsMixin.scopes  s     r   c                 .    | j                   j                  S )zA callable that will be used to intercept/modify requests.

        The callable must accept a single argument for the request
        being intercepted.
        r   request_interceptorr   s    r   r\   z(InspectRequestsMixin.request_interceptor  s     ||///r   interceptorc                 &    || j                   _        y r   r[   r   r]   s     r   r\   z(InspectRequestsMixin.request_interceptor%  s    +6(r   c                 &    d | j                   _        y r   r[   r   s    r   r\   z(InspectRequestsMixin.request_interceptor)  s    +/(r   c                 .    | j                   j                  S )zA callable that will be used to intercept/modify responses.

        The callable must accept two arguments: the response being
        intercepted and the originating request.
        r   response_interceptorr   s    r   rc   z)InspectRequestsMixin.response_interceptor-  s     ||000r   c                     t        t        j                  |      j                        dk7  rt	        d      || j
                  _        y )N   zEA response interceptor takes two parameters: the request and response)leninspect	signature
parametersRuntimeErrorr   rc   r_   s     r   rc   z)InspectRequestsMixin.response_interceptor6  s8    w  -889Q>fgg,7)r   c                 &    d | j                   _        y r   rb   r   s    r   rc   z)InspectRequestsMixin.response_interceptor<  s    ,0)r   N)
   )__name__
__module____qualname____doc__propertyr   r	   r   deleterr   r   r   r   r8   r   intfloatr'   r   r/   setterr3   r>   rF   rL   rR   rW   callabler\   rc    r   r   r   r      s'   U4$w- 4 4 . .8x0 8 	8hw/ 	8 	8 kC  k%U
2C  kW  kD IS I I - -6 0 0K
 * * , ,2 . . ) ) , ,* . . ) ) 1 1( !!9 "9 "". #. 3 3  < < 0 0 #S	 # # ]]%T#Y % % ^^! ! 0X 0 0 7x 7  7   0 !0 1h 1 1   8 8 !8
 !!1 "1r   r   )rg   r!   typingr   r   r   r   selenium.common.exceptionsr   seleniumwirer   seleniumwire.requestr	   r   rw   r   r   <module>r|      s&      2 2 7  (s1 s1r   