-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Route definitions #2004
Route definitions #2004
Conversation
The usage:
|
cool! what if |
I don't follow. |
Codecov Report
@@ Coverage Diff @@
## master #2004 +/- ##
==========================================
+ Coverage 97.09% 97.16% +0.06%
==========================================
Files 39 39
Lines 7781 7967 +186
Branches 1357 1389 +32
==========================================
+ Hits 7555 7741 +186
- Misses 101 102 +1
+ Partials 125 124 -1
Continue to review full report at Codecov.
|
for example, url prefix, or some kind of permissions, etc. or even use different route style. |
Right now we have no permissions, let's don't discuss it here.
Final url will be P.S. Now I think |
I think of |
Sub-application has a context: state aka |
This is a movement in opposite direction than your original propose.
At first glance it could be Fetching data by analyzing P.S. P.P.S. |
aiohttp/web_urldispatcher.py
Outdated
|
||
HTTP_METHOD_RE = re.compile(r"^[0-9A-Za-z!#\$%&'\*\+\-\.\^_`\|~]+$") | ||
PATH_SEP = re.escape('/') | ||
|
||
|
||
class RouteDef(namedtuple('_RouteDef', 'method, path, handler, kwargs')): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not only Route
?
What's the meaning of Def
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we already use Route
name for routes added by app.router.add_route()
call.
RouteDef
is a scratch for non-added-yet route definition
|
||
|
||
def head(path, handler, **kwargs): | ||
return route(hdrs.METH_HEAD, path, handler, **kwargs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the code gets more readable when we do not use abbreviations. Can I open a PR
by renaming hdrs
to headers
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good in general but for this particular case it doesn't make sense I think.
hdrs
is not a part of our public API anyway.
route.register(self) | ||
|
||
|
||
def route(method, path, handler, **kwargs): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think after refactoring this function is not required
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is: user might want to provide custom HTTP method in very rare cases
aiohttp/web_urldispatcher.py
Outdated
|
||
HTTP_METHOD_RE = re.compile(r"^[0-9A-Za-z!#\$%&'\*\+\-\.\^_`\|~]+$") | ||
PATH_SEP = re.escape('/') | ||
|
||
|
||
class RouteDef(namedtuple('_RouteDef', 'method, path, handler, kwargs')): | ||
# TODO: add __repr__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def __repr__(self):
return (
"<RouteDef {method} {path} -> {handler.__name__!r}, "
"{kwargs}>".format(**self._asdict())
)
def __str__(self):
return self.__repr__()
__________
result
<RouteDef GET /some -> 'get_user', {}>
but i think will be better just Route
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said Route
name is captured by already existing class hierarchy.
It's present instantiated route.
In opposite route definition is for list of routes to be added to router instance.
I still think the distinction is matter.
aiohttp/web_urldispatcher.py
Outdated
def __init__(self): | ||
self._items = [] | ||
|
||
# TODO: add __repr__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import operator
def __repr__(self):
items = sorted(
self._items,
key=operator.attrgetter('path', 'method')
)
return "<RoutesDef\n\t{}\n>".format(
"\n\t".join(map(str, items))
)
____
result
<RoutesDef
< RouteDef GET /article -> 'get_article', {}>
< RouteDef GET /user -> 'get_user', {}>
< RouteDef POST /user -> 'post_user', {}>
>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for suggestion for route definitions should be sorted by insertion order.
It is crucial for router implementation.
@fafhrd91 and others. I love to merge the PR after polishing. Now we have directive way for adding new routes: Also it adds decoration based style like flask and bottle: https://github.com/aio-libs/aiohttp/pull/2004/files#diff-1fe46683db6b1c2e6b977a6b3f5da19f I feel people need it too. If @fafhrd91 will say strict no for PR I'll split it into separate ones for table and decorators. |
lgtm |
Thank you @fafhrd91 |
BTW the PR is pretty close to https://github.com/IlyaSemenov/aiohttp_route_decorator |
Docs added, code is 100% covered. |
aiohttp/web_urldispatcher.py
Outdated
info += ", {}={}".format(name, value) | ||
return ("<RouteDef {method} {path} -> {handler.__name__!r}" | ||
"{info}>".format(method=self.method, path=self.path, | ||
handler=self.handler, info=''.join(info))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think will be better if info will be string
then you can delete ''.join(info)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This style is used by asyncio in many places, I like to keep it in aiohttp too
Alternative to #2003
@fafhrd91 your proposal was very seductive.
I've implemented it :)