
    E6i2                         d dl Z d dlZd dlZd dlmZ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mZ dd	lmZmZmZ dd
lmZ ddlmZ  edd      Z G d de      Zd Zd Zy)    N)
namedtupleOrderedDict)request)http_method_funcs   )
HTTPStatusabort)marshalmarshal_with)ModelOrderedModelSchemaModelRequestParser)mergeResourceRoutezresource urls route_doc kwargsc                      e Zd ZdZ	 	 	 	 	 	 d dZed        Zd Zd Zd Z	d!d	Z
d
 Zd Zd Zd"dZd#dZd Zd Zd Zd Zd Zd Zdej.                  dfdZd Zd Zd Zd$dZd!dZd!dZd Zd Z d Z!ed        Z"y)%	Namespaceak  
    Group resources together.

    Namespace is to API what :class:`flask:flask.Blueprint` is for :class:`flask:flask.Flask`.

    :param str name: The namespace name
    :param str description: An optional short description
    :param str path: An optional prefix path. If not provided, prefix is ``/+name``
    :param list decorators: A list of decorators to apply to each resources
    :param bool validate: Whether or not to perform validation on this namespace
    :param bool ordered: Whether or not to preserve order on models and marshalling
    :param Api api: an optional API to attache to the namespace
    NFc                    || _         || _        || _        d | _        || _        i | _        i | _        |r|ng | _        g | _        t               | _
        d | _        || _        || _        g | _        d|v r| j                  j                  |d          t!        j"                  t$        dz   | j                   z         | _        y )Napi.)namedescription_path_schema	_validatemodelsurls
decorators	resourcesr   error_handlersdefault_error_handlerauthorizationsorderedapisappendlogging	getLogger__name__logger)	selfr   r   pathr    validater$   r%   kwargss	            X/home/azureuser/techstart-app/venv/lib/python3.12/site-packages/flask_restx/namespace.py__init__zNamespace.__init__#   s     	&
!	(2*)m%)",	F?IIVE]+''3(BC    c                 Z    | j                   xs d| j                  z   j                  d      S )N/)r   r   rstripr,   s    r0   r-   zNamespace.pathA   s$    

/sTYY77<<r2   c                     |j                  di       }| j                  j                  t        ||||             | j                  D ]+  }|j                  | |      } |j                  | |g|i | - y)a/  
        Register a Resource for a given API Namespace

        :param Resource resource: the resource ro register
        :param str urls: one or more url routes to match for the resource,
                         standard flask routing rules apply.
                         Any url variables will be passed to the resource method as args.
        :param str endpoint: endpoint name (defaults to :meth:`Resource.__name__.lower`
            Can be used to reference this route in :class:`fields.Url` fields
        :param list|tuple resource_class_args: args to be forwarded to the constructor of the resource.
        :param dict resource_class_kwargs: kwargs to be forwarded to the constructor of the resource.

        Additional keyword arguments not specified above will be passed as-is
        to :meth:`flask.Flask.add_url_rule`.

        Examples::

            namespace.add_resource(HelloWorld, '/', '/hello')
            namespace.add_resource(Foo, '/foo', endpoint="foo")
            namespace.add_resource(FooSpecial, '/special/foo', endpoint="foo")
        	route_docN)popr!   r'   r   r&   ns_urlsregister_resource)r,   resourcer   r/   r8   r   r:   s          r0   add_resourcezNamespace.add_resourceE   sr    , JJ{B/	mHdIvNO99 	FCkk$-G!C!!$E7EfE	Fr2   c                       fd}|S )z1
        A decorator to route resources.
        c                     j                  dd       }|j                  | |      d<    j                  | gi  | S )Ndocr8   )r9   
_build_docr=   )clsr@   r/   r,   r   s     r0   wrapperz Namespace.route.<locals>.wrapperf   sL    **UD)C&*ooc3&?{#Dc3D3F3Jr2    )r,   r   r/   rC   s   ``` r0   routezNamespace.routea   s    
	 r2   c                 6   |du ryt        |       t        |       t        D ]_  }||v s||   du rt        ||          t        ||          d||   v s4t        ||   d   t        t
        f      rQ||   d   g||   d<   a t        t        |di       |      S )NFexpect
__apidoc__)unshortcut_params_descriptionhandle_deprecationsr   
isinstancelisttupler   getattr)r,   rB   r@   http_methods       r0   rA   zNamespace._build_docp   s    %<%c*C , 		NKc!{#u,-c+.>?#C$45s;//
$X.u9 36k2B82L1MC$X.		N WS,3S99r2   c                 l     t        |t              r|d<   t        |t              r|nd fd}|S )zAA decorator to add some api documentation to the decorated objectidTc                 >    j                  | rnd      | _        | S )NF)rA   rH   )
documentedr/   r,   shows    r0   rC   zNamespace.doc.<locals>.wrapper   s%    $(OOdF%J! r2   )rK   strbool)r,   shortcutr/   rC   rT   s   ` ` @r0   r@   zNamespace.doc   s2    h$#F4L%h5x4	 r2   c                 0     | j                  d      |      S )z>A decorator to hide a resource or a method from specificationsFr@   r,   funcs     r0   hidezNamespace.hide   s    txxt$$r2   c                     t        |i | y)zd
        Properly abort the current request

        See: :func:`~flask_restx.errors.abort`
        Nr	   r,   argsr/   s      r0   r
   zNamespace.abort   s     	tvr2   c                 d    || j                   |<   | j                  D ]  }||j                   |<    |S N)r   r&   )r,   r   
definitionr   s       r0   	add_modelzNamespace.add_model   s7    &D99 	*C)CJJt	*r2   c                     | j                   rt        nt        } |||||      }|j                  j	                  |       | j                  ||      S )z
        Register a model

        :param bool strict - should model validation raise error when non-specified param
                             is provided?

        .. seealso:: :class:`Model`
        )maskstrict)r%   r   r   rH   updaterc   )r,   r   modelre   rf   r/   rB   s          r0   rh   zNamespace.model   sF     #lllD%d6:'~~dE**r2   c                 >    t        ||      }| j                  ||      S )zG
        Register a model

        .. seealso:: :class:`Model`
        )r   rc   )r,   r   schemarh   s       r0   schema_modelzNamespace.schema_model   s!     D&)~~dE**r2   c                     t        |t              r||gz   }t        j                  |g| }nt        j                  |||      }| j	                  ||      S )zr
        Extend a model (Duplicate all fields)

        :deprecated: since 0.9. Use :meth:`clone` instead
        )rK   rL   r   extendrc   )r,   r   parentfieldsparentsrh   s         r0   rm   zNamespace.extend   sP     fd#x'GLL00ELLvv6E~~dE**r2   c                 R    t        j                  |g| }| j                  ||      S )z
        Clone a model (Duplicate all fields)

        :param str name: the resulting model name
        :param specs: a list of models from which to clone the fields

        .. seealso:: :meth:`Model.clone`

        )r   clonerc   r,   r   specsrh   s       r0   rr   zNamespace.clone   s(     D)5)~~dE**r2   c                 R    t        j                  |g| }| j                  ||      S )z~
        Inherit a model (use the Swagger composition pattern aka. allOf)

        .. seealso:: :meth:`Model.inherit`
        )r   inheritrc   rs   s       r0   rv   zNamespace.inherit   s(     d+U+~~dE**r2   c                     g }|j                  d| j                        |d}|D ]  }|j                  |         | j                  di |S )z
        A decorator to Specify the expected input model

        :param ModelBase|Parse inputs: An expect model or request parser
        :param bool validate: whether to perform validation or not

        r.   )r.   rG   rD   )getr   r'   r@   )r,   inputsr/   rG   paramsparams         r0   rG   zNamespace.expect   sR     $jjT^^DPVW 	!EMM% 	!txx!&!!r2   c                     t               S )z%Instanciate a :class:`~RequestParser`r   r6   s    r0   parserzNamespace.parser   s
    r2   c                 B    t        t        |di       ddi      |_        |S )z/Allow to specify nested lists for documentationrH   as_listT)r   rN   rH   )r,   fields     r0   r   zNamespace.as_list   s$     b!AItCTUr2   c                 &      fd}|S )a  
        A decorator specifying the fields to use for serialization.

        :param bool as_list: Indicate that the return type is a list (for the documentation)
        :param int code: Optionally give the expected HTTP response code if its different from 200

        c                     t              rgfnfij                  dd      d}t        t        | di       |      | _         t        fdj                  i|       S )Nre   T)	responses__mask__rH   r%   )rU   rx   r   rN   rH   r   r%   )r[   r@   r   coder   ro   r/   r,   s     r0   rC   z'Namespace.marshal_with.<locals>.wrapper   s     I !,fXv>%vv6
 #JJD	C $GD,$CSIDOG<GGGMMr2   rD   )r,   ro   r   r   r   r/   rC   s   `````` r0   r   zNamespace.marshal_with   s    	N 	N r2   c                 *     | j                   |dfi |S )zHA shortcut decorator for :meth:`~Api.marshal_with` with ``as_list=True``T)r   )r,   ro   r/   s      r0   marshal_list_withzNamespace.marshal_list_with  s     t  888r2   c                     t        |i |S )z(A shortcut to the :func:`marshal` helper)r   r^   s      r0   r   zNamespace.marshal  s    '''r2   c                 r     t        j                        rt        t              r fd}|S  _        S )z>A decorator to register an error handler for a given exceptionc                 &    | j                   <   | S ra   )r"   )r[   	exceptionr,   s    r0   rC   z'Namespace.errorhandler.<locals>.wrapper  s    15##I.r2   )inspectisclass
issubclass	Exceptionr#   )r,   r   rC   s   `` r0   errorhandlerzNamespace.errorhandler  s4    ??9%*Y	*J N *3D&r2   c                 B    |}||d<   ||d<   | j                  ||i      S )z
        A decorator to specify one of the expected parameters

        :param str name: the parameter name
        :param str description: a small description
        :param str _in: the parameter location `(query|header|formData|body|cookie)`
        inr   )rz   rY   )r,   r   r   _inr/   r{   s         r0   r{   zNamespace.param#  s1     d*mxxe}x--r2   c                 B    | j                  t        |      |||fi      S )z
        A decorator to specify one of the expected responses

        :param int code: the HTTP status code
        :param str description: a small description about the response
        :param ModelBase model: an optional response model

        )r   )r@   rU   )r,   r   r   rh   r/   s        r0   responsezNamespace.response0  s%     xx3t9{E6.J"KxLLr2   c                 T    d|i}|j                  |       | j                  ||i      S )z
        A decorator to specify one of the expected headers

        :param str name: the HTTP header name
        :param str description: a description about the header

        r   )headersrg   r@   )r,   r   r   r/   headers        r0   r   zNamespace.header;  s/      -fxxvx//r2   c                 &    | j                  |      S )z9A decorator to specify the MIME types the API can produce)producesrY   )r,   	mimetypess     r0   r   zNamespace.producesG  s    xxx++r2   c                 2     | j                  d      |      S )z8A decorator to mark a resource or a method as deprecatedT)
deprecatedrY   rZ   s     r0   r   zNamespace.deprecatedK  s    (txx4x(..r2   c                 V    |D ]  }|j                  |        | j                  |      S )a  
        A decorator to expose vendor extensions.

        Extensions can be submitted as dict or kwargs.
        The ``x-`` prefix is optionnal and will be added if missing.

        See: http://swagger.io/specification/#specification-extensions-128
        )vendorr   )r,   r_   r/   args       r0   r   zNamespace.vendorO  s0      	CMM#	xxvx&&r2   c                 *    t        j                         S )z6Store the input payload in the current request context)r   get_jsonr6   s    r0   payloadzNamespace.payload\  s     !!r2   )NNNNNFra   )NNNF)NN)Nquery)#r*   
__module____qualname____doc__r1   propertyr-   r=   rE   rA   r@   r\   r
   rc   rh   rk   rm   rr   rv   rG   r}   r   r   OKr   r   r   r   r{   r   r   r   r   r   r   rD   r2   r0   r   r      s    " D< = =F8:"%+++++" $*--T69(.	M
0,/' " "r2   r   c                 z    d| v r7| d   j                         D ]   \  }}t        |t              sd|i| d   |<   " y y )Nrz   r   )itemsrK   rU   )datar   r   s      r0   rI   rI   b  sO    4!%h!5!5!7 	DD++s+(5{'CXt$	D r2   c                 &   d| v rDt        j                  dt        d       | j                  dg       | j	                  d      gz   | d<   d| v rEt        j                  dt        d       | j                  dg       | j	                  d      gz   | d<   y y )Nr}   z6The parser attribute is deprecated, use expect instead   )
stacklevelrG   bodyz4The body attribute is deprecated, use expect instead)warningswarnDeprecationWarningrx   r9   rY   s    r0   rJ   rJ   i  s    3D	

 "-1B0CCH}B	

 "-0AAH r2   )r   r   r(   collectionsr   r   flaskr   flask.viewsr   _httpr   errorsr
   marshallingr   r   rh   r   r   r   reqparser   utilsr   r   objectr   rI   rJ   rD   r2   r0   <module>r      sV       /  )   . 3 3 #  ?,LMK" K"\
DBr2   