U
    dr                     @   s  d dl Z d dlZd dlmZ d dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlmZmZ d dlmZ d dlmZmZmZ d dlmZ zd dlmZ W n  ek
r   d dlmZ Y nX 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#m$Z$m%Z%m&Z&m'Z' d dl(m)Z* e*+dd  dkr8d dl,m-Z. nd dl,m.Z. ddl/m0Z0 ddl1m2Z2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z=m>Z>m?Z? ddl@mAZA ddlBmCZC eDdZEdZFdeAfgZGeHeIZJG d d! d!eKZLG d"d# d#e9ZMd$d% ZNd&d' ZOdS )(    N)chain)OrderedDict)wrapspartial)
MethodType)url_forrequestcurrent_app)make_response)_endpoint_from_view_func)got_request_exception)RefResolver)cached_property)Headers)HTTPExceptionMethodNotAllowedNotFoundNotAcceptableInternalServerError)__version__.2)Response)BaseResponse   )apidoc)
ParseError	MaskError)	Namespace)PostmanCollectionV1)Resource)Swagger)
default_idcamel_to_dashunpack)output_json)
HTTPStatusz(<.*>))zContent-Lengthapplication/jsonc                   @   s  e Zd ZdZdddddddddddddeddddddd	dddddd
fddZdd Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zded1d2Zd3d4 Zd5d6 Zed7d8 Zed9d: Zed;d< Zed=d> Z ed?d@ Z!dAdB Z"dCdD Z#dEdF Z$dGdH Z%dIdJ Z&dKdL Z'dMdN Z(dfdOdPZ)dgdQdRZ*edSdT Z+edUdV Z,e-dhdWdXZ.dYdZ Z/d[d\ Z0d]d^ Z1d_d` Z2dadb Z3dcdd Z4dS )iApia
  
    The main entry point for the application.
    You need to initialize it with a Flask Application: ::

    >>> app = Flask(__name__)
    >>> api = Api(app)

    Alternatively, you can use :meth:`init_app` to set the Flask application
    after it has been constructed.

    The endpoint parameter prefix all views and resources:

        - The API root/documentation will be ``{endpoint}.root``
        - A resource registered as 'resource' will be available as ``{endpoint}.resource``

    :param flask.Flask|flask.Blueprint app: the Flask application object or a Blueprint
    :param str version: The API version (used in Swagger documentation)
    :param str title: The API title (used in Swagger documentation)
    :param str description: The API description (used in Swagger documentation)
    :param str terms_url: The API terms page URL (used in Swagger documentation)
    :param str contact: A contact email for the API (used in Swagger documentation)
    :param str license: The license associated to the API (used in Swagger documentation)
    :param str license_url: The license page URL (used in Swagger documentation)
    :param str endpoint: The API base endpoint (default to 'api).
    :param str default: The default namespace base name (default to 'default')
    :param str default_label: The default namespace label (used in Swagger documentation)
    :param str default_mediatype: The default media type to return
    :param bool validate: Whether or not the API should perform input payload validation.
    :param bool ordered: Whether or not preserve order models and marshalling.
    :param str doc: The documentation path. If set to a false value, documentation is disabled.
                (Default to '/')
    :param list decorators: Decorators to attach to every resource
    :param bool catch_all_404s: Use :meth:`handle_error`
        to handle 404 errors throughout your app
    :param dict authorizations: A Swagger Authorizations declaration as dictionary
    :param bool serve_challenge_on_401: Serve basic authentication challenge with 401
        responses (default 'False')
    :param FormatChecker format_checker: A jsonschema.FormatChecker object that is hooked into
        the Model validator. A default or a custom FormatChecker can be provided (e.g., with custom
        checkers), otherwise the default action is to not enforce any format validation.
    :param url_scheme: If set to a string (e.g. http, https), then the specs_url and base_url will explicitly use this
        scheme regardless of how the application is deployed. This is necessary for some deployments behind a reverse
        proxy.
    :param str default_swagger_filename: The default swagger filename.
    Nz1.0/defaultzDefault namespace Fr'   zswagger.jsonc                 K   sJ  || _ |pd| _|| _|| _|| _|
| _|	| _|| _|| _|| _	|| _
|| _|| _|| _|| _d | _d | _|ppg | _ttttti| _d | _i | _d | _|| _g | _|| _t | _tt | _!i | _"|| _#|| _$|r|ng | _%|| _&|| _'d | _(t) | _*g | _+d | _,d | _-| j.||d/||| dd| _0|| _1|d k	rF|| _,| 2| d S )NZAPIz{0}-declarationr)   )endpointvalidateapipath)3versiontitledescription	terms_urlcontactcontact_emailcontact_urllicenselicense_urlauthorizationssecurityr"   ordered	_validate_doc	_doc_view_default_error_handlertagsr   r   mask_parse_error_handlerr   mask_error_handlererror_handlers_schemamodels_refresolverformat_checker
namespacesdefault_swagger_filenamedictns_pathsDEFAULT_REPRESENTATIONSrepresentationsurlsprefixdefault_mediatype
decoratorscatch_all_404sserve_challenge_on_401blueprint_setupset	endpoints	resourcesapp	blueprint	namespaceformatdefault_namespace
url_schemeinit_app)selfrX   r0   r1   r2   r3   r7   r8   r4   r6   r5   r9   r:   docr"   r*   Zdefault_labelr-   r@   rO   r;   rP   rQ   rR   rS   rG   r]   rI   kwargs rb   3/tmp/pip-unpacked-wheel-dt_sn2ih/flask_restx/api.py__init__m   sp    

  

zApi.__init__c                 K   s   || _ |d| j| _|d| j| _|d| j| _|d| j| _|d| j| _|d| j| _|d| j| _|d| j	| _	|d	| j
| _
|d
d| _| | | | z|| j W n tk
r   | | Y nX || _dS )a  
        Allow to lazy register the API on a Flask application::

        >>> app = Flask(__name__)
        >>> api = Api()
        >>> api.init_app(app)

        :param flask.Flask app: the Flask application object
        :param str title: The API title (used in Swagger documentation)
        :param str description: The API description (used in Swagger documentation)
        :param str terms_url: The API terms page URL (used in Swagger documentation)
        :param str contact: A contact email for the API (used in Swagger documentation)
        :param str license: The license associated to the API (used in Swagger documentation)
        :param str license_url: The license page URL (used in Swagger documentation)
        :param url_scheme: If set to a string (e.g. http, https), then the specs_url and base_url will explicitly use
            this scheme regardless of how the application is deployed. This is necessary for some deployments behind a
            reverse proxy.
        r1   r2   r3   r4   r6   r5   r7   r8   r]   Z	add_specsTN)rX   getr1   r2   r3   r4   r6   r5   r7   r8   r]   
_add_specs_register_specs_register_docrecord_deferred_blueprint_initAttributeError	_init_apprY   )r_   rX   ra   rb   rb   rc   r^      s$    

zApi.init_appc                 C   s   t | j|j|_t | j|j|_t| jdkrX| jD ]"\}}}}| j|||f|| q4| jD ]}| || q^| 	| | j
dk	r| j
n|jdd| _
|jdd |jdd |jd	d d
|jkr|jd
 |jd< tdt dS )z
        Perform initialization actions with the given :class:`flask.Flask` object.

        :param flask.Flask app: The flask application object
        r   NZRESTX_VALIDATEFZRESTX_MASK_HEADERzX-FieldsZRESTX_MASK_SWAGGERTZRESTX_INCLUDE_ALL_MODELSZERROR_404_HELPRESTX_ERROR_404_HELPzt'ERROR_404_HELP' config setting is deprecated and will be removed in the future. Use 'RESTX_ERROR_404_HELP' instead.)r   error_routerZhandle_exceptionZhandle_user_exceptionlenrW   _register_viewrH   _configure_namespace_logger_register_apidocr<   configre   
setdefaultwarningswarnDeprecationWarning)r_   rX   resourcerZ   rN   ra   nsrb   rb   rc   rl      s0     


zApi._init_appc                 C   s6   zt | j|W S  tk
r0   td|Y nX d S )NzApi does not have {0} attribute)getattrr\   rk   r[   r_   namerb   rb   rc   __getattr__  s    zApi.__getattr__c                 C   s    || j |f}ddd |D S )ar  
        This method is used to defer the construction of the final url in
        the case that the Api is created with a Blueprint.

        :param url_part: The part of the url the endpoint is registered with
        :param registration_prefix: The part of the url contributed by the
            blueprint.  Generally speaking, BlueprintSetupState.url_prefix
        r+   c                 s   s   | ]}|r|V  qd S Nrb   ).0partrb   rb   rc   	<genexpr>)  s      z$Api._complete_url.<locals>.<genexpr>)rO   join)r_   Zurl_partZregistration_prefixpartsrb   rb   rc   _complete_url  s    	zApi._complete_urlc                 C   s2   |j di }|dds&|tj d|d< d S )NZrestxZapidoc_registeredFT)
extensionsrt   re   Zregister_blueprintr   )r_   rX   confrb   rb   rc   rr   +  s    zApi._register_apidocc                 C   s>   | j r:td}| j|t| jd| j || fd | j| d S )Nspecsr)   )r,   resource_class_args)rf   strrp   SwaggerViewr\   rI   rV   add)r_   app_or_blueprintr,   rb   rb   rc   rg   1  s    zApi._register_specsc                 C   s8   | j r| jr|| jd| j || jp*dd| j d S )Nr`   r)   root)rf   r=   add_url_rule
render_docrO   render_root)r_   r   rb   rb   rc   rh   >  s    zApi._register_docc                 O   sp   | dd }t|p| ||}||d< | j| | jd k	rX| j| j||f|| n| j||||f |S )Nr,   )	popr   default_endpointrV   r   rX   rp   rW   append)r_   rZ   rx   rN   ra   r,   rb   rb   rc   register_resourceD  s    
zApi.register_resourcec                 C   s.   |j jD ]}|j | q|j |j j d S r~   )loggerhandlers
addHandlersetLevellevel)r_   rX   rZ   handlerrb   rb   rc   rq   Q  s    zApi._configure_namespace_loggerc                 O   s  | dd pt|j}| dd}| di }|t|di krl|j| jd }	|	|krld}
t|
||	jf |  |_||_	| 
|j|| f||}t|j| jD ]}||}q|D ]\}| jr| jr| jj|fd|i| qqt| j|}n| |d	}|j|fd|i| qd S )
Nr,   r   rb   resource_class_kwargsview_functionsZ
view_classz2This endpoint (%s) is already set to the class %s.	view_funcr+   )r   r#   __name__rz   r   __dict__
ValueErrormediatypes_method
mediatypesr,   outputZas_viewr   rQ   rY   rT   r   r   r   )r_   rX   rx   rZ   rN   ra   r,   r   r   Zprevious_view_classmsgZresource_func	decoratorurlrulerb   rb   rc   rp   V  sH    
 
zApi._register_viewc                    s   t   fdd}|S )z
        Wraps a resource (as a flask view function),
        for cases where the resource does not directly return a response object

        :param resource: The resource as a flask view function
        c                     s6    | |}t |tr|S t|\}}}j|||dS )N)headers)
isinstancer   r$   r
   )argsra   respdatacoder   rx   r_   rb   rc   wrapper  s
    

zApi.output.<locals>.wrapper)r   )r_   rx   r   rb   r   rc   r     s    z
Api.outputc                 O   s   | ddp| j}tjj| j|d}|dkr2t || jkr`| j| |f||}||jd< |S |dkrtt	|f||}d|jd< |S t
 dS )a  
        Looks up the representation transformer for the requested media
        type, invoking the transformer to create a response object. This
        defaults to default_mediatype if no transformer is found for the
        requested mediatype. If default_mediatype is None, a 406 Not
        Acceptable response will be sent as per RFC 2616 section 14.1

        :param data: Python object containing response data to be transformed
        fallback_mediatypeN)r*   zContent-Type
text/plain)r   rP   r   accept_mimetypes
best_matchrM   r   r   original_flask_make_responser   r   )r_   r   r   ra   rP   	mediatyper   rb   rb   rc   r
     s"    


zApi.make_responsec                 C   s
   || _ |S )z<A decorator to specify a view function for the documentation)r>   )r_   funcrb   rb   rc   documentation  s    zApi.documentationc                 C   s   |  tj d S r~   )abortr&   	NOT_FOUNDr_   rb   rb   rc   r     s    zApi.render_rootc                 C   s*   | j r|   S | js | tj t| S )z8Override this method to customize the documentation page)r>   r=   r   r&   r   r   Zui_forr   rb   rb   rc   r     s
    zApi.render_docc                 C   s\   t |j}|| jk	r"dj||d}|| jkrXd}dj||d}|| jkrN|}qX|d7 }q0|S )a  
        Provide a default endpoint for a resource on a given namespace.

        Endpoints are ensured not to collide.

        Override this method specify a custom algorithm for default endpoint.

        :param Resource resource: the resource for which we want an endpoint
        :param Namespace namespace: the namespace holding the resource
        :returns str: An endpoint name
        z{ns.name}_{endpoint})ry   r,      z{base}_{suffix})basesuffixr   )r#   r   r\   r[   rV   )r_   rx   rZ   r,   r   Znew_endpointrb   rb   rc   r     s    




zApi.default_endpointc                 C   s   | j |S r~   )rK   re   )r_   ry   rb   rb   rc   get_ns_path  s    zApi.get_ns_pathc                    s"   |  |p|j  fdd|D S )Nc                    s   g | ]} | qS rb   rb   )r   r   r/   rb   rc   
<listcomp>  s     zApi.ns_urls.<locals>.<listcomp>)r   r/   )r_   ry   rN   rb   r   rc   ns_urls  s    zApi.ns_urlsc                 C   s   || j kr>| j | | |jkr,|j|  |dk	r>|| j|< |jD ]*}| ||j}| j||jf||j	 qD|j
 D ]\}}|| j
|< qz| js| jdk	r| | j| dS )a  
        This method registers resources from namespace for current instance of api.
        You can use argument path for definition custom prefix url for namespace.

        :param Namespace ns: the namespace
        :param path: registration prefix of namespace
        N)rH   r   ZapisrK   rW   r   rN   r   rx   ra   rE   itemsrY   rX   rq   )r_   ry   r/   rrN   r|   Z
definitionrb   rb   rc   add_namespace  s    



zApi.add_namespacec                 O   s*   | d| j|d< t||}| | |S )z\
        A namespace factory.

        :returns Namespace: a new namespace instance
        r;   )re   r;   r   r   )r_   r   ra   ry   rb   rb   rc   rZ     s    

zApi.namespacec                 C   s   | j rd| j j|S |S d S )N{0}.{1})rY   r[   r|   r{   rb   rb   rc   r,     s    zApi.endpointc                 C   s(   | j dkrdnd}t| d| j |dS )z
        The Swagger specifications relative url (ie. `swagger.json`). If
        the spec_url_scheme attribute is set, then the full url is provided instead
        (e.g. http://localhost/swaggger.json).

        :rtype: str
        NTr   Z_scheme	_external)r]   r   r,   )r_   Zexternalrb   rb   rc   	specs_url  s    	  zApi.specs_urlc                 C   s   t | d| jddS )z@
        The API base absolute url

        :rtype: str
        r   Tr   )r   r,   r]   r   rb   rb   rc   base_url"  s    zApi.base_urlc                 C   s   t | dddS )z3
        The API path

        :rtype: str
        r   F)r   )r   r,   r   rb   rb   rc   	base_path+  s    zApi.base_pathc                 C   sJ   | j sDzt|  | _ W n* tk
rB   d}t| d|i Y S X | j S )zz
        The Swagger specifications/schema for this API

        :returns dict: the schema as a serializable dict
        zUnable to render schemaerror)rD   r!   as_dict	Exceptionlog	exception)r_   r   rb   rb   rc   
__schema__4  s    
zApi.__schema__c                 C   s>   t  }|| j | jD ] }|j D ]\}}|||< q&q|S r~   )r   updaterC   rH   r   )r_   rvry   r   r   rb   rb   rc   _own_and_child_error_handlersF  s    
z!Api._own_and_child_error_handlersc                    s4   t  r&t tr& fdd}|S  _ S dS )z>A decorator to register an error handler for a given exceptionc                    s   | j  < | S r~   )rC   r   r   r_   rb   rc   r   S  s    
z!Api.errorhandler.<locals>.wrapperN)inspectisclass
issubclassr   r?   )r_   r   r   rb   r   rc   errorhandlerO  s
    zApi.errorhandlerc                 C   s<   | j r2|| j jr.|| j jd dd }ndS || jkS )z
        Tests if an endpoint name (not path) belongs to this Api.
        Takes into account the Blueprint name part of the endpoint name.

        :param str endpoint: The name of the endpoint being checked
        :return: bool
        r   r   F)rY   
startswithr|   splitrV   )r_   r,   rb   rb   rc   owns_endpoint]  s
    	zApi.owns_endpointc              
   C   s   t t}z|  W nx tk
rd } z0|jd }|j|dd\}}| |j W Y S d}~X Y n, tk
r|   | j	 Y S  t
k
r   Y nX dS )a5  
        Determine if error should be handled with FR or default Flask

        The goal is to return Flask error handlers for non-FR-related routes,
        and FR errors (with the correct media type) for FR endpoints. This
        method currently handles 404 and 405 errors.

        :return: bool
        r   T)methodZreturn_ruleN)r	   Zcreate_url_adapterr   matchr   Zvalid_methodsr   r,   r   rR   r   )r_   adaptereZvalid_route_methodr   _rb   rb   rc   _should_use_fr_error_handlerm  s    


 
z Api._should_use_fr_error_handlerc                 C   s$   |   rdS tjsdS | tjjS )zGEncapsulating the rules for whether the request was to a Flask endpointTF)r   r   Zurl_ruler   r,   r   rb   rb   rc   _has_fr_route  s
    zApi._has_fr_routec              
   C   sL   |   rDz| |W S  tk
rB } z|| W Y S d}~X Y nX ||S )a  
        This function decides whether the error occurred in a flask-restx
        endpoint or not. If it happened in a flask-restx endpoint, our
        handler will be dispatched. If it happened in an unrelated view, the
        app's original error handler will be dispatched.
        In the event that the error occurred in a flask-restx endpoint but
        the local handler can't resolve the situation, the router will fall
        back onto the original_handler as last resort.

        :param function original_handler: the original Flask error handler for the app
        :param Exception e: the exception raised while handling the request
        N)r   handle_errorr   )r_   original_handlerr   frb   rb   rc   rn     s    zApi.error_routerc                 C   s$   t jd}|dk	r|S t jp"t jS )a  
        Returns the value of the ``PROPAGATE_EXCEPTIONS`` configuration
        value in case it's set, otherwise return true if app.debug or
        app.testing is set. This method was deprecated in Flask 2.3 but
        we still need it for our error handlers.
        ZPROPAGATE_EXCEPTIONSN)r	   rs   re   Ztestingdebug)r_   r   rb   rb   rc   _propagate_exceptions  s    zApi._propagate_exceptionsc                 C   s0  t |tsD|  rDt |t| j sDt \}}}||kr@ n|tj	
dd}i }t }| j D ]2\}}	t ||rf|	|}
t|
tj\}}} qqftjt |d t |trt|j}|rdt|d|ji}| j}n<| jr| |}
t|
tj\}}}ntj}|rd|ji}|r8|
dt||d< t|d|}d}|tjkrzt }|d dkrnd}t| nr|tjkrtj	
d	dr|r| |
dd|d< n8|tjkr| jdkrt| j  }|r|d
 nd}t!D ]}|"|d q| j#||||d}|tj$kr,| %|}|S )z
        Error handler for the API transforms a raised exception into a Flask response,
        with the appropriate HTTP status code and body.

        :param Exception e: the raised Exception object

        ZERROR_INCLUDE_MESSAGET)r   messager2   r   Nr   rm   r   r   )r   )&r   r   r   tupler   keyssysexc_infor	   rs   re   r   r   r$   r&   INTERNAL_SERVER_ERRORr   sendZ_get_current_objectr   rz   phraseZget_responser   r?   r   Zlog_exceptionr   _help_on_404NOT_ACCEPTABLErP   listrM   HEADERS_BLACKLISTr   r
   UNAUTHORIZEDunauthorized)r_   r   exc_type	exc_valuetbZinclude_message_in_responseZdefault_datar   Z	typecheckr   resultr   r   r   r   Zsupported_mediatypesheaderr   rb   rb   rc   r     s     
 



     
zApi.handle_errorc                    sp   t dd tj D  ttj  }|rld	|rD|
dd nddtjdd	 fd	d
|D df}|S )Nc                 S   s    g | ]}t d |j|jfqS )r+   )RE_RULESsubr   )r   r   rb   rb   rc   r     s   z$Api._help_on_404.<locals>.<listcomp>r+   r   z. zYou have requested this URI [z] but did you mean z or c                 3   s   | ]} | V  qd S r~   rb   )r   r   rulesrb   rc   r   "  s     z#Api._help_on_404.<locals>.<genexpr>z ?)rJ   r	   Zurl_mapZ
iter_rulesdifflibget_close_matchesr   r/   r   r   rstrip)r_   r   Zclose_matchesrb   r   rc   r     s"    
zApi._help_on_404c                 C   s   t | |dj|dS )z
        Serialize the API as Postman collection (v1)

        :param bool urlvars: whether to include or not placeholders for query strings
        :param bool swagger: whether to include or not the swagger.json specifications

        )swagger)urlvars)r   r   )r_   r  r  rb   rb   rc   
as_postman(  s    zApi.as_postmanc                 C   s   t  S )z6Store the input payload in the current request context)r   Zget_jsonr   rb   rb   rc   payload2  s    zApi.payloadc                 C   s   | j st| j| _ | j S r~   )rF   r   Zfrom_schemar   r   rb   rb   rc   refresolver7  s    zApi.refresolverc                 K   s   t |r|| j}n| jr$| j| }|d| j |dkrBt|}| j}d|krbt|f|d}| jj	|d| j
j|f |fd|i| dS )a  
        Method used to patch BlueprintSetupState.add_url_rule for setup
        state instance corresponding to this Api instance.  Exists primarily
        to enable _complete_url's function.

        :param blueprint_setup: The BlueprintSetupState instance (self)
        :param rule: A string or callable that takes a string and returns a
            string(_complete_url) that is the url rule for the endpoint
            being registered
        :param endpoint: See BlueprintSetupState.add_url_rule
        :param view_func: See BlueprintSetupState.add_url_rule
        :param **options: See BlueprintSetupState.add_url_rule
        	subdomainNdefaultsz%s.%s)callableZ
url_prefixrt   r	  r   Zurl_defaultsrJ   r   rX   r   rY   r|   )rT   r   r,   r   optionsr
  rb   rb   rc   #_blueprint_setup_add_url_rule_patch=  s&    
z'Api._blueprint_setup_add_url_rule_patchc                 C   sF   || _ |jjdkr(|j|_ttj||_|js6td| 	|j
 dS )a  
        Synchronize prefix between blueprint/api and registration options, then
        perform initialization with setup_state.app :class:`flask.Flask` object.
        When a :class:`flask_restx.Api` object is initialized with a blueprint,
        this method is recorded on the blueprint to be run when the blueprint is later
        registered to a :class:`flask.Flask` object.  This method also monkeypatches
        BlueprintSetupState.add_url_rule with _blueprint_setup_add_url_rule_patch.

        :param setup_state: The setup state object passed to deferred functions
            during blueprint registration
        :type setup_state: flask.blueprints.BlueprintSetupState

        r  z3flask-restx blueprints can only be registered once.N)rT   r   r   Z_original_add_url_ruler   r(   r  Zfirst_registrationr   rl   rX   )r_   Zsetup_staterb   rb   rc   rj   a  s     zApi._deferred_blueprint_initc                    s    fddS )z1Return a method that returns a list of mediatypesc                    s       jg S r~   )r   rP   )Zresource_clsr   rb   rc   <lambda>|      z'Api.mediatypes_method.<locals>.<lambda>rb   r   rb   r   rc   r   z  s    zApi.mediatypes_methodc                 C   s    dd t tjtdddD S )z@Returns a list of requested mediatypes sent in the Accept headerc                 S   s   g | ]\}}|qS rb   rb   )r   hqrb   rb   rc   r     s   z"Api.mediatypes.<locals>.<listcomp>r   T)keyreverse)sortedr   r   operator
itemgetterr   rb   rb   rc   r   ~  s      zApi.mediatypesc                    s    fdd}|S )a  
        Allows additional representation transformers to be declared for the
        api. Transformers are functions that must be decorated with this
        method, passing the mediatype the transformer represents. Three
        arguments are passed to the transformer:

        * The data to be represented in the response body
        * The http status code
        * A dictionary of headers

        The transformer should convert the data appropriately for the mediatype
        and return a Flask response object.

        Ex::

            @api.representation('application/xml')
            def xml(data, code, headers):
                resp = make_response(convert_data_to_xml(data), code)
                resp.headers.extend(headers)
                return resp
        c                    s   | j  < | S r~   )rM   r   r   r_   rb   rc   r     s    
z#Api.representation.<locals>.wrapperrb   )r_   r   r   rb   r  rc   representation  s    zApi.representationc                 C   s.   | j r*tjdd}dd|}||jd< |S )z2Given a response, change it to ask for credentialsZHTTP_BASIC_AUTH_REALMzflask-restxz{0} realm="{1}"ZBasiczWWW-Authenticate)rS   r	   rs   re   r[   r   )r_   responserealm	challengerb   rb   rc   r     s
    
zApi.unauthorizedc                 K   s(   |j }| jrd| jj|}t|f|S )zc
        Generates a URL to the given resource.

        Works like :func:`flask.url_for`.
        r   )r,   rY   r[   r|   r   )r_   rx   valuesr,   rb   rb   rc   r     s    zApi.url_for)N)N)FF)NN)5r   
__module____qualname____doc__r"   rd   r^   rl   r}   r   rr   rg   rh   r   rq   rp   r   r
   r   r   r   r   r   r   r   rZ   r,   propertyr   r   r   r   r   r   r   r   r   r   rn   r   r   r   r  r  r  staticmethodr  rj   r   r   r  r   r   rb   rb   rb   rc   r(   >   s   0
]*%4






b




   #	
r(   c                   @   s    e Zd ZdZdd Zdd ZdS )r   z)Render the Swagger specifications as JSONc                 C   s    | j j}|d|krtjntjfS )Nr   )r.   r   r&   r   OK)r_   Zschemarb   rb   rc   re     s    zSwaggerView.getc                 C   s   dgS )Nr'   rb   r   rb   rb   rc   r     s    zSwaggerView.mediatypesN)r   r  r  r  re   r   rb   rb   rb   rc   r     s   r   c                 C   s   dd | itjfS )zWhen a mask can't be parsedr   zMask parse error: {0}r[   r&   BAD_REQUESTr   rb   rb   rc   rA     s    rA   c                 C   s   dd | itjfS )zWhen any error occurs on maskr   zMask error: {0}r#  r%  rb   rb   rc   rB     s    rB   )Pr  r   	itertoolsr   loggingr  rer   ru   collectionsr   	functoolsr   r   typesr   Zflaskr   r   r	   r
   r   Zflask.helpersr   ImportErrorZflask.scaffoldZflask.signalsr   Z
jsonschemar   Zwerkzeug.utilsr   Zwerkzeug.datastructuresr   Zwerkzeug.exceptionsr   r   r   r   r   Zwerkzeugr   Zwerkzeug_versionr   Zwerkzeug.wrappersr   r   r+   r   maskr   r   rZ   r   Zpostmanr   rx   r    r  r!   utilsr"   r#   r$   rM   r%   Z_httpr&   compiler   r   rL   	getLoggerr   r   objectr(   r   rA   rB   rb   rb   rb   rc   <module>   sb   


       