Coverage for src/nats_contrib/connect_opts/connect.py: 100%
12 statements
« prev ^ index » next coverage.py v7.4.2, created at 2024-02-26 14:37 +0100
« prev ^ index » next coverage.py v7.4.2, created at 2024-02-26 14:37 +0100
1from __future__ import annotations
3from typing import TypeVar, overload
5from nats.aio.client import Client
7from .connect_opts import ConnectOption, ConnectOpts
9CT = TypeVar("CT", bound=Client)
12@overload
13async def connect(
14 *opt: ConnectOption,
15 client: None = None,
16) -> Client: ...
19@overload
20async def connect(
21 *opt: ConnectOption,
22 client: CT,
23) -> CT: ...
26async def connect(
27 *opt: ConnectOption,
28 client: Client | None = None,
29) -> Client:
30 """Connect to a NATS server using the provided options.
32 Args:
33 *opt: Options to use when connecting to the NATS server.
35 Returns:
36 Client: The connected client.
38 Example:
40 ```python
41 from nats.aio.connect import connect, WithServers, WithUserPassword
43 client = await connect(
44 WithServer("nats://localhost:4222""),
45 )
46 ```
47 """
48 opts = ConnectOpts()
49 for o in opt:
50 o(opts)
51 nc = client or Client()
52 await nc.connect(**opts.to_dict())
53 return nc