def test_bp_group_as_nested_group(): blueprint_1 = Blueprint("blueprint_1", url_prefix="/bp1") blueprint_2 = Blueprint("blueprint_2", url_prefix="/bp2") blueprint_group_1 = Blueprint.group( Blueprint.group(blueprint_1, blueprint_2) ) assert len(blueprint_group_1) == 2
def test_bp_group_properties(): blueprint_1 = Blueprint("blueprint_1", url_prefix="/bp1") blueprint_2 = Blueprint("blueprint_2", url_prefix="/bp2") group = Blueprint.group( blueprint_1, blueprint_2, version=1, version_prefix="/api/v", url_prefix="/grouped", strict_slashes=True, ) primary = Blueprint.group(group, url_prefix="/primary") @blueprint_1.route("/") def blueprint_1_default_route(request): return text("BP1_OK") @blueprint_2.route("/") def blueprint_2_default_route(request): return text("BP2_OK") app = Sanic("PropTest") app.blueprint(group) app.blueprint(primary) app.router.finalize() routes = [route.path for route in app.router.routes] assert len(routes) == 4 assert "api/v1/grouped/bp1/" in routes assert "api/v1/grouped/bp2/" in routes assert "api/v1/primary/grouped/bp1" in routes assert "api/v1/primary/grouped/bp2" in routes
def test_bp_group(app: Sanic): blueprint_1 = Blueprint("blueprint_1", url_prefix="/bp1") blueprint_2 = Blueprint("blueprint_2", url_prefix="/bp2") @blueprint_1.route("/") def blueprint_1_default_route(request): return text("BP1_OK") @blueprint_2.route("/") def blueprint_2_default_route(request): return text("BP2_OK") blueprint_group_1 = Blueprint.group( blueprint_1, blueprint_2, url_prefix="/bp" ) blueprint_3 = Blueprint("blueprint_3", url_prefix="/bp3") @blueprint_group_1.middleware("request") def blueprint_group_1_middleware(request): global MIDDLEWARE_INVOKE_COUNTER MIDDLEWARE_INVOKE_COUNTER["request"] += 1 @blueprint_3.route("/") def blueprint_3_default_route(request): return text("BP3_OK") blueprint_group_2 = Blueprint.group( blueprint_group_1, blueprint_3, url_prefix="/api" ) @blueprint_group_2.middleware("response") def blueprint_group_2_middleware(request, response): global MIDDLEWARE_INVOKE_COUNTER MIDDLEWARE_INVOKE_COUNTER["response"] += 1 app.blueprint(blueprint_group_2) @app.route("/") def app_default_route(request): return text("APP_OK") _, response = app.test_client.get("/") assert response.text == "APP_OK" _, response = app.test_client.get("/api/bp/bp1") assert response.text == "BP1_OK" _, response = app.test_client.get("/api/bp/bp2") assert response.text == "BP2_OK" _, response = app.test_client.get("/api/bp3") assert response.text == "BP3_OK" assert MIDDLEWARE_INVOKE_COUNTER["response"] == 4 assert MIDDLEWARE_INVOKE_COUNTER["request"] == 4
def test_bp_group(app: Sanic): blueprint_1 = Blueprint("blueprint_1", url_prefix="/bp1") blueprint_2 = Blueprint("blueprint_2", url_prefix="/bp2") @blueprint_1.route("/") def blueprint_1_default_route(request): return text("BP1_OK") @blueprint_2.route("/") def blueprint_2_default_route(request): return text("BP2_OK") blueprint_group_1 = Blueprint.group(blueprint_1, blueprint_2, url_prefix="/bp") blueprint_3 = Blueprint("blueprint_3", url_prefix="/bp3") @blueprint_group_1.middleware("request") def blueprint_group_1_middleware(request): global MIDDLEWARE_INVOKE_COUNTER MIDDLEWARE_INVOKE_COUNTER["request"] += 1 @blueprint_3.route("/") def blueprint_3_default_route(request): return text("BP3_OK") blueprint_group_2 = Blueprint.group(blueprint_group_1, blueprint_3, url_prefix="/api") @blueprint_group_2.middleware("response") def blueprint_group_2_middleware(request, response): global MIDDLEWARE_INVOKE_COUNTER MIDDLEWARE_INVOKE_COUNTER["response"] += 1 app.blueprint(blueprint_group_2) @app.route("/") def app_default_route(request): return text("APP_OK") _, response = app.test_client.get("/") assert response.text == "APP_OK" _, response = app.test_client.get("/api/bp/bp1") assert response.text == "BP1_OK" _, response = app.test_client.get("/api/bp/bp2") assert response.text == "BP2_OK" _, response = app.test_client.get("/api/bp3") assert response.text == "BP3_OK" assert MIDDLEWARE_INVOKE_COUNTER["response"] == 3 assert MIDDLEWARE_INVOKE_COUNTER["request"] == 2
def test_blueprint_group_strict_slashes(): app = Sanic(name="blueprint-group-test") bp1 = Blueprint(name="bp1", url_prefix=None, strict_slashes=False) bp2 = Blueprint(name="bp2", version=3, url_prefix="/bp2", strict_slashes=None) bp3 = Blueprint(name="bp3", version=None, url_prefix="/bp3/", strict_slashes=None) @bp1.get("/r1") async def bp1_r1(request): return json({"from": "bp1/r1"}) @bp2.get("/r1") async def bp2_r1(request): return json({"from": "bp2/r1"}) @bp2.get("/r2/") async def bp2_r2(request): return json({"from": "bp2/r2"}) @bp3.get("/r1") async def bp3_r1(request): return json({"from": "bp3/r1"}) group = Blueprint.group( [bp1, bp2], url_prefix="/slash-check/", version=1.3, strict_slashes=True, ) group2 = Blueprint.group([bp3], url_prefix="/other-prefix/", version="v2", strict_slashes=False) app.blueprint(group) app.blueprint(group2) assert app.test_client.get("/v1.3/slash-check/r1")[1].status == 200 assert app.test_client.get("/v1.3/slash-check/r1/")[1].status == 200 assert app.test_client.get("/v3/slash-check/bp2/r1")[1].status == 200 assert app.test_client.get("/v3/slash-check/bp2/r1/")[1].status == 404 assert app.test_client.get("/v3/slash-check/bp2/r2")[1].status == 404 assert app.test_client.get("/v3/slash-check/bp2/r2/")[1].status == 200 assert app.test_client.get("/v2/other-prefix/bp3/r1")[1].status == 200
def test_bp_group_properties(): blueprint_1 = Blueprint("blueprint_1", url_prefix="/bp1") blueprint_2 = Blueprint("blueprint_2", url_prefix="/bp2") group = Blueprint.group( blueprint_1, blueprint_2, version=1, version_prefix="/api/v", url_prefix="/grouped", strict_slashes=True, ) assert group.version_prefix == "/api/v" assert blueprint_1.version_prefix == "/api/v" assert blueprint_2.version_prefix == "/api/v" assert group.version == 1 assert blueprint_1.version == 1 assert blueprint_2.version == 1 assert group.strict_slashes assert blueprint_1.strict_slashes assert blueprint_2.strict_slashes assert group.url_prefix == "/grouped" assert blueprint_1.url_prefix == "/grouped/bp1" assert blueprint_2.url_prefix == "/grouped/bp2"
def test_bp_group_list_operations(app: Sanic): blueprint_1 = Blueprint("blueprint_1", url_prefix="/bp1") blueprint_2 = Blueprint("blueprint_2", url_prefix="/bp2") @blueprint_1.route("/") def blueprint_1_default_route(request): return text("BP1_OK") @blueprint_2.route("/") def blueprint_2_default_route(request): return text("BP2_OK") blueprint_group_1 = Blueprint.group( blueprint_1, blueprint_2, url_prefix="/bp" ) blueprint_3 = Blueprint("blueprint_2", url_prefix="/bp3") @blueprint_3.route("/second") def blueprint_3_second_route(request): return text("BP3_OK") assert len(blueprint_group_1) == 2 blueprint_group_1.append(blueprint_3) assert len(blueprint_group_1) == 3 del blueprint_group_1[2] assert len(blueprint_group_1) == 2 blueprint_group_1[1] = blueprint_3 assert len(blueprint_group_1) == 2 assert blueprint_group_1.url_prefix == "/bp"
def test_bp_group_with_default_url_prefix(app): from sanic.response import json bp_resources = Blueprint("bp_resources") @bp_resources.get("/") def list_resources_handler(request): resource = {} return json([resource]) bp_resource = Blueprint("bp_resource", url_prefix="/<resource_id>") @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 == [{}] from uuid import uuid4 resource_id = str(uuid4()) request, response = app.test_client.get( "/api/v1/resources/{0}".format(resource_id) ) assert response.json == {"resource_id": resource_id}
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]
def test_bp_group_with_default_url_prefix(app): from sanic.response import json bp_resources = Blueprint('bp_resources') @bp_resources.get('/') def list_resources_handler(request): resource = {} return json([resource]) bp_resource = Blueprint('bp_resource', url_prefix='/<resource_id>') @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 == [{}] from uuid import uuid4 resource_id = str(uuid4()) request, response = app.test_client.get( '/api/v1/resources/{0}'.format(resource_id)) assert response.json == {'resource_id': resource_id}
def test_blueprint_group_versioning(): app = Sanic(name="blueprint-group-test") bp1 = Blueprint(name="bp1", url_prefix="/bp1") bp2 = Blueprint(name="bp2", url_prefix="/bp2", version=2) bp3 = Blueprint(name="bp3", url_prefix="/bp3") @bp3.get("/r1") async def bp3_r1(request): return json({"from": "bp3/r1"}) @bp1.get("/pre-group") async def pre_group(request): return json({"from": "bp1/pre-group"}) group = Blueprint.group([bp1, bp2], url_prefix="/group1", version=1) group2 = Blueprint.group([bp3]) @bp1.get("/r1") async def r1(request): return json({"from": "bp1/r1"}) @bp2.get("/r2") async def r2(request): return json({"from": "bp2/r2"}) @bp2.get("/r3", version=3) async def r3(request): return json({"from": "bp2/r3"}) app.blueprint([group, group2]) assert app.test_client.get("/v1/group1/bp1/r1/")[1].status == 200 assert app.test_client.get("/v2/group1/bp2/r2")[1].status == 200 assert app.test_client.get("/v1/group1/bp1/pre-group")[1].status == 200 assert app.test_client.get("/v3/group1/bp2/r3")[1].status == 200 assert app.test_client.get("/bp3/r1")[1].status == 200 assert group.version == 1 assert group2.strict_slashes is None
def test_bp_group(): app = Sanic('test_nested_bp_groups') deep_0 = Blueprint('deep_0', url_prefix='/deep') deep_1 = Blueprint('deep_1', url_prefix='/deep1') @deep_0.route('/') def handler(request): return text('D0_OK') @deep_1.route('/bottom') def handler(request): return text('D1B_OK') mid_0 = Blueprint.group(deep_0, deep_1, url_prefix='/mid') mid_1 = Blueprint('mid_tier', url_prefix='/mid1') @mid_1.route('/') def handler(request): return text('M1_OK') top = Blueprint.group(mid_0, mid_1) app.blueprint(top) @app.route('/') def handler(request): return text('TOP_OK') request, response = app.test_client.get('/') assert response.text == 'TOP_OK' request, response = app.test_client.get('/mid1') assert response.text == 'M1_OK' request, response = app.test_client.get('/mid/deep') assert response.text == 'D0_OK' request, response = app.test_client.get('/mid/deep1/bottom') assert response.text == 'D1B_OK'
def test_bp_group(app): deep_0 = Blueprint("deep_0", url_prefix="/deep") deep_1 = Blueprint("deep_1", url_prefix="/deep1") @deep_0.route("/") def handler(request): return text("D0_OK") @deep_1.route("/bottom") def bottom_handler(request): return text("D1B_OK") mid_0 = Blueprint.group(deep_0, deep_1, url_prefix="/mid") mid_1 = Blueprint("mid_tier", url_prefix="/mid1") @mid_1.route("/") def handler1(request): return text("M1_OK") top = Blueprint.group(mid_0, mid_1) app.blueprint(top) @app.route("/") def handler2(request): return text("TOP_OK") request, response = app.test_client.get("/") assert response.text == "TOP_OK" request, response = app.test_client.get("/mid1") assert response.text == "M1_OK" request, response = app.test_client.get("/mid/deep") assert response.text == "D0_OK" request, response = app.test_client.get("/mid/deep1/bottom") assert response.text == "D1B_OK"
from sanic.blueprints import Blueprint from ManjiApi.api.skill.all_skills import skill_all_skills from ManjiApi.api.skill.skill import skill_skill skill_endpoint = Blueprint.group(skill_all_skills, skill_skill, url_prefix="/skill")
from sanic.blueprints import Blueprint from heliotrope.routes.api.hitomi.galleryinfo import hitomi_galleyinfo from heliotrope.routes.api.hitomi.images import hitomi_images from heliotrope.routes.api.hitomi.info import hitomi_info from heliotrope.routes.api.hitomi.list import hitomi_list from heliotrope.routes.api.hitomi.search import hitomi_search hitomi_endpoint = Blueprint.group( hitomi_galleyinfo, hitomi_images, hitomi_info, hitomi_list, hitomi_search, url_prefix="/hitomi", )
from sanic import response from sanic.blueprints import Blueprint from sanic.request import Request from views.account import account_blueprint from views.apply import apply_blueprint from views.meal import meal_blueprint from views.notice import notice_blueprint from views.report import report_blueprint blueprint_group = Blueprint.group( account_blueprint, apply_blueprint, meal_blueprint, notice_blueprint, report_blueprint, ) async def ping(request: Request): return response.text('pong!')
from sanic.blueprints import Blueprint from ManjiApi.api.yoshigall import yoshigall_endpoint from ManjiApi.api.skill import skill_endpoint api_endpoint = Blueprint.group(yoshigall_endpoint, skill_endpoint, url_prefix="/api")
from sanic.blueprints import Blueprint from heliotrope.routes.about import heliotrope_about from heliotrope.routes.api import heliotrope_api heliotrope_routes = Blueprint.group(heliotrope_api, heliotrope_about)
from sanic.blueprints import Blueprint from .user import user from .management import management from .announcement import bp_announcement from .login import bp_login from .netflow import bp_netflow from .register import bp_login from .teapot import bp_teapot from .verify_mail import bp_verify_mail from .forgot_password import bp_forgot_passowrd from .log import bp_log api = Blueprint.group( user, management, bp_announcement, bp_login, bp_netflow, bp_verify_mail, bp_teapot, bp_forgot_passowrd, bp_log, url_prefix="/api", )
from sanic.blueprints import Blueprint from ManjiApi.api.yoshigall.post import yoshigall_post from ManjiApi.api.yoshigall.todaytip import yoshigall_todaytip from ManjiApi.api.yoshigall.search import yoshigall_search yoshigall_endpoint = Blueprint.group(yoshigall_post, yoshigall_todaytip, yoshigall_search, url_prefix="/yoshigall")
def test_bp_group(app: Sanic): blueprint_1 = Blueprint("blueprint_1", url_prefix="/bp1") blueprint_2 = Blueprint("blueprint_2", url_prefix="/bp2") @blueprint_1.route("/") def blueprint_1_default_route(request): return text("BP1_OK") @blueprint_1.route("/invalid") def blueprint_1_error(request: Request): raise InvalidUsage("Invalid") @blueprint_2.route("/") def blueprint_2_default_route(request): return text("BP2_OK") @blueprint_2.route("/error") def blueprint_2_error(request: Request): raise ServerError("Error") blueprint_group_1 = Blueprint.group( blueprint_1, blueprint_2, url_prefix="/bp" ) blueprint_3 = Blueprint("blueprint_3", url_prefix="/bp3") @blueprint_group_1.exception(InvalidUsage) def handle_group_exception(request, exception): return text("BP1_ERR_OK") @blueprint_group_1.middleware("request") def blueprint_group_1_middleware(request): global MIDDLEWARE_INVOKE_COUNTER MIDDLEWARE_INVOKE_COUNTER["request"] += 1 @blueprint_group_1.middleware def blueprint_group_1_middleware_not_called(request): global MIDDLEWARE_INVOKE_COUNTER MIDDLEWARE_INVOKE_COUNTER["request"] += 1 @blueprint_group_1.on_request def blueprint_group_1_convenience_1(request): global MIDDLEWARE_INVOKE_COUNTER MIDDLEWARE_INVOKE_COUNTER["request"] += 1 @blueprint_group_1.on_request() def blueprint_group_1_convenience_2(request): global MIDDLEWARE_INVOKE_COUNTER MIDDLEWARE_INVOKE_COUNTER["request"] += 1 @blueprint_3.route("/") def blueprint_3_default_route(request): return text("BP3_OK") @blueprint_3.route("/forbidden") def blueprint_3_forbidden(request: Request): raise Forbidden("Forbidden") blueprint_group_2 = Blueprint.group( blueprint_group_1, blueprint_3, url_prefix="/api" ) @blueprint_group_2.exception(SanicException) def handle_non_handled_exception(request, exception): return text("BP2_ERR_OK") @blueprint_group_2.middleware("response") def blueprint_group_2_middleware(request, response): global MIDDLEWARE_INVOKE_COUNTER MIDDLEWARE_INVOKE_COUNTER["response"] += 1 @blueprint_group_2.on_response def blueprint_group_2_middleware_convenience_1(request, response): global MIDDLEWARE_INVOKE_COUNTER MIDDLEWARE_INVOKE_COUNTER["response"] += 1 @blueprint_group_2.on_response() def blueprint_group_2_middleware_convenience_2(request, response): global MIDDLEWARE_INVOKE_COUNTER MIDDLEWARE_INVOKE_COUNTER["response"] += 1 app.blueprint(blueprint_group_2) @app.route("/") def app_default_route(request): return text("APP_OK") _, response = app.test_client.get("/") assert response.text == "APP_OK" _, response = app.test_client.get("/api/bp/bp1") assert response.text == "BP1_OK" _, response = app.test_client.get("/api/bp/bp1/invalid") assert response.text == "BP1_ERR_OK" _, response = app.test_client.get("/api/bp/bp2") assert response.text == "BP2_OK" _, response = app.test_client.get("/api/bp/bp2/error") assert response.text == "BP2_ERR_OK" _, response = app.test_client.get("/api/bp3") assert response.text == "BP3_OK" _, response = app.test_client.get("/api/bp3/forbidden") assert response.text == "BP2_ERR_OK" assert MIDDLEWARE_INVOKE_COUNTER["response"] == 18 assert MIDDLEWARE_INVOKE_COUNTER["request"] == 16
MIT License Copyright (c) 2021 SaidBySolo Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from sanic.blueprints import Blueprint from heliotrope.rest.api.hitomi import hitomi_endpoint from heliotrope.rest.api.proxy import heliotrope_image_proxy api_endpoint = Blueprint.group(hitomi_endpoint, heliotrope_image_proxy, url_prefix="/api")
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from sanic.blueprints import Blueprint from heliotrope.rest.api.hitomi.galleryinfo import hitomi_galleryinfo from heliotrope.rest.api.hitomi.image import hitomi_image from heliotrope.rest.api.hitomi.info import hitomi_info from heliotrope.rest.api.hitomi.list import hitomi_list from heliotrope.rest.api.hitomi.random import hitomi_random from heliotrope.rest.api.hitomi.search import hitomi_search hitomi_endpoint = Blueprint.group( hitomi_search, hitomi_galleryinfo, hitomi_info, hitomi_list, hitomi_random, hitomi_image, url_prefix="/hitomi", )
from sanic.blueprints import Blueprint from sanic.response import json from sanic import response from routes.v1.index_root import api_info from routes.v1.rtsq import rtsq_Api index = Blueprint.group(api_info, rtsq_Api)
import os import sentry_sdk from sanic import Sanic from sanic.blueprints import Blueprint from sentry_sdk.integrations.sanic import SanicIntegration import Heliotrope from Heliotrope.api import api sentry_sdk.init( dsn=None if os.environ.get("TEST_FLAG") else "https://[email protected]/5417505", integrations=[SanicIntegration()], release=f"heliotrope@{Heliotrope.__version__}", ) app = Sanic(__name__) app.blueprint( Blueprint.group(api, url_prefix=f"/v{Heliotrope.version_info.major}")) if not os.environ.get("TEST_FLAG"): app.config.FORWARDED_SECRET = os.environ["forwarded_secret"] app.config.FALLBACK_ERROR_FORMAT = "json"
from sanic.blueprints import Blueprint from heliotrope import version_info from heliotrope.routes.api.count import heliotrope_request_count from heliotrope.routes.api.hitomi import hitomi_endpoint from heliotrope.routes.api.proxy import heliotrope_image_proxy heliotrope_api = Blueprint.group( hitomi_endpoint, heliotrope_request_count, heliotrope_image_proxy, url_prefix="/api", version=f"v{version_info.major}", )
def test_bp_group_as_list(): blueprint_1 = Blueprint("blueprint_1", url_prefix="/bp1") blueprint_2 = Blueprint("blueprint_2", url_prefix="/bp2") blueprint_group_1 = Blueprint.group([blueprint_1, blueprint_2]) assert len(blueprint_group_1) == 2
from sanic import Sanic from sanic.blueprints import Blueprint from sanic.response import json app = Sanic(name="blue-print-group-version-example") bp1 = Blueprint(name="ultron", url_prefix="/ultron") bp2 = Blueprint(name="vision", url_prefix="/vision", strict_slashes=None) bpg = Blueprint.group(bp1, bp2, url_prefix="/sentient/robot", version=1, strict_slashes=True) @bp1.get("/name") async def bp1_name(request): """This will expose an Endpoint GET /v1/sentient/robot/ultron/name""" return json({"name": "Ultron"}) @bp2.get("/name") async def bp2_name(request): """This will expose an Endpoint GET /v1/sentient/robot/vision/name""" return json({"name": "vision"}) @bp2.get("/name", version=2) async def bp2_revised_name(request): """This will expose an Endpoint GET /v2/sentient/robot/vision/name"""
def test_bp_group_with_additional_route_params(app: Sanic): blueprint_1 = Blueprint("blueprint_1", url_prefix="/bp1") blueprint_2 = Blueprint("blueprint_2", url_prefix="/bp2") @blueprint_1.route( "/request_path", methods=frozenset({"PUT", "POST"}), version=2 ) def blueprint_1_v2_method_with_put_and_post(request: Request): if request.method == "PUT": return text("PUT_OK") elif request.method == "POST": return text("POST_OK") @blueprint_2.route( "/route/<param>", methods=frozenset({"DELETE", "PATCH"}), name="test" ) def blueprint_2_named_method(request: Request, param): if request.method == "DELETE": return text(f"DELETE_{param}") elif request.method == "PATCH": return text(f"PATCH_{param}") blueprint_group = Blueprint.group( blueprint_1, blueprint_2, url_prefix="/api" ) @blueprint_group.middleware("request") def authenticate_request(request: Request): global AUTH auth = request.headers.get("authorization") if auth: # Dummy auth check. We can have anything here and it's fine. if AUTH not in auth: return text("Unauthorized", status=401) else: return text("Unauthorized", status=401) @blueprint_group.middleware("response") def enhance_response_middleware(request: Request, response: HTTPResponse): response.headers.add("x-test-middleware", "value") app.blueprint(blueprint_group) header = {"authorization": " ".join(["Basic", AUTH])} _, response = app.test_client.put( "/v2/api/bp1/request_path", headers=header ) assert response.text == "PUT_OK" assert response.headers.get("x-test-middleware") == "value" _, response = app.test_client.post( "/v2/api/bp1/request_path", headers=header ) assert response.text == "POST_OK" _, response = app.test_client.delete("/api/bp2/route/bp2", headers=header) assert response.text == "DELETE_bp2" _, response = app.test_client.patch("/api/bp2/route/bp2", headers=header) assert response.text == "PATCH_bp2" _, response = app.test_client.put("/v2/api/bp1/request_path") assert response.status == 401
from sanic.blueprints import Blueprint from .user import bp_user from .log import bp_log from .abuse import bp_abuse from .announcement import announcement from .ip import bp_ip from .netflow import netflow from .bed_change import bp_bed_change management = Blueprint.group( bp_user, bp_log, announcement, bp_ip, bp_bed_change, bp_abuse, netflow, url_prefix="/management", )
def test_bp_group_with_additional_route_params(app: Sanic): blueprint_1 = Blueprint("blueprint_1", url_prefix="/bp1") blueprint_2 = Blueprint("blueprint_2", url_prefix="/bp2") @blueprint_1.route( "/request_path", methods=frozenset({"PUT", "POST"}), version=2 ) def blueprint_1_v2_method_with_put_and_post(request: Request): if request.method == "PUT": return text("PUT_OK") elif request.method == "POST": return text("POST_OK") @blueprint_2.route( "/route/<param>", methods=frozenset({"DELETE", "PATCH"}), name="test" ) def blueprint_2_named_method(request: Request, param): if request.method == "DELETE": return text("DELETE_{}".format(param)) elif request.method == "PATCH": return text("PATCH_{}".format(param)) blueprint_group = Blueprint.group( blueprint_1, blueprint_2, url_prefix="/api" ) @blueprint_group.middleware("request") def authenticate_request(request: Request): global AUTH auth = request.headers.get("authorization") if auth: # Dummy auth check. We can have anything here and it's fine. if AUTH not in auth: return text("Unauthorized", status=401) else: return text("Unauthorized", status=401) @blueprint_group.middleware("response") def enhance_response_middleware(request: Request, response: HTTPResponse): response.headers.add("x-test-middleware", "value") app.blueprint(blueprint_group) header = {"authorization": " ".join(["Basic", AUTH])} _, response = app.test_client.put( "/v2/api/bp1/request_path", headers=header ) assert response.text == "PUT_OK" assert response.headers.get("x-test-middleware") == "value" _, response = app.test_client.post( "/v2/api/bp1/request_path", headers=header ) assert response.text == "POST_OK" _, response = app.test_client.delete("/api/bp2/route/bp2", headers=header) assert response.text == "DELETE_bp2" _, response = app.test_client.patch("/api/bp2/route/bp2", headers=header) assert response.text == "PATCH_bp2" _, response = app.test_client.get("/v2/api/bp1/request_path") assert response.status == 401
""" MIT License Copyright (c) 2021 SaidBySolo Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from sanic.blueprints import Blueprint from heliotrope.rest.api import api_endpoint # NOTE: Will fixed rest = Blueprint.group(api_endpoint)