o
    衡i0                     @  sj   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 dZ
G dd	 d	ZG d
d dZdS )    )annotations)Callable)Any)command_builder)WebSocketConnectionc                   @  s(   e Zd ZdZddd	ZedddZdS )NetworkEventzRepresents a network event.event_classstrkwargsr   returnNonec                 K  s   || _ || _d S N)r   params)selfr   r
    r   w/var/www/html/asbeauty/laura_geller_scraping/env/lib/python3.10/site-packages/selenium/webdriver/common/bidi/network.py__init__   s   
zNetworkEvent.__init__jsondict[str, Any]c                 C  s   | dd| ddi|S )Nr    r   )get)clsr   r   r   r   	from_json"   s   zNetworkEvent.from_jsonN)r   r	   r
   r   r   r   )r   r   r   r   )__name__
__module____qualname____doc__r   classmethodr   r   r   r   r   r      s
    
r   c                   @  s   e Zd ZddddddddZd	d
ddZd5ddZ			d6d7ddZd8d9ddZd:d%d&Z		d;d<d(d)Z	d=d+d,Z
d>d-d.Zd?d1d2Zd@d3d4ZdS )A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$   connr   r   r   c                 C  s   || _ g | _i | _i | _d S r   )r+   
intercepts	callbackssubscriptions)r   r+   r   r   r   r   8   s   
zNetwork.__init__Nphaseslist[str] | Nonecontextsurl_patternslist[Any] | Noner   c                 C  sz   |du rg }i }|dur||d< |dur||d< t |dkr#||d< ndg|d< td|}| 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
        Nr1   urlPatternsr   r/   r(   znetwork.addIntercept	intercept)lenr   r+   executer,   append)r   r/   r1   r2   r   cmdresultr   r   r   _add_intercept>   s   


zNetwork._add_interceptr5   
str | Nonec              
   C  s   |du r!| j  }|D ]}| jtdd|i | j | qdS z| jtdd|i | j | W dS  tyH } ztd| d}~w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.removeInterceptr5   zException: )r,   copyr+   r7   r   remove	Exception)r   r5   intercepts_to_removeintercept_ider   r   r   _remove_intercept_   s   
zNetwork._remove_intercept
event_namer	   callbackCallable[[Request], Any]intc                   sT   t |}d fdd}j||}|jv r"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
        
event_datar   r   r   c                   s   t | jd dd | jd dd | jd dd | jd dd | jd dd | jd dd | jd dd | jd dd d		} | d S )
NrequestbodySizecookieszgoog:resourceTypeheadersheadersSizetimingsurl)	network
request_id	body_sizerK   resource_typerL   headers_sizerN   rO   )Requestr   r   )rH   rI   rE   r   r   r   	_callback   s   z&Network._on_request.<locals>._callbackN)rH   r   r   r   )r   r+   add_callbackr-   r8   )r   rD   rE   eventrW   callback_idr   rV   r   _on_requestw   s   
zNetwork._on_requestrY   c           
      C  s   z| j | }| j| }W n ty   td| dw | j|g||d}| ||}|| jv r9| j| | ni }	|g|	d< | j	t
d|	 |g| j|< |d | j|< |S )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)r/   r2   r1   eventszsession.subscriber5   )EVENTSPHASESKeyErrorr?   r;   r[   r.   r8   r+   r7   r   r-   )
r   rY   rE   r2   r1   rD   
phase_namer:   rZ   r   r   r   r   add_request_handler   s    


zNetwork.add_request_handlerrZ   c                 C  s   z| j | }W n ty   td| dw t|}| j|| | | j|  | j|= | j| 	| t
| j| dkrUi }|g|d< | jtd| | j|= dS dS )zRemove a request handler from the network.

        Args:
            event: The event to unsubscribe from.
            callback_id: The callback id to remove.
        r\   r]   r   r^   session.unsubscribeN)r_   ra   r?   r   r+   remove_callbackrC   r-   r.   r>   r6   r7   r   )r   rY   rZ   rD   	net_eventr   r   r   r   remove_request_handler   s    
zNetwork.remove_request_handlerc                 C  st   | j D ]1}t|}| j | D ]}| j|| | | j|  | j|= qi }|g|d< | jtd| qi | _ dS )z,Clear all request handlers from the network.r^   rd   N)r.   r   r+   re   rC   r-   r7   r   )r   rD   rf   rZ   r   r   r   r   clear_request_handlers   s   



zNetwork.clear_request_handlersusernamepasswordc                   s    d}d	 fdd}|  ||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$   rI   rU   r   r   c                   s   |    d S r   )_continue_with_auth)rI   rj   ri   r   r   rW      s   z+Network.add_auth_handler.<locals>._callbackN)rI   rU   r   r   )rc   )r   ri   rj   rY   rW   r   rl   r   add_auth_handler   s   
zNetwork.add_auth_handlerc                 C  s   d}|  || dS )z~Remove an authentication handler from the network.

        Args:
            callback_id: The callback id to remove.
        r$   N)rg   )r   rZ   rY   r   r   r   remove_auth_handler   s   zNetwork.remove_auth_handler)r+   r   r   r   )NNN)r/   r0   r1   r0   r2   r3   r   r   r   )r5   r<   r   r   )rD   r	   rE   rF   r   rG   NN)
rY   r	   rE   rF   r2   r3   r1   r0   r   rG   )rY   r	   rZ   rG   r   r   r   r   )ri   r	   rj   r	   r   rG   )rZ   rG   r   r   )r   r   r   r_   r`   r   r;   rC   r[   rc   rg   rh   rm   rn   r   r   r   r   r   '   s6    
!
(
'

r   c                   @  sX   e Zd ZdZ								ddddZd ddZ					d!d"ddZd#d$ddZdS )%rU   z*Represents an intercepted network request.NrP   r   rQ   r   rR   
int | NonerK   rS   r<   rL   rT   methodrN   rO   r   r   c                 C  s@   || _ || _|| _|| _|| _|| _|| _|| _|	| _|
| _	d S r   )
rP   rQ   rR   rK   rS   rL   rT   rr   rN   rO   )r   rP   rQ   rR   rK   rS   rL   rT   rr   rN   rO   r   r   r   r     s   
zRequest.__init__c                 C  s0   | j stdd| j i}| jjtd| dS )zFail this request.Request not found.rI   znetwork.failRequestNrQ   
ValueErrorrP   r+   r7   r   )r   r   r   r   r   fail_request  s   
zRequest.fail_requestbodyc                 C  s   | j stdd| j i}|dur||d< |dur||d< |dur$||d< |dur,||d< |dur4||d< | jjtd	| dS )
z)Continue after intercepting this request.rs   rI   Nrw   rr   rL   rK   rO   r   rt   )r   rw   rr   rL   rK   rO   r   r   r   r   r&   %  s   	
zRequest.continue_requestri   rj   c                 C  sP   i }| j |d< |r|sd|d< nd|d< d||d|d< | jjtd| d	S )
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.
        rI   defaultactionprovideCredentialsrj   )typeri   rj   credentialsr    N)rQ   rP   r+   r7   r   )r   ri   rj   r   r   r   r   rk   ?  s   


zRequest._continue_with_auth)NNNNNNNN)rP   r   rQ   r   rR   rq   rK   r   rS   r<   rL   r   rT   rq   rr   r<   rN   r   rO   r<   r   r   rp   )NNNNN)rw   r   rr   r<   rL   r   rK   r   rO   r<   r   r   ro   )ri   r<   rj   r<   r   r   )r   r   r   r   r   rv   r&   rk   r   r   r   r   rU     s&    

rU   N)
__future__r   collections.abcr   typingr   %selenium.webdriver.common.bidi.commonr   .selenium.webdriver.remote.websocket_connectionr   r   r   rU   r   r   r   r   <module>   s    \