Maillol / aiohttp-pydantic

Aiohttp View that validates request body and query sting regarding the annotations declared in the View method
MIT License
67 stars 21 forks source link

call a POST request in the code #50

Closed Rama3an closed 7 months ago

Rama3an commented 10 months ago

Hello!

I'm in code trying to handle an error that occurs due to invalid input data, and I'm trying to call the POST request again in on_validation_error, but I'm getting an error that the POST request is not expecting parameters

async def on_validation_error(
            self, exception: ValidationError, context: str):
        ...
        request_not_error_model = await self.post(request_input, config_models=not_error_config)
        ...

@staticmethod
async def post(request_input: AggregatorInput, config_models: dict = None):
        ...

I'm getting an error

request_not_error_model = await self.post(request_input, config_models=not_error_config)\nTypeError: AggregatorView.post() got an unexpected keyword argument 'config_models'"}
Maillol commented 10 months ago

Hello,

In fact, you cannot call post directly, (or other view methods such as get, put ...) because, under the hood, your post method become aiohttp_pydantic.view.inject_params.wrapped_handler.

You should not call post in the on_validation_error to avoid infinite recursion (if request data is not fixed and post is called again)

However, if you still want do that, try to update the self.request attribute to fix data, and await post method without parameters, but I'm not sure that you can update an aiohttp request object.

async def on_validation_error(
            self, exception: ValidationError, context: str):

        self.request = FixedRequestFromWrongRequest(self.request) 
        return await self.post()