Skip to content

Commit e450fbb

Browse files
committed
feat: populate broker source from env
Allows the broker source information to be populated from environment variables. This still requires the `broker_source` method to be called: ```python ( Verifier("my-provider") # ... .broker_source() ) ``` This can be disabled by setting the new `use_env` argument to `False` (defaults to True), or by setting the relevant argument to `None` if you want to disable an input. Signed-off-by: JP-Ellis <[email protected]>
1 parent 8fddfc8 commit e450fbb

File tree

1 file changed

+51
-18
lines changed

1 file changed

+51
-18
lines changed

src/pact/verifier.py

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575

7676
import json
7777
import logging
78+
import os
7879
from collections.abc import Mapping
7980
from contextlib import nullcontext
8081
from datetime import date
@@ -87,7 +88,7 @@
8788
import pact_ffi
8889
from pact._server import MessageProducer, StateCallback
8990
from pact._util import apply_args
90-
from pact.types import Message, MessageProducerArgs, StateHandlerArgs
91+
from pact.types import UNSET, Message, MessageProducerArgs, StateHandlerArgs, Unset
9192

9293
if TYPE_CHECKING:
9394
from collections.abc import Iterable
@@ -1114,53 +1115,67 @@ def _add_source_remote(
11141115
@overload
11151116
def broker_source(
11161117
self,
1117-
url: str | URL,
1118+
url: str | URL | Unset = UNSET,
11181119
*,
1119-
username: str | None = None,
1120-
password: str | None = None,
1120+
username: str | Unset = UNSET,
1121+
password: str | Unset = UNSET,
11211122
selector: Literal[False] = False,
1123+
use_env: bool = True,
11221124
) -> Self: ...
11231125

11241126
@overload
11251127
def broker_source(
11261128
self,
1127-
url: str | URL,
1129+
url: str | URL | None | Unset = UNSET,
11281130
*,
1129-
token: str | None = None,
1131+
token: str | None | Unset = UNSET,
11301132
selector: Literal[False] = False,
1133+
use_env: bool = True,
11311134
) -> Self: ...
11321135

11331136
@overload
11341137
def broker_source(
11351138
self,
1136-
url: str | URL,
1139+
url: str | URL | None | Unset = UNSET,
11371140
*,
1138-
username: str | None = None,
1139-
password: str | None = None,
1141+
username: str | None | Unset = UNSET,
1142+
password: str | None | Unset = UNSET,
11401143
selector: Literal[True],
1144+
use_env: bool = True,
11411145
) -> BrokerSelectorBuilder: ...
11421146

11431147
@overload
11441148
def broker_source(
11451149
self,
1146-
url: str | URL,
1150+
url: str | URL | None | Unset = UNSET,
11471151
*,
1148-
token: str | None = None,
1152+
token: str | None | Unset = UNSET,
11491153
selector: Literal[True],
1154+
use_env: bool = True,
11501155
) -> BrokerSelectorBuilder: ...
11511156

1152-
def broker_source(
1157+
def broker_source( # noqa: PLR0913
11531158
self,
1154-
url: str | URL,
1159+
url: str | URL | None | Unset = UNSET,
11551160
*,
1156-
username: str | None = None,
1157-
password: str | None = None,
1158-
token: str | None = None,
1161+
username: str | None | Unset = UNSET,
1162+
password: str | None | Unset = UNSET,
1163+
token: str | None | Unset = UNSET,
11591164
selector: bool = False,
1165+
use_env: bool = True,
11601166
) -> BrokerSelectorBuilder | Self:
11611167
"""
11621168
Adds a broker source to the verifier.
11631169
1170+
If any of the values are `None`, the value will be read from the
1171+
environment variables unless the `use_env` parameter is set to `False`.
1172+
The known variables are:
1173+
1174+
- `PACT_BROKER_BASE_URL` for the `url` parameter.
1175+
- `PACT_BROKER_USERNAME` for the `username` parameter.
1176+
- `PACT_BROKER_PASSWORD` for the `password` parameter.
1177+
- `PACT_BROKER_TOKEN` for the `token` parameter.
1178+
11641179
By default, or if `selector=False`, this function returns the verifier
11651180
instance to allow for method chaining. If `selector=True` is given, this
11661181
function returns a
@@ -1195,22 +1210,40 @@ def broker_source(
11951210
of the broker source and must be finalised with a call to
11961211
[`build()`][pact.verifier.BrokerSelectorBuilder.build].
11971212
1213+
use_env:
1214+
Whether to read missing values from the environment variables.
1215+
This is `True` by default which allows for easy configuration
1216+
from the standard Pact environment variables. In all cases, the
1217+
explicitly provided values take precedence over the environment
1218+
variables.
1219+
11981220
Raises:
11991221
ValueError:
12001222
If mutually exclusive authentication parameters are provided.
12011223
"""
1224+
1225+
def maybe_var(v: Any | Unset, env: str) -> str | None: # noqa: ANN401
1226+
if isinstance(v, Unset):
1227+
return os.getenv(env) if use_env else None
1228+
return v
1229+
1230+
url = maybe_var(url, "PACT_BROKER_BASE_URL")
1231+
if not url:
1232+
msg = "A broker URL must be provided"
1233+
raise ValueError(msg)
12021234
url = URL(url)
12031235

1236+
username = maybe_var(username, "PACT_BROKER_USERNAME")
12041237
if url.user and username:
12051238
msg = "Cannot specify both `username` and a username in the URL"
12061239
raise ValueError(msg)
1207-
username = url.user or username
12081240

1241+
password = maybe_var(password, "PACT_BROKER_PASSWORD")
12091242
if url.password and password:
12101243
msg = "Cannot specify both `password` and a password in the URL"
12111244
raise ValueError(msg)
1212-
password = url.password or password
12131245

1246+
token = maybe_var(token, "PACT_BROKER_TOKEN")
12141247
if token and (username or password):
12151248
msg = "Cannot specify both `token` and `username`/`password`"
12161249
raise ValueError(msg)

0 commit comments

Comments
 (0)