Skip to content

Commit 9a4ab09

Browse files
committed
feat: Add check_status_codes and check_response_types SCIMClient params.
1 parent 062aaa9 commit 9a4ab09

File tree

2 files changed

+63
-42
lines changed

2 files changed

+63
-42
lines changed

doc/changelog.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ Changelog
77
Added
88
^^^^^
99
- :class:`~scim2_client.client.BaseSyncSCIMClient.discover` has parameters to select which objects to discover.
10-
10+
- Add :paramref:`~scim2_client.SCIMClient.check_response_content_type` and
11+
:paramref:`~scim2_client.SCIMClient.check_response_status_codes` parameters.
1112

1213
[0.4.1] - 2024-12-02
1314
--------------------

scim2_client/client.py

Lines changed: 61 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class SCIMClient:
6060
This value can be overwritten in methods.
6161
:param check_response_payload: Whether to validate that the response payloads are valid.
6262
If set, the raw payload will be returned. This value can be overwritten in methods.
63+
:param check_response_content_type: Whether to validate that the response content types are valid.
64+
:param check_response_status_codes: Whether to validate that the response status codes are valid.
6365
:param raise_scim_errors: If :data:`True` and the server returned an
6466
:class:`~scim2_models.Error` object during a request, a :class:`~scim2_client.SCIMResponseErrorObject`
6567
exception will be raised. If :data:`False` the error object is returned. This value can be overwritten in methods.
@@ -156,13 +158,17 @@ def __init__(
156158
service_provider_config: Optional[ServiceProviderConfig] = None,
157159
check_request_payload: bool = True,
158160
check_response_payload: bool = True,
161+
check_response_content_type: bool = True,
162+
check_response_status_codes: bool = True,
159163
raise_scim_errors: bool = True,
160164
):
161165
self.resource_models = tuple(resource_models or [])
162166
self.resource_types = resource_types
163167
self.service_provider_config = service_provider_config
164168
self.check_request_payload = check_request_payload
165169
self.check_response_payload = check_response_payload
170+
self.check_response_content_type = check_response_content_type
171+
self.check_response_status_codes = check_response_status_codes
166172
self.raise_scim_errors = raise_scim_errors
167173

168174
def get_resource_model(self, name: str) -> Optional[type[Resource]]:
@@ -220,6 +226,31 @@ def register_naive_resource_types(self):
220226
if model not in CONFIG_RESOURCES
221227
]
222228

229+
def _check_status_codes(
230+
self, status_code: int, expected_status_codes: Optional[list[int]]
231+
):
232+
if (
233+
self.check_response_status_codes
234+
and expected_status_codes
235+
and status_code not in expected_status_codes
236+
):
237+
raise UnexpectedStatusCode(status_code)
238+
239+
def _check_content_types(self, headers: dict):
240+
# Interoperability considerations: The "application/scim+json" media
241+
# type is intended to identify JSON structure data that conforms to
242+
# the SCIM protocol and schema specifications. Older versions of
243+
# SCIM are known to informally use "application/json".
244+
# https://datatracker.ietf.org/doc/html/rfc7644.html#section-8.1
245+
246+
actual_content_type = headers.get("content-type", "").split(";").pop(0)
247+
expected_response_content_types = ("application/scim+json", "application/json")
248+
if (
249+
self.check_response_content_type
250+
and actual_content_type not in expected_response_content_types
251+
):
252+
raise UnexpectedContentType(content_type=actual_content_type)
253+
223254
def check_response(
224255
self,
225256
payload: Optional[dict],
@@ -234,19 +265,8 @@ def check_response(
234265
if raise_scim_errors is None:
235266
raise_scim_errors = self.raise_scim_errors
236267

237-
if expected_status_codes and status_code not in expected_status_codes:
238-
raise UnexpectedStatusCode(status_code)
239-
240-
# Interoperability considerations: The "application/scim+json" media
241-
# type is intended to identify JSON structure data that conforms to
242-
# the SCIM protocol and schema specifications. Older versions of
243-
# SCIM are known to informally use "application/json".
244-
# https://datatracker.ietf.org/doc/html/rfc7644.html#section-8.1
245-
246-
actual_content_type = headers.get("content-type", "").split(";").pop(0)
247-
expected_response_content_types = ("application/scim+json", "application/json")
248-
if actual_content_type not in expected_response_content_types:
249-
raise UnexpectedContentType(content_type=actual_content_type)
268+
self._check_status_codes(status_code, expected_status_codes)
269+
self._check_content_types(headers)
250270

251271
# In addition to returning an HTTP response code, implementers MUST return
252272
# the errors in the body of the response in a JSON format
@@ -560,11 +580,11 @@ def create(
560580
561581
:param resource: The resource to create
562582
If is a :data:`dict`, the resource type will be guessed from the schema.
563-
:param check_request_payload: If set, overwrites :paramref:`SCIMClient.check_request_payload`.
564-
:param check_response_payload: If set, overwrites :paramref:`SCIMClient.check_response_payload`.
583+
:param check_request_payload: If set, overwrites :paramref:`~scim2_client.SCIMClient.check_request_payload`.
584+
:param check_response_payload: If set, overwrites :paramref:`~scim2_client.SCIMClient.check_response_payload`.
565585
:param expected_status_codes: The list of expected status codes form the response.
566586
If :data:`None` any status code is accepted.
567-
:param raise_scim_errors: If set, overwrites :paramref:`SCIMClient.raise_scim_errors`.
587+
:param raise_scim_errors: If set, overwrites :paramref:`~scim2_client.SCIMClient.raise_scim_errors`.
568588
:param kwargs: Additional parameters passed to the underlying HTTP request
569589
library.
570590
@@ -612,11 +632,11 @@ def query(
612632
:param resource_model: A :class:`~scim2_models.Resource` subtype or :data:`None`
613633
:param id: The SCIM id of an object to get, or :data:`None`
614634
:param search_request: An object detailing the search query parameters.
615-
:param check_request_payload: If set, overwrites :paramref:`SCIMClient.check_request_payload`.
616-
:param check_response_payload: If set, overwrites :paramref:`SCIMClient.check_response_payload`.
635+
:param check_request_payload: If set, overwrites :paramref:`scim2_client.SCIMClient.check_request_payload`.
636+
:param check_response_payload: If set, overwrites :paramref:`scim2_client.SCIMClient.check_response_payload`.
617637
:param expected_status_codes: The list of expected status codes form the response.
618638
If :data:`None` any status code is accepted.
619-
:param raise_scim_errors: If set, overwrites :paramref:`SCIMClient.raise_scim_errors`.
639+
:param raise_scim_errors: If set, overwrites :paramref:`scim2_client.SCIMClient.raise_scim_errors`.
620640
:param kwargs: Additional parameters passed to the underlying HTTP request library.
621641
622642
:return:
@@ -681,11 +701,11 @@ def search(
681701
:param resource_models: Resource type or union of types expected
682702
to be read from the response.
683703
:param search_request: An object detailing the search query parameters.
684-
:param check_request_payload: If set, overwrites :paramref:`SCIMClient.check_request_payload`.
685-
:param check_response_payload: If set, overwrites :paramref:`SCIMClient.check_response_payload`.
704+
:param check_request_payload: If set, overwrites :paramref:`scim2_client.SCIMClient.check_request_payload`.
705+
:param check_response_payload: If set, overwrites :paramref:`scim2_client.SCIMClient.check_response_payload`.
686706
:param expected_status_codes: The list of expected status codes form the response.
687707
If :data:`None` any status code is accepted.
688-
:param raise_scim_errors: If set, overwrites :paramref:`SCIMClient.raise_scim_errors`.
708+
:param raise_scim_errors: If set, overwrites :paramref:`scim2_client.SCIMClient.raise_scim_errors`.
689709
:param kwargs: Additional parameters passed to the underlying
690710
HTTP request library.
691711
@@ -728,10 +748,10 @@ def delete(
728748
729749
:param resource_model: The type of the resource to delete.
730750
:param id: The type id the resource to delete.
731-
:param check_response_payload: If set, overwrites :paramref:`SCIMClient.check_response_payload`.
751+
:param check_response_payload: If set, overwrites :paramref:`scim2_client.SCIMClient.check_response_payload`.
732752
:param expected_status_codes: The list of expected status codes form the response.
733753
If :data:`None` any status code is accepted.
734-
:param raise_scim_errors: If set, overwrites :paramref:`SCIMClient.raise_scim_errors`.
754+
:param raise_scim_errors: If set, overwrites :paramref:`scim2_client.SCIMClient.raise_scim_errors`.
735755
:param kwargs: Additional parameters passed to the underlying
736756
HTTP request library.
737757
@@ -766,11 +786,11 @@ def replace(
766786
767787
:param resource: The new resource to replace.
768788
If is a :data:`dict`, the resource type will be guessed from the schema.
769-
:param check_request_payload: If set, overwrites :paramref:`SCIMClient.check_request_payload`.
770-
:param check_response_payload: If set, overwrites :paramref:`SCIMClient.check_response_payload`.
789+
:param check_request_payload: If set, overwrites :paramref:`scim2_client.SCIMClient.check_request_payload`.
790+
:param check_response_payload: If set, overwrites :paramref:`scim2_client.SCIMClient.check_response_payload`.
771791
:param expected_status_codes: The list of expected status codes form the response.
772792
If :data:`None` any status code is accepted.
773-
:param raise_scim_errors: If set, overwrites :paramref:`SCIMClient.raise_scim_errors`.
793+
:param raise_scim_errors: If set, overwrites :paramref:`scim2_client.SCIMClient.raise_scim_errors`.
774794
:param kwargs: Additional parameters passed to the underlying
775795
HTTP request library.
776796
@@ -837,11 +857,11 @@ async def create(
837857
838858
:param resource: The resource to create
839859
If is a :data:`dict`, the resource type will be guessed from the schema.
840-
:param check_request_payload: If set, overwrites :paramref:`SCIMClient.check_request_payload`.
841-
:param check_response_payload: If set, overwrites :paramref:`SCIMClient.check_response_payload`.
860+
:param check_request_payload: If set, overwrites :paramref:`scim2_client.SCIMClient.check_request_payload`.
861+
:param check_response_payload: If set, overwrites :paramref:`scim2_client.SCIMClient.check_response_payload`.
842862
:param expected_status_codes: The list of expected status codes form the response.
843863
If :data:`None` any status code is accepted.
844-
:param raise_scim_errors: If set, overwrites :paramref:`SCIMClient.raise_scim_errors`.
864+
:param raise_scim_errors: If set, overwrites :paramref:`scim2_client.SCIMClient.raise_scim_errors`.
845865
:param kwargs: Additional parameters passed to the underlying HTTP request
846866
library.
847867
@@ -889,11 +909,11 @@ async def query(
889909
:param resource_model: A :class:`~scim2_models.Resource` subtype or :data:`None`
890910
:param id: The SCIM id of an object to get, or :data:`None`
891911
:param search_request: An object detailing the search query parameters.
892-
:param check_request_payload: If set, overwrites :paramref:`SCIMClient.check_request_payload`.
893-
:param check_response_payload: If set, overwrites :paramref:`SCIMClient.check_response_payload`.
912+
:param check_request_payload: If set, overwrites :paramref:`scim2_client.SCIMClient.check_request_payload`.
913+
:param check_response_payload: If set, overwrites :paramref:`scim2_client.SCIMClient.check_response_payload`.
894914
:param expected_status_codes: The list of expected status codes form the response.
895915
If :data:`None` any status code is accepted.
896-
:param raise_scim_errors: If set, overwrites :paramref:`SCIMClient.raise_scim_errors`.
916+
:param raise_scim_errors: If set, overwrites :paramref:`scim2_client.SCIMClient.raise_scim_errors`.
897917
:param kwargs: Additional parameters passed to the underlying HTTP request library.
898918
899919
:return:
@@ -958,11 +978,11 @@ async def search(
958978
:param resource_models: Resource type or union of types expected
959979
to be read from the response.
960980
:param search_request: An object detailing the search query parameters.
961-
:param check_request_payload: If set, overwrites :paramref:`SCIMClient.check_request_payload`.
962-
:param check_response_payload: If set, overwrites :paramref:`SCIMClient.check_response_payload`.
981+
:param check_request_payload: If set, overwrites :paramref:`scim2_client.SCIMClient.check_request_payload`.
982+
:param check_response_payload: If set, overwrites :paramref:`scim2_client.SCIMClient.check_response_payload`.
963983
:param expected_status_codes: The list of expected status codes form the response.
964984
If :data:`None` any status code is accepted.
965-
:param raise_scim_errors: If set, overwrites :paramref:`SCIMClient.raise_scim_errors`.
985+
:param raise_scim_errors: If set, overwrites :paramref:`scim2_client.SCIMClient.raise_scim_errors`.
966986
:param kwargs: Additional parameters passed to the underlying
967987
HTTP request library.
968988
@@ -1005,10 +1025,10 @@ async def delete(
10051025
10061026
:param resource_model: The type of the resource to delete.
10071027
:param id: The type id the resource to delete.
1008-
:param check_response_payload: If set, overwrites :paramref:`SCIMClient.check_response_payload`.
1028+
:param check_response_payload: If set, overwrites :paramref:`scim2_client.SCIMClient.check_response_payload`.
10091029
:param expected_status_codes: The list of expected status codes form the response.
10101030
If :data:`None` any status code is accepted.
1011-
:param raise_scim_errors: If set, overwrites :paramref:`SCIMClient.raise_scim_errors`.
1031+
:param raise_scim_errors: If set, overwrites :paramref:`scim2_client.SCIMClient.raise_scim_errors`.
10121032
:param kwargs: Additional parameters passed to the underlying
10131033
HTTP request library.
10141034
@@ -1043,11 +1063,11 @@ async def replace(
10431063
10441064
:param resource: The new resource to replace.
10451065
If is a :data:`dict`, the resource type will be guessed from the schema.
1046-
:param check_request_payload: If set, overwrites :paramref:`SCIMClient.check_request_payload`.
1047-
:param check_response_payload: If set, overwrites :paramref:`SCIMClient.check_response_payload`.
1066+
:param check_request_payload: If set, overwrites :paramref:`scim2_client.SCIMClient.check_request_payload`.
1067+
:param check_response_payload: If set, overwrites :paramref:`scim2_client.SCIMClient.check_response_payload`.
10481068
:param expected_status_codes: The list of expected status codes form the response.
10491069
If :data:`None` any status code is accepted.
1050-
:param raise_scim_errors: If set, overwrites :paramref:`SCIMClient.raise_scim_errors`.
1070+
:param raise_scim_errors: If set, overwrites :paramref:`scim2_client.SCIMClient.raise_scim_errors`.
10511071
:param kwargs: Additional parameters passed to the underlying
10521072
HTTP request library.
10531073

0 commit comments

Comments
 (0)