def test_blueprints_add_resource(): bp = Blueprint(name='test') app = Rafter() async def main(request): return {'test': 'abc'} bp.add_resource(main, '/') app.blueprint(bp) request, response = app.test_client.get('/') assert response.status == 200 assert response.json == {'test': 'abc'}
def test_blueprint_extra_kwargs(): bp = Blueprint('test', url_prefix='/prefix') app = Rafter() def simple_filter(get_response, params): async def decorated_filter(request, *args, **kwargs): if request.args.get('a') == '1': return Response([1, 2, 3]) return await get_response(request, *args, **kwargs) return decorated_filter @bp.resource('/test', validators=[simple_filter]) async def main(request): return {'test': 'abc'} app.blueprint(bp) request, response = app.test_client.get('/test') assert response.status == 404 assert response.json == { 'status': 404, 'message': 'Requested URL /test not found' } request, response = app.test_client.get('/prefix/test') assert response.status == 200 assert response.json == {'test': 'abc'} request, response = app.test_client.get('/prefix/test?a=1') assert response.status == 200 assert response.json == [1, 2, 3]
# -*- coding: utf-8 -*- from rafter import Blueprint, Rafter bpv1 = Blueprint('v1', url_prefix='/v1') bpv2 = Blueprint('v2') def header_filter(get_response, params): async def decorated_filter(request, *args, **kwargs): response = await get_response(request, *args, **kwargs) response.headers['x-test'] = 'abc' return response return decorated_filter @bpv1.resource('/') async def v1_root(request): return {'version': 1} @bpv1.resource('/test') async def v1_test(request): return [3, 2, 1] @bpv2.resource('/', validators=[header_filter]) async def v2_root(request): return {'version': 2}
# from sanic.blueprints import Blueprint from rafter import Blueprint from sanic.response import json from rafter_openapi import doc from models import Car, Status from data import test_car, test_success blueprint = Blueprint('Car', '/car') @blueprint.get("/", strict_slashes=True) @doc.summary("Fetches all cars") @doc.description("Really gets the job done fetching these cars. I mean, really, wow.") # noqa @doc.produces([Car]) def car_list(request): return json([test_car]) @blueprint.get("/<car_id:int>", strict_slashes=True) @doc.summary("Fetches a car") @doc.produces(Car) def car_get(request, car_id): return json(test_car) @blueprint.put("/<car_id:int>", strict_slashes=True) @doc.summary("Updates a car") @doc.consumes(Car, location='body') @doc.consumes({'AUTHORIZATION': str}, location='header')
# from sanic.blueprints import Blueprint from rafter import Blueprint from sanic.response import json from rafter_openapi import doc from models import Driver, Status from data import test_driver, test_success blueprint = Blueprint('Driver', '/driver') @blueprint.get("/", strict_slashes=True) @doc.summary("Fetches all drivers") @doc.produces([Driver]) def driver_list(request): return json([test_driver]) @blueprint.get("/<driver_id:int>", strict_slashes=True) @doc.summary("Fetches a driver") @doc.produces(Driver) def driver_get(request, driver_id): return json(test_driver) @blueprint.put("/<driver_id:int>", strict_slashes=True) @doc.summary("Updates a driver") @doc.consumes(Driver) @doc.produces(Driver) def driver_put(request, driver_id): return json(test_driver)
# from sanic.blueprints import Blueprint from rafter import Blueprint from sanic.response import json from rafter_openapi import doc from models import Driver, Status from data import test_manufacturer, test_success blueprint = Blueprint('Manufacturer', '/manufacturer') @blueprint.get("/", strict_slashes=True) @doc.summary("Fetches all manufacturers") @doc.produces([Driver]) def manufacturer_list(request): return json([test_manufacturer]) @blueprint.get("/<manufacturer_id:int>", strict_slashes=True) @doc.summary("Fetches a manufacturer") @doc.produces(Driver) def manufacturer_get(request, manufacturer_id): return json(test_manufacturer) @blueprint.put("/<manufacturer_id:int>", strict_slashes=True) @doc.summary("Updates a manufacturer") @doc.consumes(Driver) @doc.produces(Driver) def manufacturer_put(request, manufacturer_id): return json(test_manufacturer)
# from sanic.blueprints import Blueprint from rafter import Blueprint from sanic.response import json from rafter_openapi import doc from models import Garage, Car, Status from data import test_garage, test_success blueprint = Blueprint('Garage', '/garage') @blueprint.get("/", strict_slashes=True) @doc.summary("Fetches the garage") @doc.produces(Garage) async def get_garage(request): return json(test_garage) @blueprint.get("/cars", strict_slashes=True) @doc.summary("Fetches the cars in the garage") @doc.consumes({"doors": int}) @doc.produces({"cars": [Car]}) async def get_cars(request): return json(test_garage.get('cars')) @blueprint.put("/car", strict_slashes=True) @doc.summary("Adds a car to the garage") @doc.consumes(Car) @doc.produces(Status) async def add_car(request):