
    i0                    |    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	  G d d      Z
 G d d	      Z G d
 d      Zy)    )annotations)Callable)Any)command_builder)WebSocketConnectionc                  *    e Zd ZdZddZedd       Zy)NetworkEventzRepresents a network event.c                     || _         || _        y N)event_classparams)selfr   kwargss      b/var/www/html/BatchJob/venv/lib/python3.12/site-packages/selenium/webdriver/common/bidi/network.py__init__zNetworkEvent.__init__   s    &    c                6     | dd|j                  dd      i|S )Nr     )get)clsjsons     r   	from_jsonzNetworkEvent.from_json"   s     Ctxxr:CdCCr   N)r   strr   r   returnNone)r   dict[str, Any]r   r	   )__name__
__module____qualname____doc__r   classmethodr   r   r   r   r	   r	      s     % D Dr   r	   c                      e Zd ZddddddddZd	d
ddZddZ	 	 	 d	 	 	 	 	 	 	 ddZdddZddZ	 	 d	 	 	 	 	 	 	 	 	 ddZ	ddZ
d dZd!dZd"dZy)#Networkznetwork.beforeRequestSentznetwork.responseStartedznetwork.responseCompletedznetwork.authRequiredznetwork.fetchErrornetwork.continueRequestnetwork.continueWithAuth)before_requestresponse_startedresponse_completedauth_requiredfetch_errorcontinue_requestcontinue_authbeforeRequestSentresponseStartedauthRequired)r'   r(   r*   c                <    || _         g | _        i | _        i | _        y r   )conn
intercepts	callbackssubscriptions)r   r2   s     r   r   zNetwork.__init__8   s    	%'/135r   Nc                    |g }i }|||d<   |||d<   t        |      dkD  r||d<   ndg|d<   t        d|      }| j                  j                  |      }| j                  j                  |d          |S )aQ  Add an intercept to the network.

        Args:
            phases: A list of phases to intercept. Default is None (empty list).
            contexts: A list of contexts to intercept. Default is None.
            url_patterns: A list of URL patterns to intercept. Default is None.

        Returns:
            str: intercept id
        contextsurlPatternsr   phasesr.   znetwork.addIntercept	intercept)lenr   r2   executer3   append)r   r9   r7   url_patternsr   cmdresults          r   _add_interceptzNetwork._add_intercept>   s      >F!)F:#$0F=!v;?%F8 34F84f=!%!2!23!7vk23r   c                   |d| j                   j                         }|D ]D  }| j                  j                  t	        dd|i             | j                   j                  |       F y	 | j                  j                  t	        dd|i             | j                   j                  |       y# t        $ r}t        d|       d}~ww xY w)a  Remove a specific intercept, or all intercepts.

        Args:
            intercept: The intercept to remove. Default is None.

        Raises:
            ValueError: If intercept is not found.

        Note:
            If intercept is None, all intercepts will be removed.
        Nznetwork.removeInterceptr:   zException: )r3   copyr2   r<   r   remove	Exception)r   r:   intercepts_to_removeintercept_ides        r   _remove_interceptzNetwork._remove_intercept_   s     #'??#7#7#9  4 5		!!/2Kk[gMh"ij&&|453		!!/2Kk[dMe"fg&&y1 3+aS 1223s   (AB+ +	C4CCc                     t        |      }d fd} j                  j                  ||      }| j                  v r  j                  |   j	                  |       |S |g j                  |<   |S )a%  Set a callback function to subscribe to a network event.

        Args:
            event_name: The event to subscribe to.
            callback: The callback function to execute on event.
                Takes Request object as argument.

        Returns:
            int: callback id
        c                   t        | j                  d   j                  dd       | j                  d   j                  dd       | j                  d   j                  dd       | j                  d   j                  dd       | j                  d   j                  dd       | j                  d   j                  dd       | j                  d   j                  dd       | j                  d   j                  dd       		      } |       y )
NrequestbodySizecookieszgoog:resourceTypeheadersheadersSizetimingsurl)	network
request_id	body_sizerN   resource_typerO   headers_sizerQ   rR   )Requestr   r   )
event_datarL   callbackr   s     r   	_callbackz&Network._on_request.<locals>._callback   s    %,,Y7;;ItL$++I6:::tL")))488DI(//	:>>?RTXY")))488DI'..y9==mTR")))488DI%%i044UDA
G Wr   )rY   r	   r   r   )r	   r2   add_callbackr4   r=   )r   
event_namerZ   eventr[   callback_ids   ` `   r   _on_requestzNetwork._on_requestw   so     Z(	  9911%C'NN:&--k:  +6DNN:&r   c                   	 | j                   |   }| j                  |   }| j	                  |g||      }| j                  ||      }|| j                  v r| j                  |   j                  |       n=i }	|g|	d<   | j                  j                  t        d|	             |g| j                  |<   |d   | j                  |<   |S # t        $ r t        d| d      w xY w)a  Add a request handler to the network.

        Args:
            event: The event to subscribe to.
            callback: The callback function to execute on request interception.
                Takes Request object as argument.
            url_patterns: A list of URL patterns to intercept. Default is None.
            contexts: A list of contexts to intercept. Default is None.

        Returns:
            int: callback id
        Event 
 not found)r9   r>   r7   eventszsession.subscriber:   )EVENTSPHASESKeyErrorrE   rA   r`   r5   r=   r2   r<   r   r4   )
r   r^   rZ   r>   r7   r]   
phase_namer@   r_   r   s
             r   add_request_handlerzNetwork.add_request_handler   s    &	8U+JU+J $$ZL|^f$g&&z8<+++z*11+>%'F *|F8IIo.A6JK.9]Dz*&,[&9{#  	8fUG:677	8s   C Cc                   	 | j                   |   }t        |      }| j                  j                  ||       | j                  | j                  |          | j                  |= | j                  |   j                  |       t        | j                  |         dk(  r;i }|g|d<   | j                  j                  t        d|             | j                  |= yy# t        $ r t        d| d      w xY w)zRemove a request handler from the network.

        Args:
            event: The event to unsubscribe from.
            callback_id: The callback id to remove.
        rb   rc   r   rd   session.unsubscribeN)re   rg   rE   r	   r2   remove_callbackrI   r4   r5   rD   r;   r<   r   )r   r^   r_   r]   	net_eventr   s         r   remove_request_handlerzNetwork.remove_request_handler   s    	8U+J !,			!!)[9t~~k:;NN;':&--k:t!!*-.!3%'F *|F8IIo.CVLM"":.	 4  	8fUG:677	8s   C C1c                Z   | j                   D ]  }t        |      }| j                   |   D ]I  }| j                  j                  ||       | j	                  | j
                  |          | j
                  |= K i }|g|d<   | j                  j                  t        d|              i | _         y)z,Clear all request handlers from the network.rd   rk   N)r5   r	   r2   rl   rI   r4   r<   r   )r   r]   rm   r_   r   s        r   clear_request_handlerszNetwork.clear_request_handlers   s    ,, 	NJ$Z0I#11*= 0		)))[A&&t~~k'BCNN;/0 &(F *|F8IIo.CVLM	N  r   c                <    d}dfd}| j                  ||      S )zAdd an authentication handler to the network.

        Args:
            username: The username to authenticate with.
            password: The password to authenticate with.

        Returns:
            int: callback id
        r*   c                *    | j                         y r   )_continue_with_auth)rL   passwordusernames    r   r[   z+Network.add_auth_handler.<locals>._callback   s    ''(;r   )rL   rX   r   r   )ri   )r   ru   rt   r^   r[   s    ``  r   add_auth_handlerzNetwork.add_auth_handler   s$      	< ''y99r   c                ,    d}| j                  ||       y)z~Remove an authentication handler from the network.

        Args:
            callback_id: The callback id to remove.
        r*   N)rn   )r   r_   r^   s      r   remove_auth_handlerzNetwork.remove_auth_handler   s      ##E;7r   )r2   r   r   r   )NNN)r9   list[str] | Noner7   ry   r>   list[Any] | Noner   r   r   )r:   
str | Noner   r   )r]   r   rZ   Callable[[Request], Any]r   intNN)
r^   r   rZ   r|   r>   rz   r7   ry   r   r}   )r^   r   r_   r}   r   r   r   r   )ru   r   rt   r   r   r}   )r_   r}   r   r   )r   r   r    re   rf   r   rA   rI   r`   ri   rn   rp   rv   rx   r   r   r   r$   r$   '   s    559/+53F .-'F6 $(%))-	  # '	
 
B30"P *.%)%% +% '	%
 #% 
%N/0 :"8r   r$   c                      e Zd ZdZ	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZd	dZ	 	 	 	 	 d
	 	 	 	 	 	 	 	 	 	 	 ddZdddZy)rX   z*Represents an intercepted network request.Nc                    || _         || _        || _        || _        || _        || _        || _        || _        |	| _        |
| _	        y r   )
rS   rT   rU   rN   rV   rO   rW   methodrQ   rR   )r   rS   rT   rU   rN   rV   rO   rW   r   rQ   rR   s              r   r   zRequest.__init__  sL     $"*(r   c                    | j                   st        d      d| j                   i}| j                  j                  j	                  t        d|             y)zFail this request.Request not found.rL   znetwork.failRequestNrT   
ValueErrorrS   r2   r<   r   )r   r   s     r   fail_requestzRequest.fail_request  sC    122"+T__!=!!/2G"PQr   c                    | j                   st        d      d| j                   i}|||d<   |||d<   |||d<   |||d<   |||d<   | j                  j                  j	                  t        d	|             y)
z)Continue after intercepting this request.r   rL   Nbodyr   rO   rN   rR   r%   r   )r   r   r   rO   rN   rR   r   s          r   r,   zRequest.continue_request%  s     122"+T__!=!F6N%F8 'F9 'F9?F5M!!/2KV"TUr   c                    i }| j                   |d<   |r|sd|d<   nd|d<   d||d|d<   | j                  j                  j                  t	        d|             y	)
a	  Continue with authentication.

        Args:
            username: The username to authenticate with.
            password: The password to authenticate with.

        Note:
            If username or password is None, it attempts auth with no credentials.
        rL   defaultactionprovideCredentialsrt   )typeru   rt   credentialsr&   N)rT   rS   r2   r<   r   )r   ru   rt   r   s       r   rs   zRequest._continue_with_auth?  sb     "$ OOyx(F83F8-7X[c$dF=!!!/2Lf"UVr   )NNNNNNNN)rS   r$   rT   r   rU   
int | NonerN   r   rV   r{   rO   r   rW   r   r   r{   rQ   r   rR   r{   r   r   r   )NNNNN)r   r   r   r{   rO   r   rN   r   rR   r{   r   r   r~   )ru   r{   rt   r{   r   r   )r   r   r    r!   r   r   r,   rs   r   r   r   rX   rX     s    4 !%$(#'!  	
  "  !    
0R !VV V 	V
 V V 
V4Wr   rX   N)
__future__r   collections.abcr   typingr   %selenium.webdriver.common.bidi.commonr   .selenium.webdriver.remote.websocket_connectionr   r	   r$   rX   r   r   r   <module>r      s=   $ # $  A N	D 	DX8 X8vPW PWr   