U
    Uc	                     @   s8   d Z ddlZddlmZ ddlmZ G dd deZdS )zDMiddleware for injecting client-side feature flags into the Context.    N)context)errorsc                   @   s    e Zd ZdZdd Zdd ZdS )ClientFeatureFlagsMiddlewarea  Middleware for injecting client-side feature flags into the Context.

    The client webapp is expected to include a json-serialized version of its
    FeatureFlags in the `X-TensorBoard-Feature-Flags` header. This middleware
    extracts the header value and converts it into the client_feature_flags
    property for the DataProvider's Context object, where client_feature_flags
    is a Dict of string keys and arbitrary value types.
    c                 C   s
   || _ dS )zxInitializes this middleware.

        Args:
          application: The WSGI application to wrap (see PEP 3333).
        N)_application)selfZapplication r   L/tmp/pip-unpacked-wheel-g8kmtpbc/tensorboard/backend/client_feature_flags.py__init__!   s    z%ClientFeatureFlagsMiddleware.__init__c                 C   s   | d}|s| ||S zt|}W n  tjk
rH   tdY nX t|ts^tdt	
|j|d}t	|| | ||S )NZ HTTP_X_TENSORBOARD_FEATURE_FLAGSz3X-TensorBoard-Feature-Flags cannot be JSON decoded.z8X-TensorBoard-Feature-Flags cannot be decoded to a dict.)client_feature_flags)getr   jsonloadsJSONDecodeErrorr   ZInvalidArgumentError
isinstancedictr   Zfrom_environreplaceZset_in_environ)r   environZstart_responseZpossible_feature_flagsr
   ctxr   r   r   __call__)   s$    



z%ClientFeatureFlagsMiddleware.__call__N)__name__
__module____qualname____doc__r	   r   r   r   r   r   r      s   	r   )r   r   Ztensorboardr   r   objectr   r   r   r   r   <module>   s   