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

Lambda api and unification #64

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

Lambda api and unification #64

wants to merge 8 commits into from

Conversation

pdyba
Copy link
Owner

@pdyba pdyba commented Aug 25, 2022

No description provided.

Copy link
Collaborator

@redlickigrzegorz redlickigrzegorz left a 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 😉

Copy link
Collaborator

@redlickigrzegorz redlickigrzegorz left a 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(
Copy link
Collaborator

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 🤔

Copy link
Collaborator

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
Copy link
Collaborator

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 😉

Comment on lines 40 to 51
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}",
)
Copy link
Collaborator

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 😉

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 6 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Copy link
Collaborator

@redlickigrzegorz redlickigrzegorz left a 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:

  1. Adding the BaseHandler class and the tests.
  2. Adding the deprecated decorated and the tests.
  3. 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 😉

Comment on lines +32 to +33
def _pre_handle(self) -> None:
self.pre_handle()
Copy link
Collaborator

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?

Comment on lines +35 to +38
return lambda_error_response(
result=LambdaResult.BAD_REQUEST,
error_message="Lambda execution error: Missing 'op' field in the event.",
)
Copy link
Collaborator

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
Copy link
Collaborator

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.

Comment on lines +1 to +5
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
Copy link
Collaborator

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
Copy link
Collaborator

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]
Copy link
Collaborator

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]
Copy link
Collaborator

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) 😉

Suggested change
data: Optional[Any]
data: Any

That will be used when Authorization Header is not in place.
"""
return {}
def _pre_handle(self) -> None:
Copy link
Collaborator

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:
Copy link
Collaborator

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:
Copy link
Collaborator

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:

Suggested change
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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants