How to use sanic - 10 common examples

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 bohea / sanic-limiter / test / test_extension.py View on Github external
def test_bp_combined_limit(self):
        app, limiter = self.build_app(config={}, global_limits=['1/day'])
        bp = Blueprint('/bp')
        limiter.limit('1 per hour')(bp)

        @bp.route("/bp1")
        @limiter.limit('2 per hour')
        async def bp_t1(request):
            return text("bp_t1")

        app.blueprint(bp)

        cli = app.test_client
        self.assertEqual(200, cli.get("/bp1")[1].status)
        self.assertEqual(200, cli.get("/bp1")[1].status)
        self.assertEqual(429, cli.get("/bp1")[1].status)
github bohea / sanic-limiter / test / test_demo.py View on Github external
# demo start
from sanic import Sanic, Blueprint
from sanic.response import text

from sanic_limiter import Limiter, get_remote_address

app = Sanic(__name__)
limiter = Limiter(app, global_limits=['1 per hour', '10 per day'], key_func=get_remote_address)
bp = Blueprint('some_bp')
limiter.limit("2 per hour")(bp)


@bp.route("/bp1")
async def bp_t1(request):
    return text("bp_t1")


@app.route("/t1")
@limiter.limit("100 per hour;10/minute")
async def t1(request):
    return text("t1")


@app.route("/t2")
async def t2(request):
github channelcat / sanic-speedtest / main.py View on Github external
async def test(request, commit):
	global RUNNING
	if RUNNING:
		raise InvalidUsage('Please wait for the current test to finish')
	try:
		RUNNING = True
		run_data = await git.test_commit(commit)
	finally:
		RUNNING = False

	return response.json(run_data)
github huge-success / sanic / tests / test_routes.py View on Github external
async def handler(request):
        return text("OK")
github huge-success / sanic / tests / test_request_stream.py View on Github external
async def post(request, id):
        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)
github vltr / sanic-boom / tests / test_boom_app.py View on Github external
async def handler(request):
        return text("OK")
github vltr / sanic-boom / tests / test_router.py View on Github external
async def foo_root_handler(request):
        return text("OK")  # noqa
github huge-success / sanic / tests / test_cookies.py View on Github external
def handler(request):
        response = text("pass")
        response.cookies["test"] = "pass"
        response.cookies["test"]["expires"] = expires
        return response
github huge-success / sanic / tests / test_blueprints.py View on Github external
def get_resource_hander(request, resource_id):
        resource = {"resource_id": resource_id}
        return json(resource)
github ashleysommer / sanic-cors / tests / decorator / test_methods.py View on Github external
def setUp(self):
        self.app = Sanic(__name__)

        @self.app.route('/defaults', methods=['GET', 'POST', 'HEAD', 'OPTIONS'])
        @cross_origin(self.app)
        def defaults(request):
            return text('Should only return headers on pre-flight OPTIONS request')

        @self.app.route('/test_methods_defined', methods=['POST', 'OPTIONS'])
        @cross_origin(self.app, methods=['POST'])
        def test_get(request):
            return text('Only allow POST')