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

Conversation

alex-oleshkevich
Copy link
Member

@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 March 24, 2020 23:12
@florimondmanca
Copy link
Member

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 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
Member Author

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).

@Kludex
Copy link
Member

Kludex commented Feb 1, 2022

@alex-oleshkevich After this whole time, do you still want this feature? Is it something you'd want to discuss?

@alex-oleshkevich
Copy link
Member Author

Yes, i use custom request time each time. My class has some extra properties and utilities.

@adriangb adriangb added feature New feature or request outdated No updates on issue or commits within the last 6 months labels Feb 2, 2022
@alex-oleshkevich
Copy link
Member Author

There can be a workaround with a simple decorator that inspects view signature and injects a needed request instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request outdated No updates on issue or commits within the last 6 months
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants