|
12 | 12 | from click.core import ParameterSource |
13 | 13 | from scim2_client import SCIMClientException |
14 | 14 | from scim2_models import BaseModel |
| 15 | +from scim2_models import BulkResponse |
15 | 16 | from scim2_models import Context |
| 17 | +from scim2_models import Error |
16 | 18 | from scim2_models import Mutability |
17 | 19 | from scim2_models import Resource |
18 | 20 | from scim2_models import ResourceType |
@@ -129,6 +131,30 @@ def formatted_payload(obj: Any, indent: bool) -> str: |
129 | 131 | return json.dumps(obj, indent=INDENTATION_SIZE if indent else None) |
130 | 132 |
|
131 | 133 |
|
| 134 | +def echo_response(response: Any, indent: bool) -> None: |
| 135 | + """Display the response of the server, and exit with an error code if it reports a failure.""" |
| 136 | + if response is None: |
| 137 | + return |
| 138 | + |
| 139 | + click.echo(formatted_payload(response.model_dump(), indent)) |
| 140 | + if isinstance(response, Error): |
| 141 | + summary = " ".join( |
| 142 | + str(part) for part in (response.status, response.detail) if part |
| 143 | + ) |
| 144 | + click.echo(f"Error: {escape_control_characters(summary)}", err=True) |
| 145 | + click.get_current_context().exit(1) |
| 146 | + |
| 147 | + if isinstance(response, BulkResponse): |
| 148 | + operations = response.operations or [] |
| 149 | + failed = [op for op in operations if op.status and op.status >= 400] |
| 150 | + if failed: |
| 151 | + click.echo( |
| 152 | + f"Error: {len(failed)} of {len(operations)} bulk operations failed", |
| 153 | + err=True, |
| 154 | + ) |
| 155 | + click.get_current_context().exit(1) |
| 156 | + |
| 157 | + |
132 | 158 | def split_headers(headers: list[str]) -> dict[str, str]: |
133 | 159 | """Make a dict from header strings. |
134 | 160 |
|
|
0 commit comments