I've got a base class for my view that has a get(...) method I used for many different endpoints. Just like in regular non-typed python I have a need for **kwargs that's optional. I.e. certain views have querypamaters and others don't and all with varying types.
The meat of the get(...) is the same for all views hence I'm trying to stay DRY here but I'm new to python typing and FastAPI so I don't know how to go about doing this without cluttering up my code just to handle each view's get request.
Thanks!
If you want access to the raw query parameters dict, this is available via request.query_params if you add request: starlette.requests.Request as an argument to your endpoint function. Let us know if that doesn't answer your question.
Ah, that's very useful to know. I'll give that a try. Could you point me to where that's at in the documentation?
There is a docs page here, which links to the relevant starlette docs for more info. Hope that鈥檚 useful!
Yes! That does indeed thank you :)
I'll close this ticket as my problem is ultimately solved but since we on the subject I'm wondering if you know of any proper patterns that you or others employ to achieve what I'm doing? I.e. reusable views that would take different sets of parameters based on the url. Maybe this API + typing doesn't lend itself well to that?
Thanks again.
@Subaku Yeah, I'm not sure of a great way to do this. GraphQL might be relevant depending on what you are trying to do. In general, for type safety you'd probably need to specify the specific parameters you are willing to accept everywhere it differs. You can always take this non-type-safe approach though too and do manual validation in your endpoint code if that is preferable for some reason.
Thanks for the help here @dmontagu ! :cake: :bowing_man:
And thanks @Subaku for reporting back and closing the issue :+1: