Skip to content

Commit 512c991

Browse files
committed
feat: allow setting host and headers with environment vars
1 parent c1474f5 commit 512c991

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,13 @@ dev = [
4949
"pytest-httpserver>=1.0.10",
5050
"tox-uv>=1.16.0",
5151
]
52-
5352
doc = [
5453
"shibuya>=2024.5.15",
5554
"sphinx>=7.3.7",
5655
"sphinx-click>=6.0.0",
5756
"sphinx-issues >= 5.0.0",
5857
"myst-parser>=3.0.1",
5958
]
60-
6159
bundle = [
6260
"pyinstaller>=6.10.0",
6361
]

scim2_cli/__init__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from scim2_cli.search import search_cli
1818
from scim2_cli.test import test_cli
1919
from scim2_cli.utils import DOC_URL
20+
from scim2_cli.utils import HeaderType
2021

2122
from .utils import split_headers
2223

@@ -40,16 +41,22 @@ def _is_pydantic_model(model: Any) -> TypeGuard[type[BaseModel]]:
4041

4142

4243
@click.group(cls=make_rst_to_ansi_formatter(DOC_URL, group=True))
43-
@click.option("--url", help="The SCIM server endpoint.")
44+
@click.option("--url", help="The SCIM server endpoint.", envvar="SCIM_CLI_URL")
4445
@click.option(
45-
"-h", "--headers", multiple=True, help="Header to pass in the HTTP requests."
46+
"-h",
47+
"--headers",
48+
multiple=True,
49+
type=HeaderType(),
50+
help="Header to pass in the HTTP requests.",
51+
envvar="SCIM_CLI_HEADERS",
4652
)
4753
@click.pass_context
4854
def cli(ctx, url: str, headers: list[str]):
4955
"""SCIM application development CLI."""
5056
ctx.ensure_object(dict)
5157
ctx.obj["URL"] = url
52-
client = Client(base_url=ctx.obj["URL"], headers=split_headers(headers))
58+
headers_dict = split_headers(headers)
59+
client = Client(base_url=ctx.obj["URL"], headers=headers_dict)
5360
ctx.obj["client"] = SyncSCIMClient(client, resource_models=(User, Group))
5461
ctx.obj["client"].register_naive_resource_types()
5562
ctx.obj["resource_models"] = {

scim2_cli/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
INDENTATION_SIZE = 4
99

1010

11+
class HeaderType(click.ParamType):
12+
envvar_list_splitter = ";"
13+
name = "HEADER"
14+
15+
1116
class Color(str, Enum):
1217
black = "black"
1318
red = "red"

tests/test_cli.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
from scim2_cli import cli
24

35

@@ -53,3 +55,26 @@ def test_auth_headers(runner, httpserver, simple_user_payload):
5355
],
5456
)
5557
assert result.exit_code == 0
58+
59+
60+
def test_env_vars(runner, httpserver, simple_user_payload):
61+
"""Test passing host and headers with environment vars."""
62+
httpserver.expect_request(
63+
"/Users/foobar", method="GET", headers={"Authorization": "Bearer token"}
64+
).respond_with_json(
65+
simple_user_payload("foobar"),
66+
status=200,
67+
content_type="application/scim+json",
68+
)
69+
70+
result = runner.invoke(cli, ["query", "user"])
71+
assert result.exit_code == 1
72+
73+
os.environ["SCIM_CLI_URL"] = httpserver.url_for("/")
74+
os.environ["SCIM_CLI_HEADERS"] = "Authorization: Bearer token;foo: bar"
75+
try:
76+
result = runner.invoke(cli, ["query", "user", "foobar"], catch_exceptions=False)
77+
assert result.exit_code == 0
78+
finally:
79+
del os.environ["SCIM_CLI_URL"]
80+
del os.environ["SCIM_CLI_HEADERS"]

0 commit comments

Comments
 (0)