|
75 | 75 |
|
76 | 76 | import json |
77 | 77 | import logging |
| 78 | +import os |
78 | 79 | from collections.abc import Mapping |
79 | 80 | from contextlib import nullcontext |
80 | 81 | from datetime import date |
|
87 | 88 | import pact_ffi |
88 | 89 | from pact._server import MessageProducer, StateCallback |
89 | 90 | from pact._util import apply_args |
90 | | -from pact.types import Message, MessageProducerArgs, StateHandlerArgs |
| 91 | +from pact.types import UNSET, Message, MessageProducerArgs, StateHandlerArgs, Unset |
91 | 92 |
|
92 | 93 | if TYPE_CHECKING: |
93 | 94 | from collections.abc import Iterable |
@@ -1114,53 +1115,67 @@ def _add_source_remote( |
1114 | 1115 | @overload |
1115 | 1116 | def broker_source( |
1116 | 1117 | self, |
1117 | | - url: str | URL, |
| 1118 | + url: str | URL | Unset = UNSET, |
1118 | 1119 | *, |
1119 | | - username: str | None = None, |
1120 | | - password: str | None = None, |
| 1120 | + username: str | Unset = UNSET, |
| 1121 | + password: str | Unset = UNSET, |
1121 | 1122 | selector: Literal[False] = False, |
| 1123 | + use_env: bool = True, |
1122 | 1124 | ) -> Self: ... |
1123 | 1125 |
|
1124 | 1126 | @overload |
1125 | 1127 | def broker_source( |
1126 | 1128 | self, |
1127 | | - url: str | URL, |
| 1129 | + url: str | URL | None | Unset = UNSET, |
1128 | 1130 | *, |
1129 | | - token: str | None = None, |
| 1131 | + token: str | None | Unset = UNSET, |
1130 | 1132 | selector: Literal[False] = False, |
| 1133 | + use_env: bool = True, |
1131 | 1134 | ) -> Self: ... |
1132 | 1135 |
|
1133 | 1136 | @overload |
1134 | 1137 | def broker_source( |
1135 | 1138 | self, |
1136 | | - url: str | URL, |
| 1139 | + url: str | URL | None | Unset = UNSET, |
1137 | 1140 | *, |
1138 | | - username: str | None = None, |
1139 | | - password: str | None = None, |
| 1141 | + username: str | None | Unset = UNSET, |
| 1142 | + password: str | None | Unset = UNSET, |
1140 | 1143 | selector: Literal[True], |
| 1144 | + use_env: bool = True, |
1141 | 1145 | ) -> BrokerSelectorBuilder: ... |
1142 | 1146 |
|
1143 | 1147 | @overload |
1144 | 1148 | def broker_source( |
1145 | 1149 | self, |
1146 | | - url: str | URL, |
| 1150 | + url: str | URL | None | Unset = UNSET, |
1147 | 1151 | *, |
1148 | | - token: str | None = None, |
| 1152 | + token: str | None | Unset = UNSET, |
1149 | 1153 | selector: Literal[True], |
| 1154 | + use_env: bool = True, |
1150 | 1155 | ) -> BrokerSelectorBuilder: ... |
1151 | 1156 |
|
1152 | | - def broker_source( |
| 1157 | + def broker_source( # noqa: PLR0913 |
1153 | 1158 | self, |
1154 | | - url: str | URL, |
| 1159 | + url: str | URL | None | Unset = UNSET, |
1155 | 1160 | *, |
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, |
1159 | 1164 | selector: bool = False, |
| 1165 | + use_env: bool = True, |
1160 | 1166 | ) -> BrokerSelectorBuilder | Self: |
1161 | 1167 | """ |
1162 | 1168 | Adds a broker source to the verifier. |
1163 | 1169 |
|
| 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 | +
|
1164 | 1179 | By default, or if `selector=False`, this function returns the verifier |
1165 | 1180 | instance to allow for method chaining. If `selector=True` is given, this |
1166 | 1181 | function returns a |
@@ -1195,22 +1210,40 @@ def broker_source( |
1195 | 1210 | of the broker source and must be finalised with a call to |
1196 | 1211 | [`build()`][pact.verifier.BrokerSelectorBuilder.build]. |
1197 | 1212 |
|
| 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 | +
|
1198 | 1220 | Raises: |
1199 | 1221 | ValueError: |
1200 | 1222 | If mutually exclusive authentication parameters are provided. |
1201 | 1223 | """ |
| 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) |
1202 | 1234 | url = URL(url) |
1203 | 1235 |
|
| 1236 | + username = maybe_var(username, "PACT_BROKER_USERNAME") |
1204 | 1237 | if url.user and username: |
1205 | 1238 | msg = "Cannot specify both `username` and a username in the URL" |
1206 | 1239 | raise ValueError(msg) |
1207 | | - username = url.user or username |
1208 | 1240 |
|
| 1241 | + password = maybe_var(password, "PACT_BROKER_PASSWORD") |
1209 | 1242 | if url.password and password: |
1210 | 1243 | msg = "Cannot specify both `password` and a password in the URL" |
1211 | 1244 | raise ValueError(msg) |
1212 | | - password = url.password or password |
1213 | 1245 |
|
| 1246 | + token = maybe_var(token, "PACT_BROKER_TOKEN") |
1214 | 1247 | if token and (username or password): |
1215 | 1248 | msg = "Cannot specify both `token` and `username`/`password`" |
1216 | 1249 | raise ValueError(msg) |
|
0 commit comments