Opened 2 years ago

Last modified 2 years ago

#33780 closed New feature

FEATURE: add http method argument to the URLResolver — at Initial Version

Reported by: abetkin Owned by: nobody
Component: Core (URLs) Version: 4.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi fellows,

I know the tracker is not the best place to discuss features, still here it is.

Currently, we have URLResolver that can resolve paths. I propose that we add the http method as an argument, too:

resolver.resolve(path, method)

I think, the motivation is clear: the endpoint is actually path + http method. If we look at the swagger page of any API, that becomes clear. In the REST specification, for example, listing the items and creating new item have the same URL. Semantically - quite different things.

Other frameworks, like Sanic or FastAPI, already support direct routing by http method (you can decorate the view with @app.get(your_url), so does Pyramid).

Moreover, in the mixed wsgi + asgi application, I might want to make creating an item an async view (because the user should be notified about this event), and showing the list of item - a regular sync view. I am not able to do this currently.

Finally: the implementation. Surprisingly, it is very simple. We just add an argument to URLResolver.resolve and then pass it to the recursive call as well. In the URLPattern we first check the http method and then call the super implementation. Please have a look at this gist https://gist.github.com/abetkin/ba7fccedb95d656bbb77287719482e7b

In urls.py we shall be able to specify the method directly:

urlpatterns = [
    get   ('items/', list_items),
    post  ('items/', create_item),
]

In the end, I want to stress that there is NO DOWNSIDE of this whatsoever. It's not hard to make resolver.resolve(path) behave as it used to, when the method is not specified. All classes / instances remain the same so there will be no problems with third-party libs like rest_framework, swagger generators or others.

Personally I am ready to contribute to this feature.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top