Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hey, I just met you, and this is crazy, but lets put routes in a module... Blueprints maybe? #651

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions chalice/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def _matches_content_type(content_type, valid_content_types):
return content_type in valid_content_types


def naive_urljoin(*args):
return "/".join([str(x).rstrip('/') for x in args])


class ChaliceError(Exception):
pass

Expand Down Expand Up @@ -543,6 +547,30 @@ def _register_view(view_func):
return view_func
return _register_view

def register_blueprint(self, blueprint, url_prefix=None):

for blueprint_route in blueprint.routes:
path, view_func, kwargs = blueprint_route

if url_prefix is None:
constructed_path = path
else:
constructed_path = naive_urljoin(
url_prefix,
path.lstrip('/')
)

view_name = kwargs.pop('name', view_func.__name__)
kwargs['name'] = '.'.join([blueprint.name, view_name])

self._add_route(
constructed_path,
view_func,
**kwargs
)

blueprint.REGISTERED_APP = self

def _add_route(self, path, view_func, **kwargs):
name = kwargs.pop('name', view_func.__name__)
methods = kwargs.pop('methods', ['GET'])
Expand Down
40 changes: 40 additions & 0 deletions chalice/blueprint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from typing import Any, Callable, List # noqa

from chalice.app import Request # noqa


class Blueprint(object):

REGISTERED_APP = None

def __init__(self, name): # type: (str) -> None
self.name = name # type: str
self.routes = [] # type: List

def url_for(self, view_name): # type: (str) -> str
if view_name.startswith('{}.'.format(self.name)):
pass
else:
view_name = '{}.{}'.format(self.name, view_name.lstrip('.'))

if self.REGISTERED_APP is None:
raise RuntimeError('Blueprint must be registered first!')
for path, methods in self.REGISTERED_APP.routes.items():
for method in methods.keys():
found_name = self.REGISTERED_APP.routes[path][method].view_name
if found_name == view_name:
return path

raise LookupError('No url found for {}'.format(view_name))

@property
def current_request(self): # type: () -> Request
if self.REGISTERED_APP is None:
raise RuntimeError('Blueprint must be registered first!')
return self.REGISTERED_APP.current_request

def route(self, path, **kwargs): # type: (str, **Any) -> Callable
def _register_view(view_func): # type: (Callable) -> Callable
self.routes.append((path, view_func, kwargs))
return view_func
return _register_view
3 changes: 2 additions & 1 deletion tests/unit/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest
from pytest import fixture
import hypothesis.strategies as st
from hypothesis import given, assume
from hypothesis import given, assume, settings
import six


Expand Down Expand Up @@ -1295,6 +1295,7 @@ def test_raw_body_is_none_if_body_is_none():


@given(http_request_kwargs=HTTP_REQUEST)
@settings(suppress_health_check=(3))
def test_http_request_to_dict_is_json_serializable(http_request_kwargs):
# We have to do some slight pre-preocessing here
# to maintain preconditions. If the
Expand Down