bibajz / bitcoin-python-async-rpc

Minimal Bitcoin JSON-RPC Python asynchronous client
MIT License
33 stars 13 forks source link

Rework instantiation of `BitcoinRPC` #16

Closed bibajz closed 1 year ago

bibajz commented 1 year ago

As was already noted here - https://github.com/bibajz/bitcoin-python-async-rpc/pull/10#issuecomment-1001560293 - the way to instantiate BitcoinRPC is quite clunky.

It allows a user to instantiate the underlying httpx.AsyncClient with a auth tuple (which is btw mandatory, you cannot create a BitcoinRPC to connect to a server not requiring user/passwd!) and then somewhat awkwardly headers and other options...

To better accomodate for more advanced configurations of the httpx.AsyncClient (proxy support, connection pooling, timeouts - https://github.com/bibajz/bitcoin-python-async-rpc/issues/13#issuecomment-1510504359 ), BitcoinRPC should be instantiated with an argument client: httpx.AsyncClient.

However, to preserve the ease of one-shot testing and shield the users from dealing with the underlying httpx library, an option to instantiate the BitcoinRPC with (url, rpc_user, rpc_password) should be preserved.

Sketched roughly:

class BitcoinRPC:
    def __init__(self, url: str, client: httpx.AsyncClient):
        self._url = url
        self._client = client
        ...

    @classmethod
    def from_url(cls, url, rpc_user, rpc_password):
        client = setup_client_with_sane_defaults(rpc_user, rpc_password)
        return cls(url, client)

    ...

NOTE: This will result in a BREAKING change.