o
    i                     @   s|   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	 ddl
mZ dd	lmZ d
efddZdee d
efddZdS )    )
HTTPStatus)Optional)Request)HTTPException   )logger   )SessionManager)SageMakerSessionHeaderraw_requestc                 C   s   | j sdS | j tjS )zExtract the session ID from the request headers.

    Args:
        raw_request: FastAPI Request object

    Returns:
        Session ID string if present in headers, None otherwise
    N)headersgetr
   
SESSION_ID)r    r   n/home/ubuntu/.local/lib/python3.10/site-packages/model_hosting_container_standards/sagemaker/sessions/utils.pyget_session_id_from_request   s   	r   session_managerc                 C   sX   t |}| du r%|dur#tdtj d ttjjdtj dddS | 	|}|S )a  Retrieve the session associated with the request.

    Args:
        session_manager: SessionManager instance, or None if sessions disabled
        raw_request: FastAPI Request object containing session ID in headers

    Returns:
        Session instance if found, None if no session ID in request

    Raises:
        HTTPException: If session header is present but sessions are not enabled (400 BAD_REQUEST)
        ValueError: If session_id is not found in the session manager (propagated from get_session)
    Nz0Invalid payload. stateful sessions not enabled, z header not supported)status_codedetail)
r   r   errorr
   r   r   r   BAD_REQUESTvalueget_session)r   r   
session_idsessionr   r   r   r      s   
r   N)httpr   typingr   fastapir   fastapi.exceptionsr   logging_configr   managerr	   modelsr
   r   r   r   r   r   r   <module>   s    