Skip to content

Commit 410f260

Browse files
committed
feat: add support for custom headers to the requests
1 parent 9e9d642 commit 410f260

8 files changed

Lines changed: 48 additions & 9 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Check the [reference](https://scim2-cli.readthedocs.io/en/latest/reference.html)
1515
Here is an example of resource creation:
1616

1717
```shell
18-
$ scim2 https://auth.example create user << EOL
18+
$ scim2 https://auth.example --header "Authorization: Bearer 12345" create user << EOL
1919
{
2020
"schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
2121
"userName": "bjensen@example.com"
@@ -38,7 +38,7 @@ EOL
3838
Here is an example of resource query:
3939

4040
```shell
41-
$ scim2 https://auth.example query user 2819c223-7f76-453a-919d-413861904646
41+
$ scim2 https://auth.example --header "Authorization: Bearer 12345" query user 2819c223-7f76-453a-919d-413861904646
4242
{
4343
"schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
4444
"id": "2819c223-7f76-453a-919d-413861904646",

doc/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changelog
22
=========
33

4+
Added
5+
^^^^^
6+
- Add support for passing custom headers to requests.
7+
48
[0.1.1] - 2024-06-02
59
--------------------
610

scim2_cli/create.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from .utils import DOC_URL
99
from .utils import formatted_payload
10+
from .utils import split_headers
1011

1112

1213
@click.command(cls=make_rst_to_ansi_formatter(DOC_URL), name="create")
@@ -17,7 +18,10 @@
1718
default=True,
1819
help="Indent JSON response payloads.",
1920
)
20-
def create_cli(ctx, indent):
21+
@click.option(
22+
"-h", "--headers", multiple=True, help="Header to pass in the HTTP requests."
23+
)
24+
def create_cli(ctx, indent, headers):
2125
"""Perform a `SCIM POST <https://www.rfc-editor.org/rfc/rfc7644#section-3.3>`_ request
2226
on resources endpoint.
2327
@@ -34,7 +38,7 @@ def create_cli(ctx, indent):
3438
raise ClickException("Input data is missing")
3539

3640
try:
37-
response = ctx.obj["client"].create(payload)
41+
response = ctx.obj["client"].create(payload, headers=split_headers(headers))
3842

3943
except (httpx.HTTPError, SCIMClientError) as exc:
4044
raise ClickException(exc) from exc

scim2_cli/delete.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@
66

77
from .utils import DOC_URL
88
from .utils import formatted_payload
9+
from .utils import split_headers
910

1011

1112
@click.command(cls=make_rst_to_ansi_formatter(DOC_URL), name="delete")
1213
@click.argument("resource-type", required=True)
1314
@click.argument("id", required=True)
15+
@click.option(
16+
"-h", "--headers", multiple=True, help="Header to pass in the HTTP requests."
17+
)
1418
@click.option(
1519
"--indent/--no-indent",
1620
is_flag=True,
1721
default=True,
1822
help="Indent JSON response payloads.",
1923
)
2024
@click.pass_context
21-
def delete_cli(ctx, resource_type, id, indent):
25+
def delete_cli(ctx, resource_type, id, headers, indent):
2226
"""Perform a `SCIM DELETE query <https://www.rfc-editor.org/rfc/rfc7644#section-3.6>`_ request.
2327
2428
.. code-block:: bash
@@ -35,7 +39,9 @@ def delete_cli(ctx, resource_type, id, indent):
3539
)
3640

3741
try:
38-
response = ctx.obj["client"].delete(resource_type, id)
42+
response = ctx.obj["client"].delete(
43+
resource_type, id, headers=split_headers(headers)
44+
)
3945

4046
except (httpx.HTTPError, SCIMClientError) as exc:
4147
raise ClickException(exc) from exc

scim2_cli/query.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from .utils import DOC_URL
1313
from .utils import formatted_payload
14+
from .utils import split_headers
1415

1516

1617
@click.command(cls=make_rst_to_ansi_formatter(DOC_URL), name="query")
@@ -48,6 +49,9 @@
4849
"--sort-order",
4950
help="A string indicating the order in which the “sortBy” parameter is applied.",
5051
)
52+
@click.option(
53+
"-h", "--headers", multiple=True, help="Header to pass in the HTTP requests."
54+
)
5155
@click.option(
5256
"--indent/--no-indent",
5357
is_flag=True,
@@ -65,6 +69,7 @@ def query_cli(
6569
filter: str,
6670
sort_by: str,
6771
sort_order: str,
72+
headers: List[str],
6873
indent: bool,
6974
):
7075
"""Perform a `SCIM GET <https://www.rfc-editor.org/rfc/rfc7644#section-3.4.1>`_ request
@@ -113,11 +118,14 @@ def query_cli(
113118
id,
114119
search_request=payload,
115120
check_request_payload=check_request_payload,
121+
headers=split_headers(headers),
116122
)
117123

118124
else:
119125
response = ctx.obj["client"].query_all(
120-
search_request=payload, check_request_payload=check_request_payload
126+
search_request=payload,
127+
check_request_payload=check_request_payload,
128+
headers=split_headers(headers),
121129
)
122130

123131
except (httpx.HTTPError, SCIMClientError) as exc:

scim2_cli/replace.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@
77

88
from .utils import DOC_URL
99
from .utils import formatted_payload
10+
from .utils import split_headers
1011

1112

1213
@click.command(cls=make_rst_to_ansi_formatter(DOC_URL), name="replace")
1314
@click.pass_context
15+
@click.option(
16+
"-h", "--headers", multiple=True, help="Header to pass in the HTTP requests."
17+
)
1418
@click.option(
1519
"--indent/--no-indent",
1620
is_flag=True,
1721
default=True,
1822
help="Indent JSON response payloads.",
1923
)
20-
def replace_cli(ctx, indent):
24+
def replace_cli(ctx, headers, indent):
2125
"""Perform a `SCIM PUT <https://www.rfc-editor.org/rfc/rfc7644#section-3.5.1>`_ request
2226
on the resources endpoint.
2327
@@ -34,7 +38,7 @@ def replace_cli(ctx, indent):
3438
raise ClickException("Input data is missing")
3539

3640
try:
37-
response = ctx.obj["client"].replace(payload)
41+
response = ctx.obj["client"].replace(payload, headers=split_headers(headers))
3842

3943
except (httpx.HTTPError, SCIMClientError) as exc:
4044
raise ClickException(exc) from exc

scim2_cli/search.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from .utils import DOC_URL
1212
from .utils import formatted_payload
13+
from .utils import split_headers
1314

1415

1516
@click.command(cls=make_rst_to_ansi_formatter(DOC_URL), name="search")
@@ -45,6 +46,9 @@
4546
"--sort-order",
4647
help="A string indicating the order in which the “sortBy” parameter is applied.",
4748
)
49+
@click.option(
50+
"-h", "--headers", multiple=True, help="Header to pass in the HTTP requests."
51+
)
4852
@click.option(
4953
"--indent/--no-indent",
5054
is_flag=True,
@@ -60,6 +64,7 @@ def search_cli(
6064
filter: str,
6165
sort_by: str,
6266
sort_order: str,
67+
headers: List[str],
6368
indent: bool,
6469
):
6570
"""Perform a `SCIM GET <https://www.rfc-editor.org/rfc/rfc7644#section-3.4.1>`_ request
@@ -92,6 +97,7 @@ def search_cli(
9297
response = ctx.obj["client"].search(
9398
search_request=payload,
9499
check_request_payload=check_request_payload,
100+
headers=split_headers(headers),
95101
)
96102

97103
except (httpx.HTTPError, SCIMClientError) as exc:

scim2_cli/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@
77
def formatted_payload(obj, indent):
88
indent = INDENTATION_SIZE if indent else False
99
return json.dumps(obj, indent=indent)
10+
11+
12+
def split_headers(headers):
13+
return {
14+
header[: header.index(":")].strip(): header[header.index(":") + 1 :].strip()
15+
for header in headers
16+
}

0 commit comments

Comments
 (0)