Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#33780 closed New feature (duplicate)

Add http method argument to the URLResolver

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 (last modified by abetkin)

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 (4)

comment:1 by abetkin, 2 years ago

Triage Stage: UnreviewedAccepted
Type: UncategorizedNew feature
Version: 4.0dev

comment:2 by abetkin, 2 years ago

Description: modified (diff)

comment:3 by Mariusz Felisiak, 2 years ago

Component: UncategorizedCore (URLs)
Description: modified (diff)
Resolution: duplicate
Status: newclosed
Summary: FEATURE: add http method argument to the URLResolverAdd http method argument to the URLResolver
Triage Stage: AcceptedUnreviewed
Version: dev4.0

Duplicate of #2784.

Also, you cannot accept your own tickets.

comment:4 by abetkin, 2 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top