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

Add a function to call gRPC stubs and wrap errors #58

Merged
merged 6 commits into from
Jun 13, 2024

Conversation

llucax
Copy link
Contributor

@llucax llucax commented Jun 4, 2024

The new function call_stub_method() simplifies the common error wrapping when calling stub methods, so errors are automatically converted to ApiClientErrors. It also checks that the client is connected and raises an appropriate exception if it is not. Finally, if a transform function is provided, the response will be automatically transformed too.

This PR also adds an example to the BaseApiClient documentation, including using the new call_stub_method() function.

@llucax llucax requested a review from a team as a code owner June 4, 2024 10:04
@llucax llucax requested a review from Marenz June 4, 2024 10:04
@llucax llucax self-assigned this Jun 4, 2024
@llucax llucax requested a review from shsms June 4, 2024 10:04
@github-actions github-actions bot added part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:code Affects the code in general labels Jun 4, 2024
@llucax llucax added this to the v0.5.0 milestone Jun 4, 2024
@llucax llucax added the type:enhancement New feature or enhancement visitble to users label Jun 4, 2024
@llucax
Copy link
Contributor Author

llucax commented Jun 4, 2024

Not sure what's going on with the DCO app that is not running the check 😒

) -> ExampleResponseWrapper:
return await call_stub_method(
self,
lambda: self.stub.example_method(
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 suggest to use explicit parameter names here to avoid having ahem the reader accidentally thinking it is a parameter name 😇

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haha, too much rust going on? 😛

Will do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking at the code I'm not so convinced now. The function is called "call stub method", isn't it pretty obvious that what you are passing is... ahem, the stub method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thinks forcing to use:

call_stub_method(self, stub_method=lambda: self.stub.example_method())

Might be a bit too verbose.

This comment was marked as off-topic.

Marenz
Marenz previously approved these changes Jun 12, 2024
Copy link
Contributor

@Marenz Marenz left a comment

Choose a reason for hiding this comment

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

Made an optional suggestion

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
The name `auto_connect` could be misleading, as users might think that,
for example, the client might connect automatically when a method is
called when this option is `True`, but this option only controls if the
client is connected when it is created.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
The new function `call_stub_method()` simplifies the common error
wrapping when calling stub methods, so errors are automatically
converted to `ApiClientError`s. It also checks that the client is
connected and raises an appropriate exception if it is not. Finally, if
a transform function is provided, the response will be automatically
transformed too.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
@llucax
Copy link
Contributor Author

llucax commented Jun 12, 2024

Updated with @shsms suggestion.

@llucax llucax requested review from shsms and Marenz June 12, 2024 10:49
@llucax llucax enabled auto-merge June 12, 2024 10:49
@llucax
Copy link
Contributor Author

llucax commented Jun 12, 2024

As @Marenz comment was also optional I'm enabling auto-merge.

@llucax llucax added this pull request to the merge queue Jun 13, 2024
Merged via the queue into frequenz-floss:v0.x.x with commit 6a42779 Jun 13, 2024
14 checks passed
@llucax llucax deleted the base-client-call-stub branch June 13, 2024 07:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
part:code Affects the code in general part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests type:enhancement New feature or enhancement visitble to users
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants