
    "<i                         d Z ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddl
mZ  e	e          Z G d	 d
          Zd
gZdS )zHRequest director using openapi-core for stateless HTTP request building.    )Any)urljoinN)
SchemaPath)
get_logger   )	HTTPRoutec                       e Zd ZdZdefdZ	 ddedeee	f         dede
j        fd	Zdedeee	f         deeee	f         eee	f         eee	f         e	f         fd
Zdedeee	f         dedefdZdS )RequestDirectorzMBuilds httpx.Request objects from HTTPRoute and arguments using openapi-core.specc                     || _         dS )z+Initialize with a parsed SchemaPath object.N)_spec)selfr   s     /Users/kimhansen/Desktop/03 Workspace/ceo-agents/chl-effectiveness/mcp-servers/whoop/.venv/lib/python3.11/site-packages/fastmcp/utilities/openapi/director.py__init__zRequestDirector.__init__   s    


    http://localhostroute	flat_argsbase_urlreturnc           
         t                               d|j         d|j         d|            |                     ||          \  }}}}t                               d| d| d| d|            |                     |j        ||          }|j                                        }	|r|nd}
|r|nd}d}d}|"t          |t          t          z            r|}n|}t          j        |	||
|||	          S )
a  
        Constructs a final httpx.Request object, handling all OpenAPI serialization.

        Args:
            route: HTTPRoute containing OpenAPI operation details
            flat_args: Flattened arguments from LLM (may include suffixed parameters)
            base_url: Base URL for the request

        Returns:
            httpx.Request: Properly formatted HTTP request
        zBuilding request for  z with args: zUnflattened - path: z	, query: z, headers: z, body: N)methodurlparamsheadersjsoncontent)loggerdebugr   path_unflatten_arguments
_build_urlupper
isinstancedictlisthttpxRequest)r   r   r   r   path_paramsquery_paramsheader_paramsbodyr   r   r   r   	json_bodyr   s                 r   buildzRequestDirector.build   sF   " 	VELVV5:VV9VV	
 	
 	

 :>9R9R9:
 :
6\=$ 	o;ooooR_ooimoo	
 	
 	

 ooej+x@@ l((**!-74#0:--d7;	&* $t,,  		 }
 
 
 	
r   c                    i }i }i }i }t          |d          r|j        r|                                D ]\  }}|||j        vr&t                              d| d|j                    7|j        |         }	|	d         }
|	d         }|
dk    r|||<   `|
dk    r|||<   l|
d	k    r|||<   x|
d
k    r|||<   t                              d|
 d|            nt                              d           i }|j        D ]}|j        ||j	        <   |                                D ]\  }}|d|v rA|
                    dd          \  }}
|
dv r$|
dk    r|||<   n|
dk    r|||<   n|
d	k    r|||<   M||v r,||         }
|
dk    r|||<   e|
dk    r|||<   q|
d	k    r|||<   }|||<   d}|r|j        r|j        j        rt          t          |j        j                            }|j        j        |         }|                    d          dk    r|}nGt!          |          dk    r/t          t          |                                                    }n|}n|}||||fS )aF  
        Maps flat arguments back to their OpenAPI locations using the parameter map.

        Args:
            route: HTTPRoute with parameter_map containing location mappings
            flat_args: Flat arguments from LLM call

        Returns:
            Tuple of (path_params, query_params, header_params, body)
        parameter_mapNz
Argument 'z!' not found in parameter map for locationopenapi_namer!   queryheaderr-   zUnknown parameter location 'z' for z2No parameter map available, using fallback mapping__r   )r!   r4   r5   typeobject)hasattrr1   itemsr   warningoperation_idr    
parametersr2   namersplitrequest_bodycontent_schemanextitergetlenvalues)r   r   r   r*   r+   r,   
body_propsarg_namevaluemappingr2   r3   param_locationsparam	base_namer-   content_typebody_schemas                     r   r"   z$RequestDirector._unflatten_argumentsP   s_    
 5/** @	1u/B @	1#,??#4#4  %=5#666NNdXddPUPbdd   -h7":.&~6v%%05K--((16L..))27M,//''/4J|,,NNQxQQxQQ   /8 LLMNNN !O) = =.3n
++ $-??#4#4 1 1%= 8##*2//$*B*B'Ix#>>>#v--5:K	22%006;L33%117<M)4  ...x8H6))05H--!W,,16X..!X--27h/ ,1Jx((  	"! "e&8&G "#D);)J$K$KLL#0?M??6**h66%DD__))  Z%6%6%8%8 9 9::DD &DD!L-==r   path_templater*   c                    |}|                                 D ]2\  }}d| d}||v r#|                    |t          |                    }3t          |                    d          dz   |                    d                    S )aG  
        Build URL by substituting path parameters in the template.

        Args:
            path_template: OpenAPI path template (e.g., "/users/{id}")
            path_params: Path parameter values
            base_url: Base URL to prepend

        Returns:
            Complete URL with path parameters substituted
        {}/)r:   replacestrr   rstriplstrip)r   rP   r*   r   url_path
param_nameparam_valueplaceholders           r   r#   zRequestDirector._build_url   s     !'2'8'8':': 	K 	K#J-z---Kh&&#++K[9I9IJJ xs++c18??33G3GHHHr   N)r   )__name__
__module____qualname____doc__r   r   r   r&   rV   r   r(   r)   r/   tupler"   r#    r   r   r
   r
      s,       WWZ     +	7
 7
7
 S>7
 	7

 
7
 7
 7
 7
rj>j>+/S>j>	tCH~tCH~tCH~sB	Cj> j> j> j>XI I/3CH~IILI	I I I I I Ir   r
   )r`   typingr   urllib.parser   r(   jsonschema_pathr   fastmcp.utilities.loggingr   modelsr   r]   r   r
   __all__rb   r   r   <module>ri      s    N N                    & & & & & & 0 0 0 0 0 0      	H		BI BI BI BI BI BI BI BIL 
r   