
    NgI                     F    d dl Z d dlmZ d dlmZ ddefdZddZd	ad
 Z	dS )    N)__version__)
connectionutf-8strictreturnc                     t          | t                    r| S t          | t                    r|                     ||          S t          |           S N)
isinstancestrbytesdecode)sencodingerrorss      V/var/www/html/ai-engine/env/lib/python3.11/site-packages/langsmith/_internal/_patch.py_ensure_strr      sJ    !S !U *xx&)))q66M    c                    t          | dd          | j                            | j                   |i }n|                                }dd |D             vrt          j                    |d<   |                     ||||          S )a  Make the request.

    This function is based on the urllib3 request method, with modifications
    to handle potential issues when using vcrpy in concurrent workloads.

    Args:
        self: The HTTPConnection instance.
        method (str): The HTTP method (e.g., 'GET', 'POST').
        url (str): The URL for the request.
        body (Optional[Any]): The body of the request.
        headers (Optional[dict]): Headers to send with the request.

    Returns:
        The result of calling the parent request method.
    sockNz
user-agentc              3   X   K   | ]%}t          |                                          V  &d S r	   )r   lower).0ks     r   	<genexpr>zrequest.<locals>.<genexpr>+   s2      DDqK		22DDDDDDr   z
User-Agent)bodyheaders)getattrr   
settimeouttimeoutcopyr   _get_default_user_agent_parent_request)selfmethodurlr   r   s        r   requestr&      s    $ tVT"".	T\*** ,,..DDGDDDDD * B D D $HHHr   Fc                      t           rdS ddlm}  |                     t                    |                     d          k    rda dS t
          j        j        d         }|j        fd}|t
          j        _        da dS )ap  Patch the request method of urllib3 to avoid type errors when using vcrpy.

    In concurrent workloads (such as the tracing background queue), the
    connection pool can get in a state where an HTTPConnection is created
    before vcrpy patches the HTTPConnection class. In urllib3 >= 2.0 this isn't
    a problem since they use the proper super().request(...) syntax, but in older
    versions, super(HTTPConnection, self).request is used, resulting in a TypeError
    since self is no longer a subclass of "HTTPConnection" (which at this point
    is vcr.stubs.VCRConnection).

    This method patches the class to fix the super() syntax to avoid mixed inheritance.
    In the case of the LangSmith tracing logic, it doesn't really matter since we always
    exclude cache checks for calls to LangSmith.

    The patch is only applied for urllib3 versions older than 2.0.
    Nr   )versionz2.0Tc                 X    t          j        |           | _        t          | g|R i |S )zHandle parent request.

        This method binds the parent's request method to self and then
        calls our modified request function.
        )	functoolspartialr"   r&   )r#   argskwargsparent_requests      r   new_requestz"patch_urllib3.<locals>.new_requestS   s8      )0FFt-d---f---r   )	_PATCHED	packagingr(   parseurllib3versionr   HTTPConnection	__bases__r&   )r(   parent_classr/   r.   s      @r   patch_urllib3r7   5   s    $  !!!!!!}}^$$e(<(<<< ,6q9L!)N. . . . . )4J%HHHr   )r   r   )NN)
r*   urllib3r   r3   r   r   r   r&   r0   r7    r   r   <module>r:      s        1 1 1 1 1 1           I I I IB ( ( ( ( (r   