
    >|h.                         d Z ddl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mZmZmZmZmZmZ  G d
 d      Zy)zResponse processor    N)abc)deepcopy)wraps)jsonify)Response   )
deepupdateget_appcontextprepare_responseremove_noneresolve_schema_instance"set_status_and_headers_in_responseunpack_tuple_responsec            	       t    e Zd ZdZ	 ddddddddZ	 ddddddddddZed        Zed	        Zed
        Z	y)ResponseMixinz)Extend Blueprint to add response handlingN)content_typedescriptionexampleexamplesheadersc                    
 t               j                        }|(t        j                  t	                    j
                  }t        |||||d      
|
d<   
 fd}	|	S )a  Decorator generating an endpoint response

        :param int|str|HTTPStatus status_code: HTTP status code.
            Used if none is returned from the view function.
        :param schema schema|str|dict: :class:`Schema <marshmallow.Schema>`
            class or instance or reference or dict.
            If not None, will be used to serialize response data.
        :param str content_type: Content type of the response.
        :param str description: Description of the response (default: None).
        :param dict example: Example of response message.
        :param dict examples: Examples of response message.
        :param dict headers: Headers returned by the response.

        The decorated function is expected to return the same types of value
        than a typical flask view function, except the body part may be an
        object or a list of objects to serialize with the schema, rather than
        a ``string``.

        If the decorated function returns a ``Response`` object, the ``schema``
        and ``status_code`` parameters are only used to document the resource.
        Only in this case, ``schema`` may be a reference as string or a schema
        definition as dict.

        The `example` and `examples` parameters are mutually exclusive. The
        latter should only be used with OpenAPI 3.

        The `example`, `examples` and `headers` parameters are only used to
        document the resource.

        See :doc:`Response <response>`.
        schemar   r   r   r   r   c                 X    t                fd       }t        t        |di             |_        |j                  j	                  di       j	                  di       j	                  g       j                         |j                  j	                  dg       j                         |S )Nc                     t         | i |      \  }}}t        |t              rt        |||       |S 	|}n	j	                  |      }t               }||d<   t        
j                  |            }t        |||       ||_        |S )Nresult_dump)	r   
isinstancer   r   dumpr
   r   _prepare_response_contentstatus_code)argskwargs
result_rawr_status_code	r_headersr   
appcontextrespfuncr   selfr    s           U/var/www/html/test/engine/venv/lib/python3.12/site-packages/flask_smorest/response.pywrapperz:ResponseMixin.response.<locals>.decorator.<locals>.wrapperU   s     8M$)&)84
M9
 j(36"M9 &% >",K"(++j"9K ,-
,7
=) t==kJK24	R ('2D$    _apidocresponse	responsessuccess_status_codesr   r   getattrr-   
setdefaultappend)r(   r+   resp_docr   r)   r    s   ` r*   	decoratorz)ResponseMixin.response.<locals>.decoratorT   s    4[ D 'ww	2'FGGO**:r:K,K,! OO&&'=rBII+VNr,   r   _make_doc_response_schemahttp
HTTPStatusintphraser   )r)   r    r   r   r   r   r   r   
doc_schemar6   r5   s   ```       @r*   r.   zResponseMixin.response   sy    T )0 33F;
//#k*:;BBK$*"$"
 $0 /	b r,   F)r   r   r   r   r   r   successc                   	 ||n\t        |      }| j                  |      }
|(t        j                  t	                    j
                  }t        |
||||d      |d<   	fd}|S )a  Decorator documenting an alternative response

        :param int|str|HTTPStatus status_code: HTTP status code.
        :param str response: Response reference.
        :param schema schema|str|dict: :class:`Schema <marshmallow.Schema>`
            class or instance or reference or dict.
        :param str description: Description of the response (default: None).
        :param dict example: Example of response message.
        :param dict examples: Examples of response message.
        :param dict headers: Headers returned by the response.
        :param bool success: ``True`` if this response is part of the normal
            flow of the function. Default: ``False``.

        This decorator allows the user to document an alternative response.
        This can be an error managed with :func:`abort <abort>` or any response
        that is not the primary flow of the function documented by
        :meth:`Blueprint.response <Blueprint.response>`.

        When a response reference is passed as ``response``, it is used as
        description and the keyword arguments are ignored. Otherwise, a
        description is built from the keyword arguments.

        See :ref:`document-alternative-responses`.
        r   r   c                 V    t                fd       }t        t        |di             |_        |j                  j	                  di       j	                  di       j	                  g       j                         r+|j                  j	                  dg       j                         |S )Nc                       | i |S N )r!   r"   r(   s     r*   r+   z>ResponseMixin.alt_response.<locals>.decorator.<locals>.wrapper   s    T,V,,r,   r-   r.   r/   r0   r1   )r(   r+   r5   r    r>   s   ` r*   r6   z-ResponseMixin.alt_response.<locals>.decorator   s    4[- - 'ww	2'FGGO**:r:K,K,! **+A2FMM Nr,   r7   )r)   r    r.   r   r   r   r   r   r   r>   r=   r6   r5   s    `       `  @r*   alt_responsezResponseMixin.alt_response   s    L H -V4F 77?J""ooc+.>?FF"(#.& (&H (4H^$	. r,   c                     | S )a  Override this to modify response schema in docs

        This can be used to document a wrapping structure.

            Example: ::

                @staticmethod
                def _make_doc_response_schema(schema):
                    if schema:
                        return type(
                            "Wrap" + schema.__class__.__name__,
                            (ma.Schema,),
                            {"data": ma.fields.Nested(schema)},
                        )
                    return None
        rC   )r   s    r*   r8   z'ResponseMixin._make_doc_response_schema   s	    $ r,   c                     | S )aX  Override this to modify the data structure

        This allows to insert the data in a wrapping structure.

            Example: ::

                @staticmethod
                def _prepare_response_content(data):
                    if data is not None:
                        return {"data": data}
                    return None
        rC   )datas    r*   r   z'ResponseMixin._prepare_response_content   s	     r,   c                H   |j                  di       }|j                  r;|j                  di       j                  dg       j                  |j                         |rE|j                  j
                  dk  rt               }|d   j                         D ]T  }|D ]M  }t        |t        j                        r|d   xs |j                  }	n|j                  }	|j                  |	       O V ||j                  hk7  rt        |      |d<   |d   j                         D ]x  }
i }t        |d   |
         D ]X  }t        |t        j                        s|} n:|j!                  d      xs |j                  }	t#        |||	       t%        ||      }Z ||d   |
<   z t%        | |      } | S )Nr.   r/   default   r   produces)getDEFAULT_ERROR_RESPONSE_NAMEr3   r4   openapi_versionmajorsetvaluesr   r   MappingDEFAULT_RESPONSE_CONTENT_TYPEaddlistkeysreversedpopr   r	   )docdoc_infoapispecr"   	operationcontent_typesr/   r.   r   r    r'   s               r*   _prepare_response_docz#ResponseMixin._prepare_response_doc   s   LLR0	**$$["5Ir*778 ##))A- #!*;!7!>!>!@ 	8I$- 8%h< ( 8 !E#&#D#D )
 ,/+L+LL%)),78	8 !S%F%F$GG,0,?Ij)  )5::< ; !);)?)L M 	6H%h<' ^4 =<< ! %Xt\B%dH5D	6 7;	+&{3; S),C
r,   rB   )
__name__
__module____qualname____doc__r.   rD   staticmethodr8   r   r_   rC   r,   r*   r   r      s    3
 l
 lb R
 Rh  &   3 3r,   r   )rc   r9   collectionsr   copyr   	functoolsr   flaskr   werkzeugr   utilsr	   r
   r   r   r   r   r   r   rC   r,   r*   <module>rk      s5            ] ]r,   