當前位置: 首頁>>代碼示例>>Python>>正文


Python views.HTTPMethodView方法代碼示例

本文整理匯總了Python中sanic.views.HTTPMethodView方法的典型用法代碼示例。如果您正苦於以下問題:Python views.HTTPMethodView方法的具體用法?Python views.HTTPMethodView怎麽用?Python views.HTTPMethodView使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在sanic.views的用法示例。


在下文中一共展示了views.HTTPMethodView方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_with_middleware_response

# 需要導入模塊: from sanic import views [as 別名]
# 或者: from sanic.views import HTTPMethodView [as 別名]
def test_with_middleware_response(app):
    results = []

    @app.middleware("request")
    async def process_request(request):
        results.append(request)

    @app.middleware("response")
    async def process_response(request, response):
        results.append(request)
        results.append(response)

    class DummyView(HTTPMethodView):
        def get(self, request):
            return text("I am get method")

    app.add_route(DummyView.as_view(), "/")

    request, response = app.test_client.get("/")

    assert response.text == "I am get method"
    assert type(results[0]) is Request
    assert type(results[1]) is Request
    assert isinstance(results[2], HTTPResponse) 
開發者ID:huge-success,項目名稱:sanic,代碼行數:26,代碼來源:test_views.py

示例2: test_with_custom_class_methods

# 需要導入模塊: from sanic import views [as 別名]
# 或者: from sanic.views import HTTPMethodView [as 別名]
def test_with_custom_class_methods(app):
    class DummyView(HTTPMethodView):
        global_var = 0

        def _iternal_method(self):
            self.global_var += 10

        def get(self, request):
            self._iternal_method()
            return text(
                f"I am get method and global var " f"is {self.global_var}"
            )

    app.add_route(DummyView.as_view(), "/")
    request, response = app.test_client.get("/")
    assert response.text == "I am get method and global var is 10" 
開發者ID:huge-success,項目名稱:sanic,代碼行數:18,代碼來源:test_views.py

示例3: test_with_decorator

# 需要導入模塊: from sanic import views [as 別名]
# 或者: from sanic.views import HTTPMethodView [as 別名]
def test_with_decorator(app):
    results = []

    def stupid_decorator(view):
        def decorator(*args, **kwargs):
            results.append(1)
            return view(*args, **kwargs)

        return decorator

    class DummyView(HTTPMethodView):
        decorators = [stupid_decorator]

        def get(self, request):
            return text("I am get method")

    app.add_route(DummyView.as_view(), "/")
    request, response = app.test_client.get("/")
    assert response.text == "I am get method"
    assert results[0] == 1 
開發者ID:huge-success,項目名稱:sanic,代碼行數:22,代碼來源:test_views.py

示例4: protected

# 需要導入模塊: from sanic import views [as 別名]
# 或者: from sanic.views import HTTPMethodView [as 別名]
def protected(initialized_on=None, **kw):
    def decorator(f):
        @wraps(f)
        async def decorated_function(request, *args, **kwargs):
            if issubclass(request.__class__, HTTPMethodView):
                request = args[0]
            kwargs.update(
                {
                    "initialized_on": initialized_on,
                    "kw": kw,
                    "request": request,
                    "f": f,
                }
            )
            return await _do_protection(*args, **kwargs)

        return decorated_function

    return decorator 
開發者ID:ahopkins,項目名稱:sanic-jwt,代碼行數:21,代碼來源:decorators.py

示例5: test_methods

# 需要導入模塊: from sanic import views [as 別名]
# 或者: from sanic.views import HTTPMethodView [as 別名]
def test_methods(app, method):
    class DummyView(HTTPMethodView):
        async def get(self, request):
            assert request.stream is None
            return text("", headers={"method": "GET"})

        def post(self, request):
            return text("", headers={"method": "POST"})

        async def put(self, request):
            return text("", headers={"method": "PUT"})

        def head(self, request):
            return text("", headers={"method": "HEAD"})

        def options(self, request):
            return text("", headers={"method": "OPTIONS"})

        async def patch(self, request):
            return text("", headers={"method": "PATCH"})

        def delete(self, request):
            return text("", headers={"method": "DELETE"})

    app.add_route(DummyView.as_view(), "/")
    assert app.is_request_stream is False

    request, response = getattr(app.test_client, method.lower())("/")
    assert response.headers["method"] == method 
開發者ID:huge-success,項目名稱:sanic,代碼行數:31,代碼來源:test_views.py

示例6: test_unexisting_methods

# 需要導入模塊: from sanic import views [as 別名]
# 或者: from sanic.views import HTTPMethodView [as 別名]
def test_unexisting_methods(app):
    class DummyView(HTTPMethodView):
        def get(self, request):
            return text("I am get method")

    app.add_route(DummyView.as_view(), "/")
    request, response = app.test_client.get("/")
    assert response.text == "I am get method"
    request, response = app.test_client.post("/")
    assert "Method POST not allowed for URL /" in response.text 
開發者ID:huge-success,項目名稱:sanic,代碼行數:12,代碼來源:test_views.py

示例7: test_argument_methods

# 需要導入模塊: from sanic import views [as 別名]
# 或者: from sanic.views import HTTPMethodView [as 別名]
def test_argument_methods(app):
    class DummyView(HTTPMethodView):
        def get(self, request, my_param_here):
            return text("I am get method with %s" % my_param_here)

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

    request, response = app.test_client.get("/test123")

    assert response.text == "I am get method with test123" 
開發者ID:huge-success,項目名稱:sanic,代碼行數:12,代碼來源:test_views.py

示例8: test_with_bp

# 需要導入模塊: from sanic import views [as 別名]
# 或者: from sanic.views import HTTPMethodView [as 別名]
def test_with_bp(app):
    bp = Blueprint("test_text")

    class DummyView(HTTPMethodView):
        def get(self, request):
            assert request.stream is None
            return text("I am get method")

    bp.add_route(DummyView.as_view(), "/")

    app.blueprint(bp)
    request, response = app.test_client.get("/")

    assert app.is_request_stream is False
    assert response.text == "I am get method" 
開發者ID:huge-success,項目名稱:sanic,代碼行數:17,代碼來源:test_views.py

示例9: test_with_bp_with_url_prefix

# 需要導入模塊: from sanic import views [as 別名]
# 或者: from sanic.views import HTTPMethodView [as 別名]
def test_with_bp_with_url_prefix(app):
    bp = Blueprint("test_text", url_prefix="/test1")

    class DummyView(HTTPMethodView):
        def get(self, request):
            return text("I am get method")

    bp.add_route(DummyView.as_view(), "/")

    app.blueprint(bp)
    request, response = app.test_client.get("/test1/")

    assert response.text == "I am get method" 
開發者ID:huge-success,項目名稱:sanic,代碼行數:15,代碼來源:test_views.py

示例10: test_request_stream_method_view

# 需要導入模塊: from sanic import views [as 別名]
# 或者: from sanic.views import HTTPMethodView [as 別名]
def test_request_stream_method_view(app):
    """for self.is_request_stream = True"""

    class SimpleView(HTTPMethodView):
        def get(self, request):
            assert request.stream is None
            return text("OK")

        @stream_decorator
        async def post(self, request):
            assert isinstance(request.stream, StreamBuffer)
            result = ""
            while True:
                body = await request.stream.read()
                if body is None:
                    break
                result += body.decode("utf-8")
            return text(result)

    app.add_route(SimpleView.as_view(), "/method_view")

    assert app.is_request_stream is True

    request, response = app.test_client.get("/method_view")
    assert response.status == 200
    assert response.text == "OK"

    request, response = app.test_client.post("/method_view", data=data)
    assert response.status == 200
    assert response.text == data 
開發者ID:huge-success,項目名稱:sanic,代碼行數:32,代碼來源:test_request_stream.py

示例11: test_request_stream_100_continue

# 需要導入模塊: from sanic import views [as 別名]
# 或者: from sanic.views import HTTPMethodView [as 別名]
def test_request_stream_100_continue(app, headers, expect_raise_exception):
    class SimpleView(HTTPMethodView):
        @stream_decorator
        async def post(self, request):
            assert isinstance(request.stream, StreamBuffer)
            result = ""
            while True:
                body = await request.stream.read()
                if body is None:
                    break
                result += body.decode("utf-8")
            return text(result)

    app.add_route(SimpleView.as_view(), "/method_view")

    assert app.is_request_stream is True

    if not expect_raise_exception:
        request, response = app.test_client.post(
            "/method_view", data=data, headers={"EXPECT": "100-continue"}
        )
        assert response.status == 200
        assert response.text == data
    else:
        with pytest.raises(ValueError) as e:
            app.test_client.post(
                "/method_view",
                data=data,
                headers={"EXPECT": "100-continue-extra"},
            )
            assert "Unknown Expect: 100-continue-extra" in str(e) 
開發者ID:huge-success,項目名稱:sanic,代碼行數:33,代碼來源:test_request_stream.py

示例12: test_initialize_with_custom_endpoint_not_subclassed

# 需要導入模塊: from sanic import views [as 別名]
# 或者: from sanic.views import HTTPMethodView [as 別名]
def test_initialize_with_custom_endpoint_not_subclassed():
    class SubclassHTTPMethodView(HTTPMethodView):
        async def options(self, request):
            return text("", status=204)

        async def get(self, request):
            return text("ok")

    app = Sanic("sanic-jwt-test")
    with pytest.raises(exceptions.InvalidClassViewsFormat):
        Initialize(
            app,
            authenticate=lambda: True,
            class_views=[("/subclass", SubclassHTTPMethodView)],
        ) 
開發者ID:ahopkins,項目名稱:sanic-jwt,代碼行數:17,代碼來源:test_initialize.py

示例13: inject_user

# 需要導入模塊: from sanic import views [as 別名]
# 或者: from sanic.views import HTTPMethodView [as 別名]
def inject_user(initialized_on=None, **kw):
    def decorator(f):
        @wraps(f)
        async def decorated_function(request, *args, **kwargs):
            if issubclass(request.__class__, HTTPMethodView):
                request = args[0]

            if initialized_on and isinstance(
                initialized_on, Blueprint
            ):  # noqa
                instance = initialized_on
            else:
                instance = request.app

            with instant_config(instance, request=request, **kw):
                if request.method == "OPTIONS":
                    return await utils.call(
                        f, request, *args, **kwargs
                    )  # noqa

                payload = await instance.auth.extract_payload(request, verify=False)
                user = await utils.call(
                    instance.auth.retrieve_user, request, payload
                )
                response = f(request, user=user, *args, **kwargs)
                return await response

        return decorated_function

    return decorator 
開發者ID:ahopkins,項目名稱:sanic-jwt,代碼行數:32,代碼來源:decorators.py

示例14: test_methods

# 需要導入模塊: from sanic import views [as 別名]
# 或者: from sanic.views import HTTPMethodView [as 別名]
def test_methods():
    app = Sanic('test_methods')

    class DummyView(HTTPMethodView):

        def get(self, request):
            return text('I am get method')

        def post(self, request):
            return text('I am post method')

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

        def patch(self, request):
            return text('I am patch method')

        def delete(self, request):
            return text('I am delete method')

    app.add_route(DummyView(), '/')

    request, response = sanic_endpoint_test(app, method="get")
    assert response.text == 'I am get method'
    request, response = sanic_endpoint_test(app, method="post")
    assert response.text == 'I am post method'
    request, response = sanic_endpoint_test(app, method="put")
    assert response.text == 'I am put method'
    request, response = sanic_endpoint_test(app, method="patch")
    assert response.text == 'I am patch method'
    request, response = sanic_endpoint_test(app, method="delete")
    assert response.text == 'I am delete method' 
開發者ID:hhstore,項目名稱:annotated-py-projects,代碼行數:34,代碼來源:test_views.py

示例15: test_unexisting_methods

# 需要導入模塊: from sanic import views [as 別名]
# 或者: from sanic.views import HTTPMethodView [as 別名]
def test_unexisting_methods():
    app = Sanic('test_unexisting_methods')

    class DummyView(HTTPMethodView):

        def get(self, request):
            return text('I am get method')

    app.add_route(DummyView(), '/')
    request, response = sanic_endpoint_test(app, method="get")
    assert response.text == 'I am get method'
    request, response = sanic_endpoint_test(app, method="post")
    assert response.text == 'Error: Method POST not allowed for URL /' 
開發者ID:hhstore,項目名稱:annotated-py-projects,代碼行數:15,代碼來源:test_views.py


注:本文中的sanic.views.HTTPMethodView方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。