The Wayback Machine - https://web.archive.org/web/20201115183149/https://github.com/encode/starlette/pull/875
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

Customizable request class #875

Open
wants to merge 5 commits into
base: master
from

Conversation

@alex-oleshkevich
Copy link
Contributor

@alex-oleshkevich alex-oleshkevich commented Mar 24, 2020

This allows users to change the default Request class.

There are following customization options:

  1. Set request_class on Route constructor:
Route('/', endpoint, request_class=MyRequest)

This has higher precedence than request_class defined in HTTPEndpoint class.

  1. Set request_class in HTTPEndpoint:
class MyEndpoint(HTTPEndpoint):
    request_class = MyRequest

    def get(self, request: MyRequest): ...
@alex-oleshkevich alex-oleshkevich marked this pull request as ready for review Mar 24, 2020
@florimondmanca
Copy link
Member

@florimondmanca florimondmanca commented Mar 25, 2020

Dropping by out of curiosity — could you explicit some practical use cases that would motivate the ability to swap the request class? Maybe this is linked to an existing issue?

@em92
Copy link
Contributor

@em92 em92 commented Mar 25, 2020

could you explicit some practical use cases that would motivate the ability to swap the request class?

I would store this to MyRequest constructor. https://github.com/em92/quakelive-local-ratings/blob/879c354c4f8d25965c93f6a1c84e68c48f1aabb4/qllr/endpoints.py#L53-L68

@alex-oleshkevich
Copy link
Contributor Author

@alex-oleshkevich alex-oleshkevich commented Mar 25, 2020

I am building an application that has a custom Request class. This class has extra methods related to it (input validation, helpers to get values from GET/POST, user IP, and many other helpers).

In order to make use of it, I have to completely extend Route/HTTPEndpoint/request_response names. It is a huge overhead here.

Also, it is useful for frameworks built on top of Starlette. They often have own request classes (look at HttpRequest in Django and Request in DRF).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants
You can’t perform that action at this time.