o
    NK&h{!                     @  s   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 d dlmZ er4d dlmZ d dlmZ G dd	 d	Zd
d ZdS )    )annotations)Iterable)TYPE_CHECKINGAnyCallableOptionalUnion)RouteHandler)Request)Sanic)	Blueprintc                	      s   e Zd ZU dZg Zded< ddedhddddddf	d, fddZd-d!d"Ze	d.d&d'Z
e	edhddddddfd/d*d+Z  ZS )0HTTPMethodViewa  Class based implementation for creating and grouping handlers

    Class-based views (CBVs) are an alternative to function-based views. They
    allow you to reuse common logic, and group related views, while keeping
    the flexibility of function-based views.


    To use a class-based view, subclass the method handler, and implement
    methods (`get`, `post`, `put`, `patch`, `delete`) for the class
    to correspond to each HTTP method you want to support.

    For example:

    ```python
    class DummyView(HTTPMethodView):
        def get(self, request: Request):
            return text('I am get method')

        def put(self, request: Request):
            return text('I am put method')
    ```

    If someone tries to use a non-implemented method, they will reveive a
    405 response.

    If you need any url params just include them in method signature, like
    you would for function-based views.

    ```python
    class DummyView(HTTPMethodView):
        def get(self, request: Request, my_param_here: str):
            return text(f"I am get method with {my_param_here}")
    ```

    Next, you need to attach the view to the app or blueprint. You can do this
    in the exact same way as you would for a function-based view, except you
    should you use `MyView.as_view()` instead of `my_view_handler`.

    ```python
    app.add_route(DummyView.as_view(), "/<my_param_here>")
    ```

    Alternatively, you can use the `attach` method:

    ```python
    DummyView.attach(app, "/<my_param_here>")
    ```

    Or, at the time of subclassing:

    ```python
    class DummyView(HTTPMethodView, attach=app, uri="/<my_param_here>"):
        ...
    ```

    To add a decorator, you can either:

    1. Add it to the `decorators` list on the class, which will apply it to
         all methods on the class; or
    2. Add it to the method directly, which will only apply it to that method.

    ```python
    class DummyView(HTTPMethodView):
        decorators = [my_decorator]
        ...

    # or

    class DummyView(HTTPMethodView):
        @my_decorator
        def get(self, request: Request):
            ...
    ```

    One catch is that you need to be mindful that the call inside the decorator
    may need to account for the `self` argument, which is passed to the method
    as the first argument. Alternatively, you may want to also mark your method
    as `staticmethod` to avoid this.

    Available attributes at the time of subclassing:
    - **attach** (Optional[Union[Sanic, Blueprint]]): The app or blueprint to
        attach the view to.
    - **uri** (str): The uri to attach the view to.
    - **methods** (Iterable[str]): The HTTP methods to attach the view to.
        Defaults to `{"GET"}`.
    - **host** (Optional[str]): The host to attach the view to.
    - **strict_slashes** (Optional[bool]): Whether to add a redirect rule for
        the uri with a trailing slash.
    - **version** (Optional[int]): The version to attach the view to.
    - **name** (Optional[str]): The name to attach the view to.
    - **stream** (bool): Whether the view is a stream handler.
    - **version_prefix** (str): The prefix to use for the version. Defaults
        to `"/v"`.
    z8list[Callable[[Callable[..., Any]], Callable[..., Any]]]
decoratorsN GETFz/vattach!Optional[Union[Sanic, Blueprint]]uristrmethodsIterable[str]hostOptional[str]strict_slashesOptional[bool]versionOptional[int]namestreamboolversion_prefixkwargsr   returnNonec
                   s:   t  jdi |
 |r| j|||||||||	d	 d S d S )Nr   r   r   r   r   r   r   r     )super__init_subclass__r   )clsr   r   r   r   r   r   r   r   r    r!   	__class__r%   >/var/www/html/venv/lib/python3.10/site-packages/sanic/views.pyr'   w   s   
z HTTPMethodView.__init_subclass__requestr
   c                 O  sV   |j  }t| |d}|s|dkrt| d}|s t|j  d||g|R i |S )z/Dispatch request to appropriate handler method.Nheadgetz$ is not supported for this endpoint.)methodlowergetattrNotImplementedError)selfr,   argsr!   r/   handlerr%   r%   r+   dispatch_request   s   


zHTTPMethodView.dispatch_request
class_argsclass_kwargsr	   c                   sT    fdd| j r| j_| j D ]}|q| _| j_| j_| j_S )a~  Return view function for use with the routing system, that dispatches request to appropriate handler method.

        If you need to pass arguments to the class's constructor, you can
        pass the arguments to `as_view` and they will be passed to the class
        `__init__` method.

        Args:
            *class_args: Variable length argument list for the class instantiation.
            **class_kwargs: Arbitrary keyword arguments for the class instantiation.

        Returns:
            RouteHandler: The view function.

        Examples:
            ```python
            class DummyView(HTTPMethodView):
                def __init__(self, foo: MyFoo):
                    self.foo = foo

                async def get(self, request: Request):
                    return text(self.foo.bar)

            app.add_route(DummyView.as_view(foo=MyFoo()), "/")
            ```
        c                    s    j  i }|j| i |S )N)
view_classr6   )r4   r!   r3   r7   r8   viewr%   r+   r;      s   z$HTTPMethodView.as_view.<locals>.view)r   
__module__r9   __doc____name__)r(   r7   r8   	decoratorr%   r:   r+   as_view   s   

zHTTPMethodView.as_viewtoUnion[Sanic, Blueprint]c
           
      C  s$   |j |  ||||||||	d	 dS )a,  Attaches the view to a Sanic app or Blueprint at the specified URI.

        Args:
            cls: The class that this method is part of.
            to (Union[Sanic, Blueprint]): The Sanic application or Blueprint to attach to.
            uri (str): The URI to bind the view to.
            methods (Iterable[str], optional): A collection of HTTP methods that the view should respond to. Defaults to `frozenset({"GET"})`.
            host (Optional[str], optional): A specific host or hosts to bind the view to. Defaults to `None`.
            strict_slashes (Optional[bool], optional): Enforce or not the trailing slash. Defaults to `None`.
            version (Optional[int], optional): Version of the API if versioning is used. Defaults to `None`.
            name (Optional[str], optional): Unique name for the route. Defaults to `None`.
            stream (bool, optional): Enable or disable streaming for the view. Defaults to `False`.
            version_prefix (str, optional): The prefix for the version, if versioning is used. Defaults to `"/v"`.
        r$   N)	add_router@   )
r(   rA   r   r   r   r   r   r   r   r    r%   r%   r+   r      s   
zHTTPMethodView.attach)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r!   r   r"   r#   )r,   r
   )r7   r   r8   r   r"   r	   )rA   rB   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r"   r#   )r>   r<   __qualname__r=   r   __annotations__	frozensetr'   r6   classmethodr@   r   __classcell__r%   r%   r)   r+   r      s2   
 _
*r   c                 C  s
   d| _ | S )z1Decorator to mark a function as a stream handler.T)	is_stream)funcr%   r%   r+   r      s   r   N)
__future__r   collections.abcr   typingr   r   r   r   r   sanic.models.handler_typesr	   sanic.request.typesr
   sanicr   sanic.blueprintsr   r   r   r%   r%   r%   r+   <module>   s     `