
    hg*                     l    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dS )    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d'S ))InspectRequestsMixinzKMixin class that provides functions to inspect and modify browser requests.returnc                 >    | 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    M/var/www/fb-scrape/myenv/lib/python3.11/site-packages/seleniumwire/inspect.pyrequestszInspectRequestsMixin.requests   s     |#11333    c                 B    | j         j                                         d S N)r   r   clear_requestsr   s    r   r   zInspectRequestsMixin.requests   s    ++-----r   c              #   R   K   | j         j                                        E d{V  dS )zGReturn an iterator of requests.

        Returns: An iterator.
        N)r   r   iter_requestsr   s    r   r   z"InspectRequestsMixin.iter_requests    s7      
 <'5577777777777r   c                 >    | 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77r   
   pattimeoutc                 F   t          j                     }t          j                     |z
  |k     rR| j        j                            |          }|t          j        d           n|S t          j                     |z
  |k     Rt          d                    ||                    )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.
        Ng?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    , 	ikkE!G++l*//44G
5!!!! ikkE!G++ T[[\cehiijjjr   c                 b    t          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 E E G GHHHr   c                 $    | 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 ]\  }}|                     |           n|                     |           || j        j        _        d S r   )
isinstancelist_validate_headersr   r.   r/   )r   r/   _hs       r   r0   z%InspectRequestsMixin.header_overrides|   sl    gt$$ 	, * *1&&q))))* ""7+++(/%%%r   c                 v    |                                 D ]#}|t          |t                    s
J d            $d S )NzHeader values must be strings)valuesr2   str)r   r/   vs      r   r4   z&InspectRequestsMixin._validate_headers   sN    !! 	K 	KA}!!S))JJ+JJJJ	K 	Kr   c                     | j         j        `d S r   r-   r   s    r   r0   z%InspectRequestsMixin.header_overrides   s    L!)))r   c                 $    | 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        _        d S r   r=   )r   r>   s     r   r?   z$InspectRequestsMixin.param_overrides       '-$$$r   c                     | j         j        `d S r   r=   r   s    r   r?   z$InspectRequestsMixin.param_overrides       L!(((r   c                 $    | 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        _        d S r   rE   )r   rF   s     r   rG   z#InspectRequestsMixin.body_overrides   rA   r   c                     | j         j        `d S r   rE   r   s    r   rG   z#InspectRequestsMixin.body_overrides   rC   r   c                 $    | 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0r   c                 (    || j         j        _        d S r   rK   )r   querystringss     r   rM   z*InspectRequestsMixin.querystring_overrides   s    ,8)))r   c                     | j         j        `d S r   rK   r   s    r   rM   z*InspectRequestsMixin.querystring_overrides   s    L!---r   c                 $    | 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   rS   z"InspectRequestsMixin.rewrite_rules   s     |$22r   c                 (    || j         j        _        d S r   rR   )r   rS   s     r   rS   z"InspectRequestsMixin.rewrite_rules   s    .;+++r   c                     | j         j        `d S r   rR   r   s    r   rS   z"InspectRequestsMixin.rewrite_rules  s    L!///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   rX   zInspectRequestsMixin.scopes  s     |""r   rX   c                     || j         _        d S r   rW   )r   rX   s     r   rX   zInspectRequestsMixin.scopes  s    $r   c                     g | j         _        d S r   rW   r   s    r   rX   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         _        d S r   r\   r   r^   s     r   r]   z(InspectRequestsMixin.request_interceptor%  s    +6(((r   c                     d | j         _        d S 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   rd   z)InspectRequestsMixin.response_interceptor-  s     |00r   c                     t          t          j        |          j                  dk    rt	          d          || j        _        d S )N   zEA response interceptor takes two parameters: the request and response)leninspect	signature
parametersRuntimeErrorr   rd   r`   s     r   rd   z)InspectRequestsMixin.response_interceptor6  sB    w --899Q>>fggg,7)))r   c                     d | j         _        d S r   rc   r   s    r   rd   z)InspectRequestsMixin.response_interceptor<  s    ,0)))r   N)r   )__name__
__module____qualname____doc__propertyr   r	   r   deleterr   r   r   r   r9   r   intfloatr(   r   r0   setterr4   r?   rG   rM   rS   rX   callabler]   rd    r   r   r   r      s@       UU4$w- 4 4 4 X4 . . .8x0 8 8 8 8 	8hw/ 	8 	8 	8 X	8 k  kC  k%U
2C  kW  k  k  k  kD IS I I I XI - - X-6 0 0 0K K K
 * * * , , X,2 . . . ) ) ) , , X,* . . . ) ) ) 1 1 X1( !9 9 "!9 ". . #". 3 3 X3  < < < 0 0 0 #S	 # # # X# ]%T#Y % % % ]% ^! ! ^! 0X 0 0 0 X0 7x 7 7 7  7  0 0 ! 0 1h 1 1 1 X1  8 8 8 8 ! 8
 !1 1 "!1 1 1r   r   )rh   r"   typingr   r   r   r   selenium.common.exceptionsr   seleniumwirer   seleniumwire.requestr	   r   rw   r   r   <module>r|      s      2 2 2 2 2 2 2 2 2 2 2 2 7 7 7 7 7 7       ( ( ( ( ( (s1 s1 s1 s1 s1 s1 s1 s1 s1 s1r   