-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Add path_pattern property to routes and resources #2993
Conversation
Thank for the PR. I have no personal opinion. |
I'm for formatted string, or, actually, placeholders. Regexps are more powerful, but with their power they move part of data validation on router side. Say, Also, I would like to note, that |
As far as I've understood, regex patterns only exist in DynamicResource. If other resources return a string and DynamicResource returns a regex, it looks a bit like a broken API, inconsistent. It seems more logic to me that all resources return a string. On the other hand, I think regex is more useful than formatted string, as PD: Sorry if I've misunderstood something. It is the first time I dive into aiohttp code. |
@jettify could you help to make a decision? |
Codecov Report
@@ Coverage Diff @@
## master #2993 +/- ##
==========================================
+ Coverage 98.09% 98.09% +<.01%
==========================================
Files 42 42
Lines 7711 7718 +7
Branches 1345 1345
==========================================
+ Hits 7564 7571 +7
Misses 51 51
Partials 96 96
Continue to review full report at Codecov.
|
Sorry for late reply, was out of town. My use case is tracing system [1] where canonical name can be used for grouping spans, this also useful for metrics subsystems too to count number of requests per specific route, using path pattern as a key instead of full actual path (with user is for example). For this purpose I think formatted string is better since it is easier to read. |
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.
@pposca please update the PR:
- Rename
path_pattern
tocanonical
(I don't like long complex names). - Use formatting template instead of regex.
@asvetlov , do you prefer a 2nd commit with the update or that I amend the first commit? |
Don't care, PRs are squashed on merging anyway. |
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.
The documentation update is required.
The property audience is not very wide, perhaps changing web_reference.rst
is enough.
aiohttp/web_urldispatcher.py
Outdated
@@ -131,6 +140,15 @@ def handler(self): | |||
def name(self): | |||
"""Optional route's name, always equals to resource's name.""" | |||
|
|||
@property | |||
@abc.abstractmethod | |||
def canonical(self): |
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.
Should we have route.canonical
?
route.resource.canonical
should be enough.
route.name
exists for historical reasons, routes were added before the resource invention.
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.
The issue was asking for a route property, that's why I added it.
I will change it and I will update the docs (I completely forgot them...)
aiohttp/web_urldispatcher.py
Outdated
@@ -457,6 +478,10 @@ def __init__(self, prefix, directory, *, name=None, | |||
'HEAD': ResourceRoute('HEAD', self._handle, self, | |||
expect_handler=expect_handler)} | |||
|
|||
@property | |||
def canonical(self): | |||
return self._prefix + str(self._directory) |
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.
Looks like there is an error here.
Canonical URL should contain the prefix only, the directory is a local path.
It never returns in URLs but url_for()
constructs a return value from the prefix and a relative to the directory path.
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.
Ok, I will remove it from StaticResource, as canonical is already the prefix in PrefixResource and StaticResource will inherit the property.
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.
Everything is perfect, the last neat is left.
|
||
Read-only *canonical path* associate with the resource. For example | ||
``/path/to`` or ``/path/{to}`` | ||
|
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.
.. versionadded:: 3.3
.. attribute:: canonical | ||
|
||
Read-only *canonical path* associate with the resource. Returns the path | ||
used to create the PlainResource. For example ``/path/to`` |
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.
versionadded
|
||
Read-only *canonical path* associate with the resource. Returns the | ||
formatter obtained from the path used to create the DynamicResource. | ||
For example, from a path ``/get/{num:^\d+}``, it returns ``/get/{num}`` |
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.
versionadded
|
||
Read-only *canonical path* associate with the resource. Returns the prefix | ||
used to create the StaticResource. For example ``/prefix`` | ||
|
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.
versionadded
Read-only *canonical path* associate with the resource. Returns the | ||
prefix used to create the PrefixedSubAppResource. | ||
For example ``/prefix`` | ||
|
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.
versionadded
The canonical is the path used to add a new route. For example, /foo/bar/{name}. For DynamicResource, canonical exposes the formatter. - Add canonical property to AbstractResource - Add canonical implementation to DynamicResource, PrefixResource and StaticResource. - Add tests - Add docs Closes aio-libs#2968
Well done! |
My pleasure. Really happy to help. |
Thanks @pposca @asvetlov ! I have already added this property to aiozipkin integration (aio-libs/aiozipkin#138) |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs. |
The path_pattern is the raw path used to add a new route. For example,
/foo/bar/{name:\d+}.
PrefixResource, StaticResource, ResourceRoute and SystemRoute.
Closes #2968
What do these changes do?
Add path_pattern property to routes and resources.
The path_pattern is the raw path used to add a new route. For example, /bar/{name:\d+}.
Are there changes in behavior for the user?
The public API of Route and Resource has now a new property path_name to expose directly the raw path used to add a new route. Significant values have been selected for each implementation of the abstract classes.
Related issue number
2968
Checklist
CONTRIBUTORS.txt
CHANGES
folder<issue_id>.<type>
for example (588.bugfix)issue_id
change it to the pr id after creating the pr.feature
: Signifying a new feature..bugfix
: Signifying a bug fix..doc
: Signifying a documentation improvement..removal
: Signifying a deprecation or removal of public API..misc
: A ticket has been closed, but it is not of interest to users.