U
    dV                     @   s|   d dl mZ d dlmZ d dlmZ edd  dkrDd dlm	Z
 nd dlm
Z
 dd	lmZ dd
lmZ G dd deZdS )    )request)
MethodView)__version__.2)Response)BaseResponse   )	ModelBase)unpackc                   @   s<   e Zd ZdZdZg ZdddZdd Zddd	Zd
d Z	dS )Resourcea  
    Represents an abstract RESTX resource.

    Concrete resources should extend from this class
    and expose methods for each supported HTTP method.
    If a resource is invoked with an unsupported HTTP method,
    the API will return a response with status 405 Method Not Allowed.
    Otherwise the appropriate method is called and passed all arguments
    from the url rule used when adding the resource to an Api instance.
    See :meth:`~flask_restx.Api.add_resource` for details.
    Nc                 O   s
   || _ d S )N)api)selfr   argskwargs r   8/tmp/pip-unpacked-wheel-dt_sn2ih/flask_restx/resource.py__init__   s    zResource.__init__c                 O   s   t | tj d }|d kr0tjdkr0t | dd }|d k	sFtdtj | jD ]}||}qL| | |||}t|tr||S | j	pi }tj
j|d d}||krt|\}}	}
|| ||	|
}||jd< |S |S )NHEADgetzUnimplemented method %r)defaultzContent-Type)getattrr   methodlowerAssertionErrormethod_decoratorsvalidate_payload
isinstancer   representationsZaccept_mimetypes
best_matchr   headers)r   r   r   meth	decoratorrespr   	mediatypedatacoder    r   r   r   dispatch_request"   s$    






zResource.dispatch_requestFc                 C   s\   t  }|rBt|tr|n|g}|D ]}||| jj| jj q$n||| jj| jj dS )z
        :param ModelBase expect: the expected model for the input payload
        :param bool collection: False if a single object of a resource is
        expected, True if a collection of objects of a resource is expected.
        N)r   Zget_jsonr   listvalidater   ZrefresolverZformat_checker)r   expect
collectionr%   objr   r   r   Z__validate_payload>   s    zResource.__validate_payloadc                 C   s   t |dddk	r|j}|dd}|dk	r.|n| jj}|r|dg D ]R}t|trt|dkrt|d tr| j	|d dd	 t|trF| j	|dd	 qFdS )
z;Perform a payload validation on expected model if necessary
__apidoc__Fr)   Nr*   r	   r   T)r+   )
r   r-   r   r   	_validater   r(   lenr
   _Resource__validate_payload)r   funcdocr)   r*   r   r   r   r   M   s    
zResource.validate_payload)N)F)
__name__
__module____qualname____doc__r   r   r   r'   r0   r   r   r   r   r   r      s   

r   N)Zflaskr   Zflask.viewsr   Zwerkzeugr   Zwerkzeug_versionsplitZwerkzeug.wrappersr   r   modelr
   utilsr   r   r   r   r   r   <module>   s   