22
33import click
44from click import ClickException
5+ from pydanclick import from_pydantic
56from scim2_client import SCIMClientError
7+ from scim2_models import Context
68from sphinx_click .rst_to_ansi_formatter import make_rst_to_ansi_formatter
79
810from .utils import DOC_URL
11+ from .utils import ModelCommand
912from .utils import formatted_payload
1013from .utils import split_headers
14+ from .utils import unacceptable_fields
1115
1216
13- @click .command (cls = make_rst_to_ansi_formatter (DOC_URL ), name = "replace" )
17+ def replace_payload (client , payload , indent , headers ):
18+ try :
19+ response = client .replace (payload , headers = split_headers (headers ))
20+
21+ except SCIMClientError as scim_exc :
22+ message = str (scim_exc )
23+ if sys .version_info >= (3 , 11 ) and hasattr (
24+ scim_exc , "__notes__"
25+ ): # pragma: no cover
26+ for note in scim_exc .__notes__ :
27+ message = f"{ message } \n { note } "
28+ raise ClickException (message ) from scim_exc
29+
30+ payload = formatted_payload (response .model_dump (), indent )
31+ click .echo (payload )
32+
33+
34+ def replace_factory (model ):
35+ exclude = unacceptable_fields (Context .RESOURCE_REPLACEMENT_REQUEST , model )
36+ exclude .remove ("id" )
37+
38+ @click .command (
39+ cls = make_rst_to_ansi_formatter (DOC_URL ),
40+ name = model .__name__ .lower (),
41+ )
42+ @click .option (
43+ "--indent/--no-indent" ,
44+ is_flag = True ,
45+ default = True ,
46+ help = "Indent JSON response payloads." ,
47+ )
48+ @click .option (
49+ "-h" , "--headers" , multiple = True , help = "Header to pass in the HTTP requests."
50+ )
51+ @from_pydantic ("obj" , model , exclude = exclude )
52+ @click .pass_context
53+ def replace_command (ctx , indent , headers , obj : model , * args , ** kwargs ):
54+ """Perform a `SCIM PUT <https://www.rfc-editor.org/rfc/rfc7644#section-3.3>`_ request
55+ on resources endpoint.
56+
57+ Input data can be passed through parameters like :code:`--external-id`.
58+
59+ .. code-block:: bash
60+
61+ scim https://scim.example replace user --id "xxxx-yyyy" --user-name "foo" --name-given-name "bar"
62+
63+ Multiple attributes should be passed as JSON payloads:
64+
65+ .. code-block:: bash
66+
67+ scim https://scim.example replace user \\
68+ --id "xxxx-yyyy" \\
69+ --user-name "foo" \\
70+ --emails '[{"value":"foo@bar.example", "primary": true}, {"value": "foo@baz.example"}]'
71+
72+ Input can also be passed through stdin in JSON format:
73+
74+ .. code-block:: bash
75+
76+ echo '{"id": "xxxx-yyyy", "userName": "bjensen@example.com", "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"]}' | scim https://scim.example replace user
77+ """
78+
79+ if obj == model ():
80+ obj = None
81+
82+ payload = ctx .obj .get ("stdin" ) or obj
83+ if not payload :
84+ click .echo (ctx .get_help ())
85+ ctx .exit (1 )
86+
87+ replace_payload (ctx .obj ["client" ], payload , indent , headers )
88+
89+ return replace_command
90+
91+
92+ @click .command (
93+ cls = ModelCommand ,
94+ factory = replace_factory ,
95+ name = "replace" ,
96+ invoke_without_command = True ,
97+ )
1498@click .pass_context
1599@click .option (
16100 "-h" , "--headers" , multiple = True , help = "Header to pass in the HTTP requests."
@@ -25,29 +109,26 @@ def replace_cli(ctx, headers, indent):
25109 """Perform a `SCIM PUT <https://www.rfc-editor.org/rfc/rfc7644#section-3.5.1>`_ request
26110 on the resources endpoint.
27111
28- Input data is expected to be passed in JSON format to stdin:
112+ There are subcommands for all the available models, with dynamic attributes.
113+ See the attributes for :code:`user` with:
114+
115+ .. code-block:: bash
116+
117+ scim https://scim.example replace user --help
118+
119+ If no subcommand is executed, input data is expected to be passed in JSON format to stdin:
29120
30121 .. code-block:: bash
31122
32123 echo '{"userName": "bjensen@example.com", "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"], "id": "1234"}' | scim https://scim.example replace user
33124
34125 """
126+ if ctx .invoked_subcommand is not None :
127+ return
35128
36129 payload = ctx .obj .get ("stdin" )
37130 if not payload :
38- raise ClickException ("Input data is missing" )
39-
40- try :
41- response = ctx .obj ["client" ].replace (payload , headers = split_headers (headers ))
42-
43- except SCIMClientError as scim_exc :
44- message = str (scim_exc )
45- if sys .version_info >= (3 , 11 ) and hasattr (
46- scim_exc , "__notes__"
47- ): # pragma: no cover
48- for note in scim_exc .__notes__ :
49- message = f"{ message } \n { note } "
50- raise ClickException (message ) from scim_exc
131+ click .echo (ctx .get_help ())
132+ ctx .exit (1 )
51133
52- payload = formatted_payload (response .model_dump (), indent )
53- click .echo (payload )
134+ replace_payload (ctx .obj ["client" ], payload , indent , headers )
0 commit comments