scim2-client 0.1.0__tar.gz → 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: scim2-client
3
- Version: 0.1.0
3
+ Version: 0.1.2
4
4
  Summary: Pythonically build SCIM requests and parse SCIM responses
5
5
  License: MIT
6
6
  Keywords: scim,scim2,provisioning,httpx,api
@@ -20,12 +20,14 @@ Classifier: Programming Language :: Python :: 3.11
20
20
  Classifier: Programming Language :: Python :: 3.12
21
21
  Classifier: Programming Language :: Python :: Implementation :: CPython
22
22
  Requires-Dist: httpx (>=0.27.0,<0.28.0)
23
- Requires-Dist: scim2-models (>=0.1.0,<0.2.0)
23
+ Requires-Dist: scim2-models (>=0.1.1,<0.2.0)
24
24
  Description-Content-Type: text/markdown
25
25
 
26
26
  # scim2-client
27
27
 
28
- A SCIM client library built upon [scim2-models](https://scim2-models.readthedocs.io), that pythonically build requests and parse responses, following the [RFC7643](https://datatracker.ietf.org/doc/html/rfc7643.html) and [RFC7644](https://datatracker.ietf.org/doc/html/rfc7644.html) specifications.
28
+ A SCIM client Python library built upon [scim2-models](https://scim2-models.readthedocs.io) and [httpx](https://github.com/encode/httpx),
29
+ that pythonically build requests and parse responses,
30
+ following the [RFC7643](https://datatracker.ietf.org/doc/html/rfc7643.html) and [RFC7644](https://datatracker.ietf.org/doc/html/rfc7644.html) specifications.
29
31
  ## Installation
30
32
 
31
33
  ```shell
@@ -40,7 +42,7 @@ Here is an example of usage:
40
42
 
41
43
  ```python
42
44
  import datetime
43
- from httpx impont Client
45
+ from httpx import Client
44
46
  from scim2_models import User, EnterpriseUserUser, Group, Error
45
47
  from scim2_client import SCIMClient
46
48
 
@@ -1,6 +1,8 @@
1
1
  # scim2-client
2
2
 
3
- A SCIM client library built upon [scim2-models](https://scim2-models.readthedocs.io), that pythonically build requests and parse responses, following the [RFC7643](https://datatracker.ietf.org/doc/html/rfc7643.html) and [RFC7644](https://datatracker.ietf.org/doc/html/rfc7644.html) specifications.
3
+ A SCIM client Python library built upon [scim2-models](https://scim2-models.readthedocs.io) and [httpx](https://github.com/encode/httpx),
4
+ that pythonically build requests and parse responses,
5
+ following the [RFC7643](https://datatracker.ietf.org/doc/html/rfc7643.html) and [RFC7644](https://datatracker.ietf.org/doc/html/rfc7644.html) specifications.
4
6
  ## Installation
5
7
 
6
8
  ```shell
@@ -15,7 +17,7 @@ Here is an example of usage:
15
17
 
16
18
  ```python
17
19
  import datetime
18
- from httpx impont Client
20
+ from httpx import Client
19
21
  from scim2_models import User, EnterpriseUserUser, Group, Error
20
22
  from scim2_client import SCIMClient
21
23
 
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
4
4
 
5
5
  [tool.poetry]
6
6
  name = "scim2-client"
7
- version = "0.1.0"
7
+ version = "0.1.2"
8
8
  description = "Pythonically build SCIM requests and parse SCIM responses"
9
9
  authors = ["Yaal Coop <contact@yaal.coop>"]
10
10
  license = "MIT"
@@ -27,7 +27,7 @@ classifiers = [
27
27
  [tool.poetry.dependencies]
28
28
  python = "^3.9"
29
29
  httpx = "^0.27.0"
30
- scim2-models = "^0.1.0"
30
+ scim2-models = "^0.1.1"
31
31
 
32
32
  [tool.poetry.group.doc]
33
33
  optional = true
@@ -0,0 +1,442 @@
1
+ import json
2
+ import json.decoder
3
+ from typing import Dict
4
+ from typing import List
5
+ from typing import Optional
6
+ from typing import Tuple
7
+ from typing import Type
8
+ from typing import Union
9
+
10
+ from httpx import Client
11
+ from httpx import Response
12
+ from pydantic import ValidationError
13
+ from scim2_models import AnyResource
14
+ from scim2_models import Context
15
+ from scim2_models import Error
16
+ from scim2_models import ListResponse
17
+ from scim2_models import PatchOp
18
+ from scim2_models import SearchRequest
19
+
20
+ from .errors import UnexpectedContentFormat
21
+ from .errors import UnexpectedContentType
22
+ from .errors import UnexpectedStatusCode
23
+
24
+ BASE_HEADERS = {
25
+ "Accept": "application/scim+json",
26
+ "Content-Type": "application/scim+json",
27
+ }
28
+
29
+
30
+ class SCIMClient:
31
+ """An object that perform SCIM requests and validate responses."""
32
+
33
+ CREATION_RESPONSE_STATUS_CODES: List[int] = [
34
+ 201,
35
+ 409,
36
+ 307,
37
+ 308,
38
+ 400,
39
+ 401,
40
+ 403,
41
+ 404,
42
+ 500,
43
+ ]
44
+ """Resource creation HTTP codes defined at :rfc:`RFC7644 §3.3
45
+ <7644#section-3.3>` and :rfc:`RFC7644 §3.12 <7644#section-3.12>`"""
46
+
47
+ QUERY_RESPONSE_STATUS_CODES: List[int] = [200, 400, 307, 308, 401, 403, 404, 500]
48
+ """Resource querying HTTP codes defined at :rfc:`RFC7644 §3.4.2
49
+ <7644#section-3.4.2>` and :rfc:`RFC7644 §3.12 <7644#section-3.12>`"""
50
+
51
+ SEARCH_RESPONSE_STATUS_CODES: List[int] = [
52
+ 200,
53
+ 307,
54
+ 308,
55
+ 400,
56
+ 401,
57
+ 403,
58
+ 404,
59
+ 409,
60
+ 413,
61
+ 500,
62
+ 501,
63
+ ]
64
+ """Resource querying HTTP codes defined at :rfc:`RFC7644 §3.4.3
65
+ <7644#section-3.4.3>` and :rfc:`RFC7644 §3.12 <7644#section-3.12>`"""
66
+
67
+ DELETION_RESPONSE_STATUS_CODES: List[int] = [
68
+ 204,
69
+ 307,
70
+ 308,
71
+ 400,
72
+ 401,
73
+ 403,
74
+ 404,
75
+ 412,
76
+ 500,
77
+ 501,
78
+ ]
79
+ """Resource deletion HTTP codes defined at :rfc:`RFC7644 §3.6
80
+ <7644#section-3.6>` and :rfc:`RFC7644 §3.12 <7644#section-3.12>`"""
81
+
82
+ REPLACEMENT_RESPONSE_STATUS_CODES: List[int] = [
83
+ 200,
84
+ 307,
85
+ 308,
86
+ 400,
87
+ 401,
88
+ 403,
89
+ 404,
90
+ 409,
91
+ 412,
92
+ 500,
93
+ 501,
94
+ ]
95
+ """Resource querying HTTP codes defined at :rfc:`RFC7644 §3.4.2
96
+ <7644#section-3.4.2>` and :rfc:`RFC7644 §3.12 <7644#section-3.12>`"""
97
+
98
+ def __init__(self, client: Client, resource_types: Optional[Tuple[Type]] = None):
99
+ self.client = client
100
+ self.resource_types = resource_types or ()
101
+
102
+ def check_resource_type(self, resource_type):
103
+ if resource_type not in self.resource_types:
104
+ raise ValueError(f"Unknown resource type: '{resource_type}'")
105
+
106
+ def resource_endpoint(self, resource_type: Type):
107
+ return f"/{resource_type.__name__}s"
108
+
109
+ def check_response(
110
+ self,
111
+ response: Response,
112
+ expected_status_codes: List[int],
113
+ expected_type: Optional[Type] = None,
114
+ scim_ctx: Optional[Context] = None,
115
+ ):
116
+ if expected_status_codes and response.status_code not in expected_status_codes:
117
+ raise UnexpectedStatusCode(response)
118
+
119
+ # Interoperability considerations: The "application/scim+json" media
120
+ # type is intended to identify JSON structure data that conforms to
121
+ # the SCIM protocol and schema specifications. Older versions of
122
+ # SCIM are known to informally use "application/json".
123
+ # https://datatracker.ietf.org/doc/html/rfc7644.html#section-8.1
124
+
125
+ expected_response_content_types = ("application/scim+json", "application/json")
126
+ if response.headers.get("content-type") not in expected_response_content_types:
127
+ raise UnexpectedContentType(response)
128
+
129
+ # In addition to returning an HTTP response code, implementers MUST return
130
+ # the errors in the body of the response in a JSON format
131
+ # https://datatracker.ietf.org/doc/html/rfc7644.html#section-3.12
132
+
133
+ no_content_status_codes = [204, 205]
134
+ if response.status_code in no_content_status_codes:
135
+ response_payload = None
136
+
137
+ else:
138
+ try:
139
+ response_payload = response.json()
140
+ except json.decoder.JSONDecodeError as exc:
141
+ raise UnexpectedContentFormat(response) from exc
142
+
143
+ try:
144
+ return Error.model_validate(response_payload)
145
+ except ValidationError:
146
+ pass
147
+
148
+ if expected_type:
149
+ try:
150
+ return expected_type.model_validate(response_payload, scim_ctx=scim_ctx)
151
+ except ValidationError as exc:
152
+ exc.response_payload = response_payload
153
+ raise exc
154
+
155
+ return response_payload
156
+
157
+ def create(
158
+ self,
159
+ resource: Union[AnyResource, Dict],
160
+ check_request_payload: bool = True,
161
+ check_response_payload: bool = True,
162
+ check_status_code: bool = True,
163
+ **kwargs,
164
+ ) -> Union[AnyResource, Error, Dict]:
165
+ """Perform a POST request to create, as defined in :rfc:`RFC7644 §3.3
166
+ <7644#section-3.3>`.
167
+
168
+ :param resource: The resource to create
169
+ :param check_request_payload: If :data:`False`,
170
+ :code:`resource` is expected to be a dict that will be passed as-is in the request.
171
+ :param check_response_payload: Whether to validate that the response payload is valid.
172
+ If set, the raw payload will be returned.
173
+ :param check_status_code: Whether to validate that the response status code is valid.
174
+ :param kwargs: Additional parameters passed to the underlying HTTP request
175
+ library.
176
+
177
+ :return:
178
+ - An :class:`~scim2_models.Error` object in case of error.
179
+ - The created object as returned by the server in case of success.
180
+ """
181
+
182
+ if not check_request_payload:
183
+ payload = resource
184
+ url = kwargs.pop("url", None)
185
+
186
+ else:
187
+ self.check_resource_type(resource.__class__)
188
+ url = kwargs.pop("url", self.resource_endpoint(resource.__class__))
189
+ payload = resource.model_dump(scim_ctx=Context.RESOURCE_CREATION_REQUEST)
190
+
191
+ response = self.client.post(url, json=payload, **kwargs)
192
+
193
+ return self.check_response(
194
+ response,
195
+ self.CREATION_RESPONSE_STATUS_CODES if check_status_code else None,
196
+ resource.__class__
197
+ if check_request_payload and check_response_payload
198
+ else None,
199
+ scim_ctx=Context.RESOURCE_CREATION_RESPONSE,
200
+ )
201
+
202
+ def query(
203
+ self,
204
+ resource_type: Type,
205
+ id: Optional[str] = None,
206
+ search_request: Optional[Union[SearchRequest, Dict]] = None,
207
+ check_request_payload: bool = True,
208
+ check_response_payload: bool = True,
209
+ check_status_code: bool = True,
210
+ **kwargs,
211
+ ) -> Union[AnyResource, ListResponse[AnyResource], Error, Dict]:
212
+ """Perform a GET request to read resources, as defined in :rfc:`RFC7644
213
+ §3.4.2 <7644#section-3.4.2>`.
214
+
215
+ - If `id` is not :data:`None`, the resource with the exact id will be reached.
216
+ - If `id` is :data:`None`, all the resources with the given type will be reached.
217
+
218
+ :param resource_type: A :class:`~scim2_models.Resource` subtype or :data:`None`
219
+ :param id: The SCIM id of an object to get, or :data:`None`
220
+ :param search_request: An object detailing the search query parameters.
221
+ :param check_request_payload: If :data:`False`,
222
+ :code:`search_request` is expected to be a dict that will be passed as-is in the request.
223
+ :param check_response_payload: Whether to validate that the response payload is valid.
224
+ If set, the raw payload will be returned.
225
+ :param check_status_code: Whether to validate that the response status code is valid.
226
+ :param kwargs: Additional parameters passed to the underlying HTTP request library.
227
+
228
+ :return:
229
+ - A :class:`~scim2_models.Error` object in case of error.
230
+ - A `resource_type` object in case of success if `id` is not :data:`None`
231
+ - A :class:`~scim2_models.ListResponse[resource_type]` object in case of success if `id` is :data:`None`
232
+ """
233
+
234
+ self.check_resource_type(resource_type)
235
+ if not check_request_payload:
236
+ payload = search_request
237
+
238
+ else:
239
+ payload = (
240
+ search_request.model_dump(
241
+ exclude_unset=True,
242
+ scim_ctx=Context.RESOURCE_QUERY_REQUEST,
243
+ )
244
+ if search_request
245
+ else None
246
+ )
247
+
248
+ if not id:
249
+ expected_type = ListResponse[resource_type]
250
+ url = self.resource_endpoint(resource_type)
251
+
252
+ else:
253
+ expected_type = resource_type
254
+ url = self.resource_endpoint(resource_type) + f"/{id}"
255
+
256
+ response = self.client.get(url, params=payload, **kwargs)
257
+ return self.check_response(
258
+ response,
259
+ self.QUERY_RESPONSE_STATUS_CODES if check_status_code else None,
260
+ expected_type if check_response_payload else None,
261
+ scim_ctx=Context.RESOURCE_QUERY_RESPONSE,
262
+ )
263
+
264
+ def query_all(
265
+ self,
266
+ search_request: Optional[SearchRequest] = None,
267
+ check_request_payload: bool = True,
268
+ check_response_payload: bool = True,
269
+ check_status_code: bool = True,
270
+ **kwargs,
271
+ ) -> Union[AnyResource, ListResponse[AnyResource], Error, Dict]:
272
+ """Perform a GET request to read all available resources, as defined in
273
+ :rfc:`RFC7644 §3.4.2.1 <7644#section-3.4.2.1>`.
274
+
275
+ :param search_request: An object detailing the search query parameters.
276
+ :param check_request_payload: If :data:`False`,
277
+ :code:`search_request` is expected to be a dict that will be passed as-is in the request.
278
+ :param check_response_payload: Whether to validate that the response payload is valid.
279
+ If set, the raw payload will be returned.
280
+ :param check_status_code: Whether to validate that the response status code is valid.
281
+ :param kwargs: Additional parameters passed to the underlying
282
+ HTTP request library.
283
+
284
+ :return:
285
+ - A :class:`~scim2_models.Error` object in case of error.
286
+ - A :class:`~scim2_models.ListResponse[resource_type]` object in case of success.
287
+ """
288
+
289
+ # A query against a server root indicates that all resources within the
290
+ # server SHALL be included, subject to filtering.
291
+ # https://datatracker.ietf.org/doc/html/rfc7644.html#section-3.4.2.1
292
+
293
+ if not check_request_payload:
294
+ payload = search_request
295
+
296
+ else:
297
+ payload = (
298
+ search_request.model_dump(
299
+ exclude_unset=True, scim_ctx=Context.RESOURCE_QUERY_REQUEST
300
+ )
301
+ if search_request
302
+ else None
303
+ )
304
+
305
+ response = self.client.get("/", params=payload)
306
+
307
+ return self.check_response(
308
+ response,
309
+ self.QUERY_RESPONSE_STATUS_CODES if check_status_code else None,
310
+ ListResponse[Union[self.resource_types]]
311
+ if check_response_payload
312
+ else None,
313
+ scim_ctx=Context.RESOURCE_QUERY_RESPONSE,
314
+ )
315
+
316
+ def search(
317
+ self,
318
+ search_request: Optional[SearchRequest] = None,
319
+ check_request_payload: bool = True,
320
+ check_response_payload: bool = True,
321
+ check_status_code: bool = True,
322
+ **kwargs,
323
+ ) -> Union[AnyResource, ListResponse[AnyResource], Error, Dict]:
324
+ """Perform a POST search request to read all available resources, as
325
+ defined in :rfc:`RFC7644 §3.4.3 <7644#section-3.4.3>`.
326
+
327
+ :param resource_types: Resource type or union of types expected
328
+ to be read from the response.
329
+ :param search_request: An object detailing the search query parameters.
330
+ :param check_request_payload: If :data:`False`,
331
+ :code:`search_request` is expected to be a dict that will be passed as-is in the request.
332
+ :param check_response_payload: Whether to validate that the response payload is valid.
333
+ If set, the raw payload will be returned.
334
+ :param check_status_code: Whether to validate that the response status code is valid.
335
+ :param kwargs: Additional parameters passed to the underlying
336
+ HTTP request library.
337
+
338
+ :return:
339
+ - A :class:`~scim2_models.Error` object in case of error.
340
+ - A :class:`~scim2_models.ListResponse[resource_type]` object in case of success.
341
+ """
342
+
343
+ if not check_request_payload:
344
+ payload = search_request
345
+
346
+ else:
347
+ payload = (
348
+ search_request.model_dump(
349
+ exclude_unset=True, scim_ctx=Context.RESOURCE_QUERY_RESPONSE
350
+ )
351
+ if search_request
352
+ else None
353
+ )
354
+
355
+ response = self.client.post("/.search", params=payload)
356
+
357
+ return self.check_response(
358
+ response,
359
+ self.SEARCH_RESPONSE_STATUS_CODES if check_status_code else None,
360
+ ListResponse[Union[self.resource_types]]
361
+ if check_response_payload
362
+ else None,
363
+ scim_ctx=Context.RESOURCE_QUERY_RESPONSE,
364
+ )
365
+
366
+ def delete(
367
+ self, resource_type: Type, id: str, check_status_code: bool = True, **kwargs
368
+ ) -> Optional[Union[Error, Dict]]:
369
+ """Perform a DELETE request to create, as defined in :rfc:`RFC7644 §3.6
370
+ <7644#section-3.6>`.
371
+
372
+ :param check_status_code: Whether to validate that the response status code is valid.
373
+ :param kwargs: Additional parameters passed to the underlying
374
+ HTTP request library.
375
+
376
+ :return:
377
+ - A :class:`~scim2_models.Error` object in case of error.
378
+ - :data:`None` in case of success.
379
+ """
380
+
381
+ self.check_resource_type(resource_type)
382
+ url = self.resource_endpoint(resource_type) + f"/{id}"
383
+ response = self.client.delete(url, **kwargs)
384
+
385
+ return self.check_response(
386
+ response, self.DELETION_RESPONSE_STATUS_CODES if check_status_code else None
387
+ )
388
+
389
+ def replace(
390
+ self,
391
+ resource: Union[AnyResource, Dict],
392
+ check_request_payload: bool = True,
393
+ check_response_payload: bool = True,
394
+ check_status_code: bool = True,
395
+ **kwargs,
396
+ ) -> Union[AnyResource, Error, Dict]:
397
+ """Perform a PUT request to replace a resource, as defined in
398
+ :rfc:`RFC7644 §3.5.1 <7644#section-3.5.1>`.
399
+
400
+ :param resource: The new state of the resource to replace.
401
+ :param check_request_payload: If :data:`False`,
402
+ :code:`resource` is expected to be a dict that will be passed as-is in the request.
403
+ :param check_response_payload: Whether to validate that the response payload is valid.
404
+ If set, the raw payload will be returned.
405
+ :param check_status_code: Whether to validate that the response status code is valid.
406
+ :param kwargs: Additional parameters passed to the underlying
407
+ HTTP request library.
408
+
409
+ :return:
410
+ - An :class:`~scim2_models.Error` object in case of error.
411
+ - The updated object as returned by the server in case of success.
412
+ """
413
+
414
+ if not check_request_payload:
415
+ payload = resource
416
+ url = kwargs.pop("url")
417
+
418
+ else:
419
+ self.check_resource_type(resource.__class__)
420
+ if not resource.id:
421
+ raise Exception("Resource must have an id")
422
+
423
+ payload = resource.model_dump(scim_ctx=Context.RESOURCE_REPLACEMENT_REQUEST)
424
+ url = kwargs.pop(
425
+ "url", self.resource_endpoint(resource.__class__) + f"/{resource.id}"
426
+ )
427
+
428
+ response = self.client.put(url, json=payload, **kwargs)
429
+
430
+ return self.check_response(
431
+ response,
432
+ self.REPLACEMENT_RESPONSE_STATUS_CODES if check_status_code else None,
433
+ resource.__class__
434
+ if check_request_payload and check_response_payload
435
+ else None,
436
+ scim_ctx=Context.RESOURCE_REPLACEMENT_RESPONSE,
437
+ )
438
+
439
+ def modify(
440
+ self, resource: Union[AnyResource, Dict], op: PatchOp, **kwargs
441
+ ) -> Optional[Union[AnyResource, Dict]]:
442
+ raise NotImplementedError()
@@ -1,352 +0,0 @@
1
- import json
2
- import json.decoder
3
- from typing import List
4
- from typing import Optional
5
- from typing import Tuple
6
- from typing import Type
7
- from typing import Union
8
-
9
- from httpx import Client
10
- from httpx import Response
11
- from pydantic import ValidationError
12
- from scim2_models import AnyResource
13
- from scim2_models import Error
14
- from scim2_models import ListResponse
15
- from scim2_models import PatchOp
16
- from scim2_models import SearchRequest
17
-
18
- from .errors import UnexpectedContentFormat
19
- from .errors import UnexpectedContentType
20
- from .errors import UnexpectedStatusCode
21
-
22
- BASE_HEADERS = {
23
- "Accept": "application/scim+json",
24
- "Content-Type": "application/scim+json",
25
- }
26
-
27
-
28
- class SCIMClient:
29
- """An object that perform SCIM requests and validate responses."""
30
-
31
- def __init__(self, client: Client, resource_types: Optional[Tuple[Type]] = None):
32
- self.client = client
33
- self.resource_types = resource_types or ()
34
-
35
- def check_resource_type(self, resource_type):
36
- if resource_type not in self.resource_types:
37
- raise ValueError(f"Unknown resource type: '{resource_type}'")
38
-
39
- def resource_endpoint(self, resource_type: Type):
40
- return f"/{resource_type.__name__}s"
41
-
42
- def check_response(
43
- self,
44
- response: Response,
45
- expected_status_codes: List[int],
46
- expected_type: Optional[Type] = None,
47
- ):
48
- if response.status_code not in expected_status_codes:
49
- raise UnexpectedStatusCode(response)
50
-
51
- # Interoperability considerations: The "application/scim+json" media
52
- # type is intended to identify JSON structure data that conforms to
53
- # the SCIM protocol and schema specifications. Older versions of
54
- # SCIM are known to informally use "application/json".
55
- # https://datatracker.ietf.org/doc/html/rfc7644.html#section-8.1
56
-
57
- expected_response_content_types = ("application/scim+json", "application/json")
58
- if response.headers.get("content-type") not in expected_response_content_types:
59
- raise UnexpectedContentType(response)
60
-
61
- # In addition to returning an HTTP response code, implementers MUST return
62
- # the errors in the body of the response in a JSON format
63
- # https://datatracker.ietf.org/doc/html/rfc7644.html#section-3.12
64
-
65
- if response.status_code in (204, 205):
66
- response_payload = None
67
-
68
- else:
69
- try:
70
- response_payload = response.json()
71
- except json.decoder.JSONDecodeError as exc:
72
- raise UnexpectedContentFormat(response) from exc
73
-
74
- try:
75
- return Error.model_validate(response_payload)
76
- except ValidationError:
77
- pass
78
-
79
- if expected_type:
80
- return expected_type.model_validate(response_payload)
81
- return response_payload
82
-
83
- def create(self, resource: AnyResource, **kwargs) -> Union[AnyResource, Error]:
84
- """Perform a POST request to create, as defined in :rfc:`RFC7644 §3.3
85
- <7644#section-3.3>`.
86
-
87
- :param resource: The resource to create
88
- :param kwargs: Additional parameters passed to the underlying HTTP request
89
- library.
90
-
91
- :return:
92
- - An :class:`~scim2_models.Error` object in case of error.
93
- - The created object as returned by the server in case of success.
94
- """
95
-
96
- self.check_resource_type(resource.__class__)
97
- url = self.resource_endpoint(resource.__class__)
98
- dump = resource.model_dump(exclude_none=True, by_alias=True, mode="json")
99
- response = self.client.post(url, json=dump, **kwargs)
100
-
101
- expected_status_codes = [
102
- # Resource creation HTTP codes defined at:
103
- # https://datatracker.ietf.org/doc/html/rfc7644#section-3.3
104
- 201,
105
- 409,
106
- # Default HTTP codes defined at:
107
- # https://datatracker.ietf.org/doc/html/rfc7644.html#section-3.12
108
- 307,
109
- 308,
110
- 400,
111
- 401,
112
- 403,
113
- 404,
114
- 500,
115
- ]
116
- return self.check_response(response, expected_status_codes, resource.__class__)
117
-
118
- def query(
119
- self,
120
- resource_type: Type,
121
- id: Optional[str] = None,
122
- search_request: Optional[SearchRequest] = None,
123
- **kwargs,
124
- ) -> Union[AnyResource, ListResponse[AnyResource], Error]:
125
- """Perform a GET request to read resources, as defined in :rfc:`RFC7644
126
- §3.4.2 <7644#section-3.4.2>`.
127
-
128
- - If `id` is not :data:`None`, the resource with the exact id will be reached.
129
- - If `id` is :data:`None`, all the resources with the given type will be reached.
130
-
131
- :param resource_type: A :class:`~scim2_models.Resource` subtype or :data:`None`
132
- :param id: The SCIM id of an object to get, or :data:`None`
133
- :param search_request: An object detailing the search query parameters.
134
- :param kwargs: Additional parameters passed to the underlying HTTP request library.
135
-
136
- :return:
137
- - A :class:`~scim2_models.Error` object in case of error.
138
- - A `resource_type` object in case of success if `id` is not :data:`None`
139
- - A :class:`~scim2_models.ListResponse[resource_type]` object in case of success if `id` is :data:`None`
140
- """
141
-
142
- self.check_resource_type(resource_type)
143
- payload = (
144
- search_request.model_dump(
145
- by_alias=True, exclude_none=True, exclude_unset=True, mode="json"
146
- )
147
- if search_request
148
- else None
149
- )
150
-
151
- if not id:
152
- expected_type = ListResponse[resource_type]
153
- url = self.resource_endpoint(resource_type)
154
-
155
- else:
156
- expected_type = resource_type
157
- url = self.resource_endpoint(resource_type) + f"/{id}"
158
-
159
- expected_status_codes = [
160
- # Resource querying HTTP codes defined at:
161
- # https://datatracker.ietf.org/doc/html/rfc7644#section-3.4.2
162
- 200,
163
- 400,
164
- # Default HTTP codes defined at:
165
- # https://datatracker.ietf.org/doc/html/rfc7644.html#section-3.12
166
- 307,
167
- 308,
168
- 401,
169
- 403,
170
- 404,
171
- 500,
172
- ]
173
- response = self.client.get(url, params=payload, **kwargs)
174
- return self.check_response(response, expected_status_codes, expected_type)
175
-
176
- def query_all(
177
- self,
178
- search_request: Optional[SearchRequest] = None,
179
- **kwargs,
180
- ) -> Union[AnyResource, ListResponse[AnyResource], Error]:
181
- """Perform a GET request to read all available resources, as defined in
182
- :rfc:`RFC7644 §3.4.2.1 <7644#section-3.4.2.1>`.
183
-
184
- :param search_request: An object detailing the search query parameters.
185
- :param kwargs: Additional parameters passed to the underlying
186
- HTTP request library.
187
-
188
- :return:
189
- - A :class:`~scim2_models.Error` object in case of error.
190
- - A :class:`~scim2_models.ListResponse[resource_type]` object in case of success.
191
- """
192
-
193
- # A query against a server root indicates that all resources within the
194
- # server SHALL be included, subject to filtering.
195
- # https://datatracker.ietf.org/doc/html/rfc7644.html#section-3.4.2.1
196
-
197
- payload = (
198
- search_request.model_dump(
199
- by_alias=True, exclude_none=True, exclude_unset=True, mode="json"
200
- )
201
- if search_request
202
- else None
203
- )
204
- response = self.client.get("/", params=payload)
205
-
206
- expected_status_codes = [
207
- # Resource querying HTTP codes defined at:
208
- # https://datatracker.ietf.org/doc/html/rfc7644#section-3.4.2
209
- 200,
210
- 400,
211
- # Default HTTP codes defined at:
212
- # https://datatracker.ietf.org/doc/html/rfc7644.html#section-3.12
213
- 307,
214
- 308,
215
- 401,
216
- 403,
217
- 404,
218
- 500,
219
- 501,
220
- ]
221
-
222
- return self.check_response(
223
- response, expected_status_codes, ListResponse[Union[self.resource_types]]
224
- )
225
-
226
- def search(
227
- self,
228
- search_request: Optional[SearchRequest] = None,
229
- **kwargs,
230
- ) -> Union[AnyResource, ListResponse[AnyResource], Error]:
231
- """Perform a POST search request to read all available resources, as
232
- defined in :rfc:`RFC7644 §3.4.3 <7644#section-3.4.3>`.
233
-
234
- :param resource_types: Resource type or union of types expected
235
- to be read from the response.
236
- :param search_request: An object detailing the search query parameters.
237
- :param kwargs: Additional parameters passed to the underlying
238
- HTTP request library.
239
-
240
- :return:
241
- - A :class:`~scim2_models.Error` object in case of error.
242
- - A :class:`~scim2_models.ListResponse[resource_type]` object in case of success.
243
- """
244
-
245
- payload = (
246
- search_request.model_dump(
247
- by_alias=True, exclude_none=True, exclude_unset=True, mode="json"
248
- )
249
- if search_request
250
- else None
251
- )
252
- response = self.client.post("/.search", params=payload)
253
-
254
- expected_status_codes = [
255
- # Resource querying HTTP codes defined at:
256
- # https://datatracker.ietf.org/doc/html/rfc7644#section-3.4.3
257
- 200,
258
- # Default HTTP codes defined at:
259
- # https://datatracker.ietf.org/doc/html/rfc7644.html#section-3.12
260
- 307,
261
- 308,
262
- 400,
263
- 401,
264
- 403,
265
- 404,
266
- 409,
267
- 413,
268
- 500,
269
- 501,
270
- ]
271
- return self.check_response(
272
- response, expected_status_codes, ListResponse[Union[self.resource_types]]
273
- )
274
-
275
- def delete(self, resource_type: Type, id: str, **kwargs) -> Optional[Error]:
276
- """Perform a DELETE request to create, as defined in :rfc:`RFC7644 §3.6
277
- <7644#section-3.6>`.
278
-
279
- :param kwargs: Additional parameters passed to the underlying
280
- HTTP request library.
281
-
282
- :return:
283
- - A :class:`~scim2_models.Error` object in case of error.
284
- - :data:`None` in case of success.
285
- """
286
-
287
- self.check_resource_type(resource_type)
288
- url = self.resource_endpoint(resource_type) + f"/{id}"
289
- response = self.client.delete(url, **kwargs)
290
-
291
- expected_status_codes = [
292
- # Resource deletion HTTP codes defined at:
293
- # https://datatracker.ietf.org/doc/html/rfc7644#section-3.6
294
- 204,
295
- # Default HTTP codes defined at:
296
- # https://datatracker.ietf.org/doc/html/rfc7644.html#section-3.12
297
- 307,
298
- 308,
299
- 400,
300
- 401,
301
- 403,
302
- 404,
303
- 412,
304
- 500,
305
- 501,
306
- ]
307
- return self.check_response(response, expected_status_codes)
308
-
309
- def replace(self, resource: AnyResource, **kwargs) -> Union[AnyResource, Error]:
310
- """Perform a PUT request to replace a resource, as defined in
311
- :rfc:`RFC7644 §3.5.1 <7644#section-3.5.1>`.
312
-
313
- :param resource: The new state of the resource to replace.
314
- :param kwargs: Additional parameters passed to the underlying
315
- HTTP request library.
316
-
317
- :return:
318
- - An :class:`~scim2_models.Error` object in case of error.
319
- - The updated object as returned by the server in case of success.
320
- """
321
-
322
- self.check_resource_type(resource.__class__)
323
- if not resource.id:
324
- raise Exception("Resource must have an id")
325
-
326
- dump = resource.model_dump(exclude_none=True, by_alias=True, mode="json")
327
- url = self.resource_endpoint(resource.__class__) + f"/{resource.id}"
328
- response = self.client.put(url, json=dump, **kwargs)
329
-
330
- expected_status_codes = [
331
- # Resource querying HTTP codes defined at:
332
- # https://datatracker.ietf.org/doc/html/rfc7644#section-3.4.2
333
- 200,
334
- # Default HTTP codes defined at:
335
- # https://datatracker.ietf.org/doc/html/rfc7644.html#section-3.12
336
- 307,
337
- 308,
338
- 400,
339
- 401,
340
- 403,
341
- 404,
342
- 409,
343
- 412,
344
- 500,
345
- 501,
346
- ]
347
- return self.check_response(response, expected_status_codes, resource.__class__)
348
-
349
- def modify(
350
- self, resource: AnyResource, op: PatchOp, **kwargs
351
- ) -> Optional[AnyResource]:
352
- raise NotImplementedError()