
    j
                         d dl Z d dlmZmZ d dlZd dlmZmZ  e j                  e	      Z
 G d de      Z G d d      Zy)    N)
NamedTupleOptional)httpx_clienturl_helpersc                   (    e Zd ZU eed<   ee   ed<   y)ProbeResult
is_healthyerror_messageN)__name__
__module____qualname__bool__annotations__r   str     z/Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/healthcheck/connection_probe.pyr   r      s    C= r   r   c                   D    e Zd ZdZdedej                  fdZdede	fdZ
y)	ConnectionProbea  
    Provides functionality to probe and verify the connection to a remote server.

    This class is designed to check the availability and responsiveness of a server
    by performing a lightweight connection test to a specific endpoint. It encapsulates
    the logic to send a request and interpret its response, ensuring it is simple to
    evaluate the connection status programmatically.
    base_urlclientc                 F    t        j                  |      | _        || _        y)aB  
        Initializes an instance with a base URL and an HTTP client.

        This constructor sets up the necessary URLs and initializes the
        client needed to make requests.

        Args:
            base_url: The base URL for API requests.
            client: The HTTP client instance to make API calls.
        N)r   get_is_alive_ping_url	_ping_url_client)selfr   r   s      r   __init__zConnectionProbe.__init__   s     %::8Dr   timeoutreturnc                    	 | j                   j                  | j                  |      }|j                  dk(  rt	        dd      S t	        dd|j                         S # t
        j                  t
        j                  f$ r}t	        dd|       cY d}~S d}~wt        $ r2}t        j                  d	|d
       t	        dd|       cY d}~S d}~ww xY w)a  
        Checks the connection to a specified server by sending a ping request and evaluates the response.

        This method performs a basic health check by sending an HTTP GET request to the server's ping URL.
        If the response status code is 200, it indicates the server is healthy, otherwise it is considered unhealthy.
        Handles timeouts and other unexpected exceptions during the connection attempt.

        Args:
            timeout: The maximum duration, in seconds, to wait for the server's response before timing out.

        Returns:
            An object indicating whether the server is healthy and, if not, the associated error message.
        )r      TN)r	   r
   FzUnexpected status code: zConnection error: z8Unexpected error while checking connection to server: %s)exc_infozUnexpected error: )r   getr   status_coder   httpxConnectErrorTimeoutException	ExceptionLOGGER	exception)r   r   responsees       r   check_connectionz ConnectionProbe.check_connection)   s    	Y||'''HH##s*"d$GG"$$<X=Q=Q<R"S  ""E$:$:; 	Y%ASTUSV?WXX 	YJ  
 %ASTUSV?WXX	Ys6   AA A #CBCC#'C
CCN)r   r   r   __doc__r   r   OpikHttpxClientr   floatr   r-   r   r   r   r   r      s6     l.J.J Y Y+ Yr   r   )loggingtypingr   r   r%   opikr   r   	getLoggerr   r)   r   r   r   r   r   <module>r5      s=     '  * 
		8	$!* !
7Y 7Yr   