How to use the sanic.views.HTTPMethodView function in sanic

To help you get started, we’ve selected a few sanic examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github huge-success / sanic / tests / test_views.py View on Github external
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"})
github ahopkins / sanic-jwt / tests / test_endpoints_cbv.py View on Github external
sanic_jwt = Initialize(sanic_app, authenticate=authenticate)


class PublicView(HTTPMethodView):
    def get(self, request):
        return json({"hello": "world"})


class ProtectedView(HTTPMethodView):
    decorators = [protected()]

    async def get(self, request):
        return json({"protected": True})


class PartiallyProtectedView(HTTPMethodView):
    async def get(self, request):
        return json({"protected": True})

    @protected()
    async def patch(self, request):
        return json({"protected": True})


sanic_app.add_route(PublicView.as_view(), "/")
sanic_app.add_route(ProtectedView.as_view(), "/protected")
sanic_app.add_route(PartiallyProtectedView.as_view(), "/partially")


class TestEndpointsCBV(object):
    def test_unprotected(self):
        _, response = sanic_app.test_client.get("/")
github icon-project / loopchain / loopchain / rest_server / rest_server.py View on Github external
logging.warning("your data is not json, your data(" + str(grpc_response.data) + ")")
                    verify_result['response_code'] = str(message_code.Response.fail.value)
            else:
                verify_result['response_code'] = str(message_code.Response.fail.value)
        else:
            verify_result['response_code'] = str(message_code.Response.fail_validate_params.value)
            verify_result['message'] = "Invalid transaction hash."
        return response.json(verify_result)


class Status(HTTPMethodView):
    async def get(self, request):
        return response.json(PeerServiceStub().get_status(get_channel_name_from_args(request.raw_args)))


class Avail(HTTPMethodView):
    async def get(self, request):
        status = HTTPStatus.OK
        result = PeerServiceStub().get_status(
            get_channel_name_from_args(request.raw_args)
        )

        # parse result and set HTTPStatus error while service is not avail.
        # util.logger.spam(f"result({result['status']})")
        if result['status'] != "Service is online: 0":
            status = HTTPStatus.SERVICE_UNAVAILABLE

        return response.json(
            result,
            status=status
        )
github ahopkins / sanic-jwt / example / cbv.py View on Github external
user = username_table.get(username, None)
    if user is None:
        raise exceptions.AuthenticationFailed("User not found.")

    if password != user.password:
        raise exceptions.AuthenticationFailed("Password is incorrect.")

    return user


app = Sanic()
initialize(app, authenticate=authenticate)


class PublicView(HTTPMethodView):
    def get(self, request):
        return json({"hello": "world"})


class ProtectedView(HTTPMethodView):
    decorators = [protected()]

    async def get(self, request):
        return json({"protected": True})


app.add_route(PublicView.as_view(), "/")
app.add_route(ProtectedView.as_view(), "/protected")


if __name__ == "__main__":
github graphql-python / sanic-graphql / sanic_graphql / graphqlview.py View on Github external
from promise import Promise
from sanic.response import HTTPResponse
from sanic.views import HTTPMethodView

from graphql.type.schema import GraphQLSchema
from graphql.execution.executors.asyncio import AsyncioExecutor
from graphql_server import (HttpQueryError, default_format_error,
                            encode_execution_results, json_encode,
                            load_json_body, run_http_query)

from .render_graphiql import render_graphiql


class GraphQLView(HTTPMethodView):
    schema = None
    executor = None
    root_value = None
    context = None
    pretty = False
    graphiql = False
    graphiql_version = None
    graphiql_template = None
    middleware = None
    batch = False
    jinja_env = None
    max_age = 86400

    _enable_async = True

    methods = ['GET', 'POST', 'PUT', 'DELETE']