GithubHelp home page GithubHelp logo

frequenz-floss / frequenz-client-base-python Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 3.0 1.57 MB

Base gRPC client

Home Page: https://frequenz-floss.github.io/frequenz-client-base-python/

License: MIT License

Python 100.00%
client frequenz grpc lib library python

frequenz-client-base-python's Introduction

Frequenz Client Base Library

Build Status PyPI Package Docs

Introduction

Utilities for writing Frequenz API clients

Supported Platforms

The following platforms are officially supported (tested):

  • Python: 3.11
  • Operating System: Ubuntu Linux 20.04
  • Architectures: amd64, arm64

Contributing

If you want to know how to build this project and contribute to it, please check out the Contributing Guide.

frequenz-client-base-python's People

Contributors

llucax avatar shsms avatar dependabot[bot] avatar marenz avatar leandro-lucarella-frequenz avatar

Watchers

 avatar  avatar  avatar

Forkers

shsms llucax marenz

frequenz-client-base-python's Issues

Add wrappers for gRPC errors

What's needed?

We need to make sure gRPC errors are properly wrapped so we don't leak the use of grpc/protobuf to client users.

From frequenz-floss/frequenz-api-dispatch#144:

gRPC errors in Python are raised as grpc.RpcError exceptions. Here's how to handle them:

import grpc

try:
    response = await self._stub.GetMicrogridDispatch(request)
    # Process the successful response here

except grpc.RpcError as error:
    status_code = error.code()  # Get the gRPC status code
    details = error.details()   # Optional error details as a string 

    # Example of handling based on status code
    if status_code == grpc.StatusCode.NOT_FOUND:
        print("Resource not found!")
    elif status_code == grpc.StatusCode.PERMISSION_DENIED:
        print("Insufficient permissions")
    else: 
        print(f"Error occurred: {status_code} - {details}")

Proposed solution

Add wrapper error classes for the relevant gRPC errors raised by clients.

Some of these errors should inherit from specific Python built-in errors, like INVALID_ARGUMENT should inherit from ValueError, NOT_FOUND and ALREADY_EXISTS should probably inherit from KeyError, etc.

For errors that should never occur (because the client is doing some sanity checks to prevent them from happening), we should probably still have a wrapper in the client, like UnexpectedError, because a server could always misbehave for some reason.

When raising these errors in the client, we can still attach the original gRPC error using the raise OurClientError(...) from grpc_error syntax, which could be very useful for debugging purposes.

Additional context

Create a `GrpcApiClient` base class

What's needed?

We need to standarize how we create clients, how to pass the destination to connect too, and how we close/cleanup the clients.

Proposed solution

Create a new base class called GrpcApiClient with at least some basic functionality, like:

class GrpcApiClient(Generic[T]):
        def __init__(self, target: str, create_stub: Callable[[GrpcChannel], T]):
            self._target = target
            self._channel = grpcaio.insecure_channel(target)
            self._stub: T = create_stub(self._grpc_channel)

    async def close(self, timeout: timedelta | None = None) -> None:
        await self._channel.close(grace=timeout.total_seconds() if timeout else None)

    async def __aenter__(self) -> "ReportingClient":
        return self

    async def __aexit__(
        self,
        _exc_type: Type[BaseException] | None,
        _exc_val: BaseException | None,
        _exc_tb: Any | None,
    ) -> bool | None:
        await self.close()
        return None

Use cases

No response

Alternatives and workarounds

Some clients take a grpc channel, and some a connection string. The advantage of taking a channel is users can chose which type of channel to use (insecure vs. secure for example), the disadvantage is we'll leak the dependency to grpc.

We could extend the connection string to chose if the channel should be secure or insecure, so we could have the best of both world. This would also enable to take connection strings directly from configuration files.

We might need a bit more research, maybe something like that already exists.

Additional context

Investigate and add tools to mock the grpc service to write unit tests

What's needed?

We need to be able to easily and reliably write unit tests for all API clients without having to spin up a server (real or mocked) for the tests.

Proposed solution

Add some utilities to mock the main grpc service, so we can somehow inject replies it should provide when some request is sent.

Use cases

No response

Alternatives and workarounds

Long term we could explore doing a more capable framework, [pytest-httpserver](https://pytest-httpserver.readthedocs.io/en/latest/ or even )like vcr (maybe it can be extended?) to record real responses from the server and then replay them in unit tests.

Additional context

No response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.