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

feat(BA-759): Revamp api handler #3714

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Conversation

fregataa
Copy link
Member

@fregataa fregataa commented Feb 16, 2025

resolves #3713 (BA-759)

Checklist: (if applicable)

  • Milestone metadata specifying the target backport version
  • Mention to the original issue

@fregataa fregataa self-assigned this Feb 16, 2025
@github-actions github-actions bot added comp:common Related to Common component size:L 100~500 LoC labels Feb 16, 2025
@fregataa fregataa added this to the 25Q1 milestone Feb 16, 2025
@fregataa fregataa marked this pull request as ready for review February 17, 2025 04:55
@@ -156,6 +184,7 @@ def status_code(self) -> int:


_ParamType: TypeAlias = BodyParam | QueryParam | PathParam | HeaderParam | MiddlewareParam
_ValidatorType: TypeAlias = BodyParam | QueryParam | PathParam | HeaderParam | type[MiddlewareParam]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a question, what is the difference between two?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can initialize all ...Param types before evaluating request data except MiddlewareParam, which requires input data for initializing. _ValidatorType represents the types that can evaluate and validate input data.

signature_validator_map: dict[str, _ValidatorType] = {}
for name, param in signature.parameters.items():
if param.annotation is inspect.Parameter.empty:
raise ValueError(f"Empty type hint is not allowed (handler:{handler_name},name:{name})")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defining and using a custom error rather than simply using ValueError would be better for future error convention modification

Copy link
Member Author

@fregataa fregataa Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error occurs when bootstrapping the server, not on runtime if any API handler function is defined incorrectly. You can test it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an exception for this case

Comment on lines +318 to +319
match param_instance_or_class:
case PathParam() | BodyParam() | HeaderParam() | QueryParam():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to ask why you changed from comparing classes to comparing instances?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is recommended to use pattern matching rather than using multiple isinstance() if it is possible
https://peps.python.org/pep-0636/#adding-a-ui-matching-objects

Comment on lines 324 to 330
case _:
try:
param_instance = param_instance_or_class.from_request(request)
except ValidationError:
raise MiddlewareParamParsingFailed(
f"Failed while parsing {param_instance_or_class}"
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there might be some confusion because MiddlewareParamParsingFailed is thrown in all cases of non_ValidatorType - are we trusting the type checker to assume a non_ValidatorType class or instance is coming in?

Copy link
Member Author

@fregataa fregataa Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As type checkers check, only ValidatorType input is allowed here. Thats why we use type checkers.

@fregataa fregataa requested a review from seedspirit February 17, 2025 12:22
@fregataa fregataa marked this pull request as draft February 20, 2025 11:24
@fregataa fregataa marked this pull request as ready for review February 21, 2025 04:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:common Related to Common component size:L 100~500 LoC
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Revamp 'api_handler()'
3 participants