
    i)                         d dl Z ddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZmZ ddlmZ dd	lmZ  G d
 de      Zy)    N   )BaseHubConnection)AuthHubConnection)IntervalReconnectionHandlerRawReconnectionHandlerReconnectionType)Helpers)HttpTransportTypeHubProtocolEncoding)BaseHubProtocol)create_ssl_contextc                   X    e Zd ZdZd Z	 ddedefdZ	 ddZdefd	Z	d
 Z
d ZdefdZy)HubConnectionBuilderzBuilder for SignalR hub connections using a fluent interface.
    Use :meth:`with_url` as the first call to configure url
        and connection options.
    Use :meth:`build` as the final call to obtain a ready-to-use connection.
    c                 4   d | _         dd i| _        t               | _        d | _        d | _        d | _        d | _        d | _        d| _	        t        j                         | _        d| _        d| _        t               | _        t!        j"                         | _        y )Naccess_token_factory   F)hub_urloptionsdictheadershas_auth_configuredprotocolpreferred_protocolpreferred_transportreconnection_handlerkeep_alive_intervalsslcreate_default_contextssl_contextenable_traceskip_negotiationproxiesr	   
get_loggerloggerselfs    [/home/work/apex_v16/venv/lib/python3.12/site-packages/signalrcore/hub_connection_builder.py__init__zHubConnectionBuilder.__init__   s     #D
 v#' )-"&#' $(!#% 557! %v((*    Nr   r   c                     ||j                         dk(  rt        d      |5t        |      t        ur$t	        dj                  | j                              | d|j                         v xr t        |d         | _	        d|j                         v xr |d   | _
        d|j                         v r;|j                  d      }t        |      t        urt	        dt               || _        d	|j                         v rd
|j                         v rt        d      d	|j                         v r>|j                  d	d      }t        |      t        urt	        d      t        |      | _        d
|j                         v r?|j                  d
d      }t        |      t"        j$                  urt	        d      || _        d|j                         v rI|j                  dd      }t        |      t        urt	        d      | j&                  j)                  |       d|j                         v r1|j                  dd      }|t        |      st	        d      || _        || _        || j                  | _        | S || _        | S )a  Configure the hub url and options like negotiation and auth function

        def login(self):
            response = requests.post(
                self.login_url,
                json={
                    "username": self.email,
                    "password": self.password
                    },verify=False)
            return response.json()["token"]

        self.connection = HubConnectionBuilder()            .with_url(self.server_url,
            options={
                "verify_ssl": False,
                "access_token_factory": self.login,
                "headers": {
                    "mycustomheader": "mycustomheadervalue"
                }
            })            .configure_logging(logging.ERROR)            .with_automatic_reconnect({
                "type": "raw",
                "keep_alive_interval": 10,
                "reconnect_interval": 5,
                "max_attempts": 5
            }).build()

        Args:
            hub_url (string): Hub URL
            options ([dict], optional): [description]. Defaults to None.

        Raises:
            ValueError: If url is invalid
            TypeError: If options are not a dict or auth function
                is not callable

        Returns:
            [HubConnectionBuilder]: configured connection
        N zhub_url must be a valid url.zoptions must be a dict {0}.r   r!   	transportztransport types:  
verify_sslr   zYYou must specify one of these two options, verify_ssl:bool or ssl_context: ssl.SSLContextzVerify ssl must be a boolz$ssl_context must be a ssl.SSLContextr   z headers must be a Dict[str, str]z$access_token_factory is not function)strip
ValueErrortyper   	TypeErrorformatr   keyscallabler   r!   getr
   r   boolr   r   r   
SSLContextr   updateauth_functionr   )r&   r   r   r,   valuer9   s         r'   with_urlzHubConnectionBuilder.with_url+   sk   X ?gmmo3;<<4=#<-44T\\BD D &',,.8 >W%;<= $ %7',,.$H %0./ ! glln,#KK4		?*;;#,->,?@B B+4(w||~-%7 EF F w||~-L$7;d*#$?@@#5e#< .M48;cnn4#$JKK#( GLLN*It4;d*#$FGG##E*%7 ',BD I ('6#>@ @%2"'.t|| =Dr)   c                 @    t        j                  ||       || _        | S )a  Configures signalr logging

        Args:
            logging_level ([type]): logging.INFO | logging.DEBUG ...
                from python logging class
            socket_trace (bool, optional): Enables socket package trace.
                Defaults to False.
            handler ([type], optional):  Custom logging handler.
                Defaults to None.

        Returns:
            [HubConnectionBuilder]: Instance hub with logging configured
        )r	   configure_loggerr    )r&   logging_levelsocket_tracehandlers       r'   configure_loggingz&HubConnectionBuilder.configure_logging   s!     	  8(r)   r"   c                 r    d|j                         vsd|j                         vrt        d      || _        | S )a4  configures proxies

        Args:
            proxies (dict): {
              "http"  : "http://host:port",
              "https" : "https://host:port",
              "ftp"   : "ftp://port:port"
            }

        Returns:
            [HubConnectionBuilder]: Instance hub with proxies configured
        httphttpsz$Only http and https keys are allowed)r3   r/   r"   )r&   r"   s     r'   configure_proxiesz&HubConnectionBuilder.configure_proxies   s7      '7',,.+HCDDr)   c                     t        t        |      t              r	|| _        | S t        |      t        u r	|| _        | S t        dt        |             )an  Changes transport protocol
            from signalrcore.types                import HubProtocolEncoding

            HubConnectionBuilder()            .with_url(self.server_url, options={"verify_ssl":False})                ...
            .with_hub_protocol(HubProtocolEncoding.binary)                ...
            .build()
        Args:
            protocol(
                    MessagePackHubProtocol|
                    JsonHubProtocol|
                    HubProtocolEncoding):
                protocol instance or HubProtocolEncoding

        Returns:
            HubConnectionBuilder: instance configured
        zWrong protocol type )
issubclassr0   r   r   r   r   r1   )r&   r   s     r'   with_hub_protocolz&HubConnectionBuilder.with_hub_protocol   sP    * d8no6$DMK>00&.D#K.tH~.>?@@r)   c                 @   | j                   rt        | j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                        S t        | j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                        S )zrCreates the connection hub

        Returns:
            [BaseHubConnection]: [connection SignalR object]
        )r   r9   urlr   r   r   r   r   r"   r!   r    r   )rJ   r   r   r   r   r   r   r"   r!   r    r   )r   r   r   r9   r   r   r   r   r   r   r"   r!   r    r   r   r%   s    r'   buildzHubConnectionBuilder.build   s    & '' !"00LL#'#:#:$($<$<%)%>%> ,,!%!6!6!..$($<$<> 	> LL#'#:#:$($<$<%)%>%> ,,!%!6!6!..$($<$<>	>r)   datac                 h   |j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      }|j                  d	g       }|| _        t        |   }|t        j                  k(  rt	        ||      | _        |t        j                  k(  rt        |      | _        | S )
a  Configures automatic reconnection
            https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-core-3-0-preview-4/

            hub = HubConnectionBuilder()            .with_url(self.server_url, options={"verify_ssl":False})            .configure_logging(logging.ERROR)            .with_automatic_reconnect({
                "type": "raw",
                "keep_alive_interval": 10,
                "reconnect_interval": 5,
                "max_attempts": 5
            })            .build()

        Args:
            data (dict): [dict with automatic reconnection parameters]

        Returns:
            [HubConnectionBuilder]: [self object for fluent interface purposes]
        r0   rawmax_attemptsNreconnect_interval   r   r   	intervals)r5   r   r   rN   r   r   intervalr   )r&   rL   reconnect_typerO   rP   r   rR   reconnection_types           r'   with_automatic_reconnectz-HubConnectionBuilder.with_automatic_reconnect   s    * &%0 xx5 "XX&:A>"hh'<bAHH["-	#6 ,^< 0 4 44(>")D%  0 9 99(C)D% r)   )N)FN)__name__
__module____qualname____doc__r(   strr   r;   rA   rE   rH   rK   rV    r)   r'   r   r      s\    +6 !ee eP >B&,A>>B.T .r)   r   )r   hub.base_hub_connectionr   hub.auth_hub_connectionr   transport.reconnectionr   r   r   helpersr	   typesr
   r   protocol.protocol_factoryr   transport.sockets.utilsr   objectr   r\   r)   r'   <module>re      s4    
 6 6J J  9 6 7]6 ]r)   