How to use the sanic.blueprints.Blueprint 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_blueprints.py View on Github external
def test_bp_listeners(app):
    blueprint = Blueprint("test_middleware")

    order = []

    @blueprint.listener("before_server_start")
    def handler_1(sanic, loop):
        order.append(1)

    @blueprint.listener("after_server_start")
    def handler_2(sanic, loop):
        order.append(2)

    @blueprint.listener("after_server_start")
    def handler_3(sanic, loop):
        order.append(3)

    @blueprint.listener("before_server_stop")
github huge-success / sanic / tests / test_blueprints.py View on Github external
def test_bp_strict_slash_default_value(app):
    bp = Blueprint("test_text", strict_slashes=True)

    @bp.get("/get")
    def get_handler(request):
        return text("OK")

    @bp.post("/post/")
    def post_handler(request):
        return text("OK")

    app.blueprint(bp)

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

    request, response = app.test_client.post("/post")
    assert response.status == 404
github huge-success / sanic / tests / test_blueprints.py View on Github external
def one(request):
        return text("one")

    @bp.get("/second")
    def second(request):
        return text("second")

    app.blueprint(bp)

    assert app.test_client.get("/one")[1].status == 200
    assert app.test_client.get("/one/")[1].status == 200

    assert app.test_client.get("/second")[1].status == 200
    assert app.test_client.get("/second/")[1].status == 404

    bp2 = Blueprint("bp2", strict_slashes=False)

    @bp2.get("/third")
    def third(request):
        return text("third")

    app.blueprint(bp2)
    assert app.test_client.get("/third")[1].status == 200
    assert app.test_client.get("/third/")[1].status == 200

    @app.get("/f1", strict_slashes=False)
    def f1(request):
        return text("f1")

    assert app.test_client.get("/f1")[1].status == 200
    assert app.test_client.get("/f1/")[1].status == 200
github huge-success / sanic / tests / test_blueprint_group.py View on Github external
def test_bp_group_indexing(app: Sanic):
    blueprint_1 = Blueprint("blueprint_1", url_prefix="/bp1")
    blueprint_2 = Blueprint("blueprint_2", url_prefix="/bp2")

    group = Blueprint.group(blueprint_1, blueprint_2)
    assert group[0] == blueprint_1

    with raises(expected_exception=IndexError) as e:
        _ = group[3]
github huge-success / sanic / tests / test_url_for_static.py View on Github external
'/testing2.file', get_file_path(static_file_directory, file_name),
        name='testing_file')

    uri = app.url_for('static')
    uri2 = app.url_for('static', filename='any')
    uri3 = app.url_for('static', name='static', filename='any')

    assert uri == '/testing.file'
    assert uri == uri2
    assert uri2 == uri3

    request, response = app.test_client.get(uri)
    assert response.status == 200
    assert response.body == get_file_content(static_file_directory, file_name)

    bp = Blueprint('test_bp_static', url_prefix='/bp')

    bp.static('/testing.file', get_file_path(static_file_directory, file_name))
    bp.static('/testing2.file',
              get_file_path(static_file_directory, file_name),
              name='testing_file')

    app.blueprint(bp)

    uri = app.url_for('static', name='test_bp_static.static')
    uri2 = app.url_for('static', name='test_bp_static.static', filename='any')
    uri3 = app.url_for('test_bp_static.static')
    uri4 = app.url_for('test_bp_static.static', name='any')
    uri5 = app.url_for('test_bp_static.static', filename='any')
    uri6 = app.url_for('test_bp_static.static', name='any', filename='any')

    assert uri == '/bp/testing.file'
github huge-success / sanic / tests / test_views.py View on Github external
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"
github huge-success / sanic / tests / test_blueprints.py View on Github external
def test_bp_static_content_type(app, file_name):
    # This is done here, since no other test loads a file here
    current_file = inspect.getfile(inspect.currentframe())
    current_directory = os.path.dirname(os.path.abspath(current_file))
    static_directory = os.path.join(current_directory, "static")

    blueprint = Blueprint("test_static")
    blueprint.static(
        "/testing.file",
        get_file_path(static_directory, file_name),
        content_type="text/html; charset=utf-8",
    )

    app.blueprint(blueprint)

    request, response = app.test_client.get("/testing.file")
    assert response.status == 200
    assert response.body == get_file_content(static_directory, file_name)
    assert response.headers["Content-Type"] == "text/html; charset=utf-8"
github huge-success / sanic / tests / test_blueprints.py View on Github external
    @bp_resources.get("/")
    def list_resources_handler(request):
        resource = {}
        return json([resource])

    bp_resource = Blueprint("bp_resource", url_prefix="/")

    @bp_resource.get("/")
    def get_resource_hander(request, resource_id):
        resource = {"resource_id": resource_id}
        return json(resource)

    bp_resources_group = Blueprint.group(
        bp_resources, bp_resource, url_prefix="/resources"
    )
    bp_api_v1 = Blueprint("bp_api_v1")

    @bp_api_v1.get("/info")
    def api_v1_info(request):
        return text("api_version: v1")

    bp_api_v1_group = Blueprint.group(
        bp_api_v1, bp_resources_group, url_prefix="/v1"
    )
    bp_api_group = Blueprint.group(bp_api_v1_group, url_prefix="/api")
    app.blueprint(bp_api_group)

    request, response = app.test_client.get("/api/v1/info")
    assert response.text == "api_version: v1"

    request, response = app.test_client.get("/api/v1/resources")
    assert response.json == [{}]
github huge-success / sanic / tests / test_blueprints.py View on Github external
def test_versioned_routes_get(app, method):
    bp = Blueprint("test_text")

    method = method.lower()

    func = getattr(bp, method)
    if callable(func):

        @func("/{}".format(method), version=1)
        def handler(request):
            return text("OK")

    else:
        print(func)
        raise Exception("{} is not callable".format(func))

    app.blueprint(bp)
github PWZER / swagger-ui-py / swagger_ui / core.py View on Github external
def _sanic_handler(self):
        from sanic import response
        from sanic.blueprints import Blueprint

        swagger_blueprint = Blueprint('swagger_blueprint', url_prefix=self._url_prefix)

        @swagger_blueprint.get('/')
        async def swagger_blueprint_doc_handler(request):
            return response.html(self.doc_html)

        if self._editor:
            @swagger_blueprint.get('/editor')
            async def swagger_blueprint_editor_handler(request):
                return response.html(self.editor_html)

        @swagger_blueprint.get('/swagger.json')
        async def swagger_blueprint_config_handler(request):
            return response.json(self.get_config(request.host))

        swagger_blueprint.static('/', str(self.static_dir))
        self._app.blueprint(swagger_blueprint)