-
Notifications
You must be signed in to change notification settings - Fork 1
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
Lambda api and unification #64
base: master
Are you sure you want to change the base?
Conversation
…lambdalizator into lambda_api � Conflicts: � CHANGELOG.md � lbz/exceptions.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't read the entire pull request yet, but the modules I commented on can be considered checked 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, it looks like I wrote pretty a lot of comments for this PR 😬
I cannot force you but definitely, there are parts of the code that could be implemented in separate smaller pull requests which could speed up the work on this, especially reviewing.
|
||
def decorator(func: Callable) -> Callable: | ||
warnings.simplefilter("always", DeprecationWarning) # turn off filter | ||
warnings.warn( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you put this warning at the wrong level which makes it printed no matter if the function was used or not 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This being done, I think tests are required 😉
CHANGELOG.md
Outdated
|
||
|
||
### Version 0.5.16 | ||
Released 2022-09-09 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should adjust this date before merging 😉
lbz/lambdas/broker.py
Outdated
if (op := self.event.get(self.op_key)) is None: | ||
return lambda_error_response( | ||
result=LambdaResult.BAD_REQUEST, | ||
error="Lambda execution error: Missing 'op' field in the event.", | ||
) | ||
try: | ||
return self.mapper[op] | ||
except KeyError: | ||
return lambda_error_response( | ||
result=LambdaResult.BAD_REQUEST, | ||
error=f"Lambda execution error: No handler implemented for operation: {op}", | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exception is more than welcome in these two situations, otherwise, they can be silently skipped by the code that uses this broker. The final decision on what to do should not be made by lbz 😉
Co-authored-by: Grzegorz Redlicki <[email protected]>
Kudos, SonarCloud Quality Gate passed!
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still, there are some comments from the past that have not been resolved yet, and now, I added new ones. Unfortunately, I'm slowly getting lost here. Too many lines of code, too many comments, and too much time spent on re-reviewing changes 😕
Can you follow my previous suggestion and split this PR? Trust me, slicing a big thing into smaller pieces always speeds up the work even if you don't see it at the first glance (#elephant-carpaccio).
We can start with three pull requests:
- Adding the
BaseHandler
class and the tests. - Adding the
deprecated
decorated and the tests. - Using the above ones to improve the resource and events.
Then, we will return to the whole lambdas
package (client and broker separately). Thanks to that, we will close it faster focusing on things that matter without decreasing my motivation to read your code 😉
def _pre_handle(self) -> None: | ||
self.pre_handle() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new method does not make sense or just I don't see that 🤔
Did you do it to stay consistent with the _post_handle()
method?
return lambda_error_response( | ||
result=LambdaResult.BAD_REQUEST, | ||
error_message="Lambda execution error: Missing 'op' field in the event.", | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only place where lambda_error_response
is used which is inconsistent with the rest of the code here and also with the comment I wrote before: #977515342 🤔
I think we need to talk about this Broker class - without good examples of what you want to achieve (tests), my review may be unfortunately inappropriate...
from lbz.lambdas.enums import LambdaResult | ||
from lbz.misc import get_logger | ||
|
||
from .exceptions import LambdaError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I remember, we don't use relative imports in lambdalizator 🤔
Maybe we should but this is not a good pull request to start doing it in one place.
from .broker import LambdaBroker | ||
from .client import LambdaClient | ||
from .enums import LambdaResult, LambdaSource | ||
from .exceptions import LambdaError | ||
from .response import LambdaResponse, lambda_error_response, lambda_ok_response |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same here, you should use absolute imports instead.
if lambda_result in allowed_error_results: | ||
return response | ||
if raise_if_error_resp: | ||
raise LambdaError(function_name, op, lambda_result, response) # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should resolve the problem instead of disabling type checking (rel. #965942904) 😉
class LambdaResponse(TypedDict, total=False): | ||
result: str | ||
data: Optional[Any] | ||
message: Optional[str] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional
here does not refer to the presence of a specific key - it just means that the value may be None
.
Do we want to allow passing None
as the message? 🤔
|
||
class LambdaResponse(TypedDict, total=False): | ||
result: str | ||
data: Optional[Any] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also here, Any
means anything (including None
) 😉
data: Optional[Any] | |
data: Any |
That will be used when Authorization Header is not in place. | ||
""" | ||
return {} | ||
def _pre_handle(self) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't overwrite the protected superclass method - things like that add to the complexity of the entire flow 😐
I know what problem this code is solving but generally speaking, you want to change the default behavior of the react()
method, so, you should overwrite the react()
method itself.
raise NotFound | ||
if self.method not in self._router[self.path]: | ||
raise UnsupportedMethod(method=self.method) | ||
def validate_path_and_method(self) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Helpers that are not needed outside the class should be protected and placed at the bottom.
However, I am not sure why you did this unnecessary refactoring now (there are already almost 1k of added lines in this PR).
assert resp == "some_data" | ||
func_1.assert_called_once_with({"y": 1}) | ||
|
||
def test_broker_responds_raises_implemented_when_event_type_is_not_recognized(self) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make this sentence more correct:
def test_broker_responds_raises_implemented_when_event_type_is_not_recognized(self) -> None: | |
def test_broker_raises_error_when_event_type_is_not_recognized(self) -> None: |
No description provided.