def setup(app, url_path_prefix): """Set up routes as resources so we can use the `Index:get` notation for URL lookup.""" combined_routes = [ *extra_routes, *employee_routes, *static_routes, *saml_routes, *downloads_routes, *microservices_handler_routes, *customsql_handler_routes, *customsql_choice_handler_routes, ] module = ('app.handler') for route in combined_routes: prefix = url_path_prefix if route.kwargs.get('use_prefix', True) else '' if route.method == "*": with add_resource_context(app, module=module, url_prefix=prefix) as new_resource: new_resource(route.path, route.handler()) else: with add_route_context(app, module=module, url_prefix=prefix) as new_route: new_route(route.method, route.path, route.handler)
def test_add_resource_context_passing_classes(self, app): with add_resource_context(app) as route: route('/articles/', views.ArticleResource()) route('/articles/{pk}', views.ArticleList()) assert app.router['ArticleResource:get'].url() == '/articles/' assert app.router['ArticleResource:post'].url() == '/articles/'
def test_add_resource_context_passing_classes(self, app): with add_resource_context(app) as route: route('/articles/', views.ArticleResource()) route('/articles/{pk}', views.ArticleList()) assert str(app.router['ArticleResource:get'].url_for()) == '/articles/' assert str(app.router['ArticleResource:post'].url_for()) == '/articles/'
def test_add_resource_context_passing_classes_with_prefix(self, app): with add_resource_context(app, name_prefix='articles') as route: route('/articles/', views.ArticleResource()) route('/articles/{pk}', views.ArticleList()) assert app.router['articles.ArticleResource:get'].url() == '/articles/' assert app.router['articles.ArticleResource:post'].url() == '/articles/' assert app.router['articles.ArticleList:post'].url(parts={'pk': 42}) == '/articles/42'
def test_passing_module_and_resources_as_strings(self, app): with add_resource_context(app, module='tests.views') as route: route('/articles/', 'ArticleResource') route('/articles/{pk}', 'ArticleList') assert app.router['ArticleResource:get'].url() == '/articles/' assert app.router['ArticleResource:post'].url() == '/articles/' assert app.router['ArticleList:post'].url(parts={'pk': 42}) == '/articles/42'
def test_add_resource_context_basic(self, app): with add_resource_context(app, views) as route: route('/articles/', 'ArticleResource') route('/articles/{pk}', 'ArticleList') assert app.router['ArticleResource:get'].url() == '/articles/' assert app.router['ArticleResource:post'].url() == '/articles/' assert app.router['ArticleList:post'].url(parts={'pk': 42}) == '/articles/42'
def test_add_resource_context_with_name_prefix_and_override(self, app): with add_resource_context(app, views, name_prefix='api') as route: route('/articles/', 'ArticleResource', names={'get': 'list_articles'}) assert app.router['api.list_articles'].url() == '/articles/' assert app.router['api.ArticleResource:post'].url() == '/articles/'
def test_passing_module_and_resources_as_strings(self, app): with add_resource_context(app, module='tests.views') as route: route('/articles/', 'ArticleResource') route('/articles/{pk}', 'ArticleList') assert str(app.router['ArticleResource:get'].url_for()) == '/articles/' assert str(app.router['ArticleResource:post'].url_for()) == '/articles/' assert str(app.router['ArticleList:post'].url_for( pk='42')) == '/articles/42'
def test_add_resource_context_passing_classes_with_prefix(self, app): with add_resource_context(app, name_prefix='articles') as route: route('/articles/', views.ArticleResource()) route('/articles/{pk}', views.ArticleList()) assert str(app.router['articles.ArticleResource:get'].url_for()) == '/articles/' assert str(app.router['articles.ArticleResource:post'].url_for()) == '/articles/' assert str(app.router['articles.ArticleList:post'].url_for( pk='42')) == '/articles/42' # noqa
def test_add_resource_context_basic(self, app): with add_resource_context(app, views) as route: route('/articles/', 'ArticleResource') route('/articles/{pk}', 'ArticleList') assert str(app.router['ArticleResource:get'].url_for()) == '/articles/' assert str(app.router['ArticleResource:post'].url_for()) == '/articles/' assert str(app.router['ArticleList:post'].url_for( pk='42')) == '/articles/42'
def test_add_resource_context_basic(self, app): with add_resource_context(app, views) as route: route('/articles/', 'ArticleResource') route('/articles/{pk}', 'ArticleList') assert app.router['ArticleResource:get'].url() == '/articles/' assert app.router['ArticleResource:post'].url() == '/articles/' assert app.router['ArticleList:post'].url( parts={'pk': 42}) == '/articles/42'
def test_add_resource_context_passing_classes_with_prefix(self, app): with add_resource_context(app, name_prefix='articles') as route: route('/articles/', views.ArticleResource()) route('/articles/{pk}', views.ArticleList()) assert app.router['articles.ArticleResource:get'].url() == '/articles/' assert app.router['articles.ArticleResource:post'].url( ) == '/articles/' assert app.router['articles.ArticleList:post'].url( parts={'pk': 42}) == '/articles/42'
def setup(app, url_path_prefix): """Set up routes as resources so we can use the `Index:get` notation for URL lookup.""" combined_routes = [*request_routes, *start_routes, *static_routes, *web_form_routes, *webchat_routes, *common_routes, *support_centre_routes] for route in combined_routes: use_prefix = route.kwargs.get('use_prefix', True) prefix = url_path_prefix if use_prefix else '' with add_resource_context(app, module='app.handlers', url_prefix=prefix) as new_route: new_route(route.path, route.handler())
def test_make_resource_override_on_context_manager(self, app): db = {} with add_resource_context(app, module='tests.views', make_resource=lambda cls: cls(db=db)) as route: route('/authors/', 'AuthorList')
def setup(app, url_path_prefix): """Set up routes as resources so we can use the `Index:get` notation for URL lookup.""" for route in routes: prefix = url_path_prefix if route.kwargs.get('use_prefix', True) else '' with add_resource_context(app, module='app.handlers', url_prefix=prefix) as new_route: new_route(route.path, route.handler())
def test_add_resource_context_with_name_prefix(self, app): with add_resource_context(app, views, name_prefix='api') as route: route('/articles/', 'ArticleResource') assert str(app.router['api.ArticleResource:get'].url_for()) == '/articles/' assert str(app.router['api.ArticleResource:post'].url_for()) == '/articles/'
def test_make_resource_override_on_context_manager(self, app): db = {} with add_resource_context(app, module='tests.views', make_resource=lambda cls: cls(db=db)) as route: route('/authors/', 'AuthorList')
@coroutine def index(request): return Response('Welcome!') class HelloResource: @coroutine def get(self, request): return Response({ 'message': 'Welcome to the API!' }) with routing.add_route_context(app) as route: route('GET', '/', index) with routing.add_resource_context(app, url_prefix='/api/') as route: route('/', HelloResource()) negotiation.setup(app) path_norm.setup(app) if __name__ == "__main__": run( app, app_uri="examples.kitchen_sink:app", reload=True, port=8000 )
def test_add_resource_context_with_name_prefix(self, app): with add_resource_context(app, views, name_prefix='api') as route: route('/articles/', 'ArticleResource') assert app.router['api.ArticleResource:get'].url() == '/articles/' assert app.router['api.ArticleResource:post'].url() == '/articles/'
def test_add_resource_context_with_url_prefix(self, app): with add_resource_context(app, views, url_prefix='/api/') as route: route('/articles/', 'ArticleResource') assert app.router['ArticleResource:get'].url() == '/api/articles/' assert app.router['ArticleResource:post'].url() == '/api/articles/'
def test_add_resource_context_with_name_prefix_and_override(self, app): with add_resource_context(app, views, name_prefix='api') as route: route('/articles/', 'ArticleResource', names={'get': 'list_articles'}) assert app.router['api.list_articles'].url() == '/articles/' assert app.router['api.ArticleResource:post'].url() == '/articles/'
from asyncio import coroutine from aiohttp import web from aiohttp_utils import Response, routing, negotiation, run, path_norm app = web.Application(router=routing.ResourceRouter()) @coroutine def index(request): return Response('Welcome!') class HelloResource: @coroutine def get(self, request): return Response({'message': 'Welcome to the API!'}) with routing.add_route_context(app) as route: route('GET', '/', index) with routing.add_resource_context(app, url_prefix='/api/') as route: route('/', HelloResource()) negotiation.setup(app) path_norm.setup(app) if __name__ == "__main__": run(app, app_uri="examples.kitchen_sink:app", reload=True, port=8000)