You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With petisco you normally define success_handler and error_map to manage all the paths your use case can take.
In the above example:
With success_handler: lambda result: {"message": "Task saved"} your program allways will return a 200 (default value) and the payload {"message": "Task saved"}.
In case of failure, it will return 409 or 422 depending on the Failure returned by TaskCreator:
* TaskAlreadyCreated -> 409
* TaskDescriptionTooLong -> 422
But what if you want to have different behaviors for successful results?
Image we want to change the status code depending of the Success values is True or False. Now, we have to do it as following;
The two solutions proposed below would have to take into account that these new mappings would be reflected in the static responses function, accessible in this example at: CreateTaskController.responses().
Proposal 1 (success_map)
Add a success_map similar to error_map and map success values.
Refactor the error_map and promote this to responses_map. In the case above will have to define a success alternative class with a domain class (e.g Success(AsynTaskQueued())
classCreateTaskController(FastAPIController):
classConfig:
responses_map= {
DEFAULT: JSONResponse(status_code=200, content={"message": "Task saved"}),
False: JSONResponse(status_code=206, content={"message": "Task will be saved"})
TaskAlreadyCreated: HttpError(status_code=409, detail="Task already created")
TaskDescriptionTooLong: HttpError(status_code=422, detail="Task description is too long")
}
defexecute(self, task: Task) ->BoolResult:
task_creator=TaskCreator(
labeler=Container.get(TaskLabeler),
repository=Container.get(CrudRepository, alias="task_repository"),
domain_event_bus=Container.get(DomainEventBus),
)
returntask_creator.execute(task=task)
The text was updated successfully, but these errors were encountered:
First Check
Commit to Help
Example Code
Description
With petisco you normally define
success_handler
anderror_map
to manage all the paths your use case can take.In the above example:
With
success_handler: lambda result: {"message": "Task saved"}
your program allways will return a 200 (default value) and the payload{"message": "Task saved"}
.In case of failure, it will return
409
or422
depending on theFailure
returned byTaskCreator
:* TaskAlreadyCreated -> 409
* TaskDescriptionTooLong -> 422
But what if you want to have different behaviors for successful results?
Image we want to change the status code depending of the Success values is True or False. Now, we have to do it as following;
The two solutions proposed below would have to take into account that these new mappings would be reflected in the static
responses
function, accessible in this example at:CreateTaskController.responses()
.Proposal 1 (
success_map
)Add a
success_map
similar toerror_map
and map success values.Proposal 2 (response_map)
Refactor the
error_map
and promote this toresponses_map
. In the case above will have to define a success alternative class with a domain class (e.gSuccess(AsynTaskQueued())
The text was updated successfully, but these errors were encountered: