Debian Codegraph @

src/auth0/management/users/permissions/raw_client.py

raw symbols SCIP
1 # This file was auto-generated by Fern from our API Definition.
2
3 import typing
4 from json.decoder import JSONDecodeError
5
6 from ...core.api_error import ApiError
7 from ...core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
8 from ...core.http_response import AsyncHttpResponse, HttpResponse
9 from ...core.jsonable_encoder import encode_path_param
10 from ...core.pagination import AsyncPager, SyncPager
11 from ...core.parse_error import ParsingError
12 from ...core.pydantic_utilities import parse_obj_as
13 from ...core.request_options import RequestOptions
14 from ...core.serialization import convert_and_respect_annotation_metadata
15 from ...errors.bad_request_error import BadRequestError
16 from ...errors.forbidden_error import ForbiddenError
17 from ...errors.not_found_error import NotFoundError
18 from ...errors.too_many_requests_error import TooManyRequestsError
19 from ...errors.unauthorized_error import UnauthorizedError
20 from ...types.list_user_permissions_offset_paginated_response_content import (
21 ListUserPermissionsOffsetPaginatedResponseContent,
22 )
23 from ...types.permission_request_payload import PermissionRequestPayload
24 from ...types.user_permission_schema import UserPermissionSchema
25 from pydantic import ValidationError
26
27 # this is used as the default value for optional parameters
28 OMIT = typing.cast(typing.Any, ...)
29
30
31 class RawPermissionsClient:
32 def __init__(self, *, client_wrapper: SyncClientWrapper):
33 self._client_wrapper = client_wrapper
34
35 def list(
36 self,
37 id: str,
38 *,
39 per_page: typing.Optional[int] = 50,
40 page: typing.Optional[int] = 0,
41 include_totals: typing.Optional[bool] = True,
42 request_options: typing.Optional[RequestOptions] = None,
43 ) -> SyncPager[UserPermissionSchema, ListUserPermissionsOffsetPaginatedResponseContent]:
44 """
45 Retrieve all permissions associated with the user.
46
47 Parameters
48 ----------
49 id : str
50 ID of the user to retrieve the permissions for.
51
52 per_page : typing.Optional[int]
53 Number of results per page.
54
55 page : typing.Optional[int]
56 Page index of the results to return. First page is 0.
57
58 include_totals : typing.Optional[bool]
59 Return results inside an object that contains the total result count (true) or as a direct array of results (false, default).
60
61 request_options : typing.Optional[RequestOptions]
62 Request-specific configuration.
63
64 Returns
65 -------
66 SyncPager[UserPermissionSchema, ListUserPermissionsOffsetPaginatedResponseContent]
67 Permissions successfully retrieved.
68 """
69 page = page if page is not None else 0
70
71 _response = self._client_wrapper.httpx_client.request(
72 f"users/{encode_path_param(id)}/permissions",
73 method="GET",
74 params={
75 "per_page": per_page,
76 "page": page,
77 "include_totals": include_totals,
78 },
79 request_options=request_options,
80 )
81 try:
82 if 200 <= _response.status_code < 300:
83 _parsed_response = typing.cast(
84 ListUserPermissionsOffsetPaginatedResponseContent,
85 parse_obj_as(
86 type_=ListUserPermissionsOffsetPaginatedResponseContent, # type: ignore
87 object_=_response.json(),
88 ),
89 )
90 _items = _parsed_response.permissions
91 _has_next = len(_items or []) > 0
92 _get_next = lambda: self.list(
93 id,
94 per_page=per_page,
95 page=page + 1,
96 include_totals=include_totals,
97 request_options=request_options,
98 )
99 return SyncPager(has_next=_has_next, items=_items, get_next=_get_next, response=_parsed_response)
100 if _response.status_code == 400:
101 raise BadRequestError(
102 headers=dict(_response.headers),
103 body=typing.cast(
104 typing.Any,
105 parse_obj_as(
106 type_=typing.Any, # type: ignore
107 object_=_response.json(),
108 ),
109 ),
110 )
111 if _response.status_code == 401:
112 raise UnauthorizedError(
113 headers=dict(_response.headers),
114 body=typing.cast(
115 typing.Any,
116 parse_obj_as(
117 type_=typing.Any, # type: ignore
118 object_=_response.json(),
119 ),
120 ),
121 )
122 if _response.status_code == 403:
123 raise ForbiddenError(
124 headers=dict(_response.headers),
125 body=typing.cast(
126 typing.Any,
127 parse_obj_as(
128 type_=typing.Any, # type: ignore
129 object_=_response.json(),
130 ),
131 ),
132 )
133 if _response.status_code == 404:
134 raise NotFoundError(
135 headers=dict(_response.headers),
136 body=typing.cast(
137 typing.Any,
138 parse_obj_as(
139 type_=typing.Any, # type: ignore
140 object_=_response.json(),
141 ),
142 ),
143 )
144 if _response.status_code == 429:
145 raise TooManyRequestsError(
146 headers=dict(_response.headers),
147 body=typing.cast(
148 typing.Any,
149 parse_obj_as(
150 type_=typing.Any, # type: ignore
151 object_=_response.json(),
152 ),
153 ),
154 )
155 _response_json = _response.json()
156 except JSONDecodeError:
157 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
158 except ValidationError as e:
159 raise ParsingError(
160 status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
161 )
162 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
163
164 def create(
165 self,
166 id: str,
167 *,
168 permissions: typing.Sequence[PermissionRequestPayload],
169 request_options: typing.Optional[RequestOptions] = None,
170 ) -> HttpResponse[None]:
171 """
172 Assign permissions to a user.
173
174 Parameters
175 ----------
176 id : str
177 ID of the user to assign permissions to.
178
179 permissions : typing.Sequence[PermissionRequestPayload]
180 List of permissions to add to this user.
181
182 request_options : typing.Optional[RequestOptions]
183 Request-specific configuration.
184
185 Returns
186 -------
187 HttpResponse[None]
188 """
189 _response = self._client_wrapper.httpx_client.request(
190 f"users/{encode_path_param(id)}/permissions",
191 method="POST",
192 json={
193 "permissions": convert_and_respect_annotation_metadata(
194 object_=permissions, annotation=typing.Sequence[PermissionRequestPayload], direction="write"
195 ),
196 },
197 headers={
198 "content-type": "application/json",
199 },
200 request_options=request_options,
201 omit=OMIT,
202 )
203 try:
204 if 200 <= _response.status_code < 300:
205 return HttpResponse(response=_response, data=None)
206 if _response.status_code == 400:
207 raise BadRequestError(
208 headers=dict(_response.headers),
209 body=typing.cast(
210 typing.Any,
211 parse_obj_as(
212 type_=typing.Any, # type: ignore
213 object_=_response.json(),
214 ),
215 ),
216 )
217 if _response.status_code == 401:
218 raise UnauthorizedError(
219 headers=dict(_response.headers),
220 body=typing.cast(
221 typing.Any,
222 parse_obj_as(
223 type_=typing.Any, # type: ignore
224 object_=_response.json(),
225 ),
226 ),
227 )
228 if _response.status_code == 403:
229 raise ForbiddenError(
230 headers=dict(_response.headers),
231 body=typing.cast(
232 typing.Any,
233 parse_obj_as(
234 type_=typing.Any, # type: ignore
235 object_=_response.json(),
236 ),
237 ),
238 )
239 if _response.status_code == 404:
240 raise NotFoundError(
241 headers=dict(_response.headers),
242 body=typing.cast(
243 typing.Any,
244 parse_obj_as(
245 type_=typing.Any, # type: ignore
246 object_=_response.json(),
247 ),
248 ),
249 )
250 if _response.status_code == 429:
251 raise TooManyRequestsError(
252 headers=dict(_response.headers),
253 body=typing.cast(
254 typing.Any,
255 parse_obj_as(
256 type_=typing.Any, # type: ignore
257 object_=_response.json(),
258 ),
259 ),
260 )
261 _response_json = _response.json()
262 except JSONDecodeError:
263 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
264 except ValidationError as e:
265 raise ParsingError(
266 status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
267 )
268 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
269
270 def delete(
271 self,
272 id: str,
273 *,
274 permissions: typing.Sequence[PermissionRequestPayload],
275 request_options: typing.Optional[RequestOptions] = None,
276 ) -> HttpResponse[None]:
277 """
278 Remove permissions from a user.
279
280 Parameters
281 ----------
282 id : str
283 ID of the user to remove permissions from.
284
285 permissions : typing.Sequence[PermissionRequestPayload]
286 List of permissions to remove from this user.
287
288 request_options : typing.Optional[RequestOptions]
289 Request-specific configuration.
290
291 Returns
292 -------
293 HttpResponse[None]
294 """
295 _response = self._client_wrapper.httpx_client.request(
296 f"users/{encode_path_param(id)}/permissions",
297 method="DELETE",
298 json={
299 "permissions": convert_and_respect_annotation_metadata(
300 object_=permissions, annotation=typing.Sequence[PermissionRequestPayload], direction="write"
301 ),
302 },
303 headers={
304 "content-type": "application/json",
305 },
306 request_options=request_options,
307 omit=OMIT,
308 )
309 try:
310 if 200 <= _response.status_code < 300:
311 return HttpResponse(response=_response, data=None)
312 if _response.status_code == 400:
313 raise BadRequestError(
314 headers=dict(_response.headers),
315 body=typing.cast(
316 typing.Any,
317 parse_obj_as(
318 type_=typing.Any, # type: ignore
319 object_=_response.json(),
320 ),
321 ),
322 )
323 if _response.status_code == 401:
324 raise UnauthorizedError(
325 headers=dict(_response.headers),
326 body=typing.cast(
327 typing.Any,
328 parse_obj_as(
329 type_=typing.Any, # type: ignore
330 object_=_response.json(),
331 ),
332 ),
333 )
334 if _response.status_code == 403:
335 raise ForbiddenError(
336 headers=dict(_response.headers),
337 body=typing.cast(
338 typing.Any,
339 parse_obj_as(
340 type_=typing.Any, # type: ignore
341 object_=_response.json(),
342 ),
343 ),
344 )
345 if _response.status_code == 404:
346 raise NotFoundError(
347 headers=dict(_response.headers),
348 body=typing.cast(
349 typing.Any,
350 parse_obj_as(
351 type_=typing.Any, # type: ignore
352 object_=_response.json(),
353 ),
354 ),
355 )
356 if _response.status_code == 429:
357 raise TooManyRequestsError(
358 headers=dict(_response.headers),
359 body=typing.cast(
360 typing.Any,
361 parse_obj_as(
362 type_=typing.Any, # type: ignore
363 object_=_response.json(),
364 ),
365 ),
366 )
367 _response_json = _response.json()
368 except JSONDecodeError:
369 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
370 except ValidationError as e:
371 raise ParsingError(
372 status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
373 )
374 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
375
376
377 class AsyncRawPermissionsClient:
378 def __init__(self, *, client_wrapper: AsyncClientWrapper):
379 self._client_wrapper = client_wrapper
380
381 async def list(
382 self,
383 id: str,
384 *,
385 per_page: typing.Optional[int] = 50,
386 page: typing.Optional[int] = 0,
387 include_totals: typing.Optional[bool] = True,
388 request_options: typing.Optional[RequestOptions] = None,
389 ) -> AsyncPager[UserPermissionSchema, ListUserPermissionsOffsetPaginatedResponseContent]:
390 """
391 Retrieve all permissions associated with the user.
392
393 Parameters
394 ----------
395 id : str
396 ID of the user to retrieve the permissions for.
397
398 per_page : typing.Optional[int]
399 Number of results per page.
400
401 page : typing.Optional[int]
402 Page index of the results to return. First page is 0.
403
404 include_totals : typing.Optional[bool]
405 Return results inside an object that contains the total result count (true) or as a direct array of results (false, default).
406
407 request_options : typing.Optional[RequestOptions]
408 Request-specific configuration.
409
410 Returns
411 -------
412 AsyncPager[UserPermissionSchema, ListUserPermissionsOffsetPaginatedResponseContent]
413 Permissions successfully retrieved.
414 """
415 page = page if page is not None else 0
416
417 _response = await self._client_wrapper.httpx_client.request(
418 f"users/{encode_path_param(id)}/permissions",
419 method="GET",
420 params={
421 "per_page": per_page,
422 "page": page,
423 "include_totals": include_totals,
424 },
425 request_options=request_options,
426 )
427 try:
428 if 200 <= _response.status_code < 300:
429 _parsed_response = typing.cast(
430 ListUserPermissionsOffsetPaginatedResponseContent,
431 parse_obj_as(
432 type_=ListUserPermissionsOffsetPaginatedResponseContent, # type: ignore
433 object_=_response.json(),
434 ),
435 )
436 _items = _parsed_response.permissions
437 _has_next = len(_items or []) > 0
438
439 async def _get_next():
440 return await self.list(
441 id,
442 per_page=per_page,
443 page=page + 1,
444 include_totals=include_totals,
445 request_options=request_options,
446 )
447
448 return AsyncPager(has_next=_has_next, items=_items, get_next=_get_next, response=_parsed_response)
449 if _response.status_code == 400:
450 raise BadRequestError(
451 headers=dict(_response.headers),
452 body=typing.cast(
453 typing.Any,
454 parse_obj_as(
455 type_=typing.Any, # type: ignore
456 object_=_response.json(),
457 ),
458 ),
459 )
460 if _response.status_code == 401:
461 raise UnauthorizedError(
462 headers=dict(_response.headers),
463 body=typing.cast(
464 typing.Any,
465 parse_obj_as(
466 type_=typing.Any, # type: ignore
467 object_=_response.json(),
468 ),
469 ),
470 )
471 if _response.status_code == 403:
472 raise ForbiddenError(
473 headers=dict(_response.headers),
474 body=typing.cast(
475 typing.Any,
476 parse_obj_as(
477 type_=typing.Any, # type: ignore
478 object_=_response.json(),
479 ),
480 ),
481 )
482 if _response.status_code == 404:
483 raise NotFoundError(
484 headers=dict(_response.headers),
485 body=typing.cast(
486 typing.Any,
487 parse_obj_as(
488 type_=typing.Any, # type: ignore
489 object_=_response.json(),
490 ),
491 ),
492 )
493 if _response.status_code == 429:
494 raise TooManyRequestsError(
495 headers=dict(_response.headers),
496 body=typing.cast(
497 typing.Any,
498 parse_obj_as(
499 type_=typing.Any, # type: ignore
500 object_=_response.json(),
501 ),
502 ),
503 )
504 _response_json = _response.json()
505 except JSONDecodeError:
506 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
507 except ValidationError as e:
508 raise ParsingError(
509 status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
510 )
511 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
512
513 async def create(
514 self,
515 id: str,
516 *,
517 permissions: typing.Sequence[PermissionRequestPayload],
518 request_options: typing.Optional[RequestOptions] = None,
519 ) -> AsyncHttpResponse[None]:
520 """
521 Assign permissions to a user.
522
523 Parameters
524 ----------
525 id : str
526 ID of the user to assign permissions to.
527
528 permissions : typing.Sequence[PermissionRequestPayload]
529 List of permissions to add to this user.
530
531 request_options : typing.Optional[RequestOptions]
532 Request-specific configuration.
533
534 Returns
535 -------
536 AsyncHttpResponse[None]
537 """
538 _response = await self._client_wrapper.httpx_client.request(
539 f"users/{encode_path_param(id)}/permissions",
540 method="POST",
541 json={
542 "permissions": convert_and_respect_annotation_metadata(
543 object_=permissions, annotation=typing.Sequence[PermissionRequestPayload], direction="write"
544 ),
545 },
546 headers={
547 "content-type": "application/json",
548 },
549 request_options=request_options,
550 omit=OMIT,
551 )
552 try:
553 if 200 <= _response.status_code < 300:
554 return AsyncHttpResponse(response=_response, data=None)
555 if _response.status_code == 400:
556 raise BadRequestError(
557 headers=dict(_response.headers),
558 body=typing.cast(
559 typing.Any,
560 parse_obj_as(
561 type_=typing.Any, # type: ignore
562 object_=_response.json(),
563 ),
564 ),
565 )
566 if _response.status_code == 401:
567 raise UnauthorizedError(
568 headers=dict(_response.headers),
569 body=typing.cast(
570 typing.Any,
571 parse_obj_as(
572 type_=typing.Any, # type: ignore
573 object_=_response.json(),
574 ),
575 ),
576 )
577 if _response.status_code == 403:
578 raise ForbiddenError(
579 headers=dict(_response.headers),
580 body=typing.cast(
581 typing.Any,
582 parse_obj_as(
583 type_=typing.Any, # type: ignore
584 object_=_response.json(),
585 ),
586 ),
587 )
588 if _response.status_code == 404:
589 raise NotFoundError(
590 headers=dict(_response.headers),
591 body=typing.cast(
592 typing.Any,
593 parse_obj_as(
594 type_=typing.Any, # type: ignore
595 object_=_response.json(),
596 ),
597 ),
598 )
599 if _response.status_code == 429:
600 raise TooManyRequestsError(
601 headers=dict(_response.headers),
602 body=typing.cast(
603 typing.Any,
604 parse_obj_as(
605 type_=typing.Any, # type: ignore
606 object_=_response.json(),
607 ),
608 ),
609 )
610 _response_json = _response.json()
611 except JSONDecodeError:
612 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
613 except ValidationError as e:
614 raise ParsingError(
615 status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
616 )
617 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
618
619 async def delete(
620 self,
621 id: str,
622 *,
623 permissions: typing.Sequence[PermissionRequestPayload],
624 request_options: typing.Optional[RequestOptions] = None,
625 ) -> AsyncHttpResponse[None]:
626 """
627 Remove permissions from a user.
628
629 Parameters
630 ----------
631 id : str
632 ID of the user to remove permissions from.
633
634 permissions : typing.Sequence[PermissionRequestPayload]
635 List of permissions to remove from this user.
636
637 request_options : typing.Optional[RequestOptions]
638 Request-specific configuration.
639
640 Returns
641 -------
642 AsyncHttpResponse[None]
643 """
644 _response = await self._client_wrapper.httpx_client.request(
645 f"users/{encode_path_param(id)}/permissions",
646 method="DELETE",
647 json={
648 "permissions": convert_and_respect_annotation_metadata(
649 object_=permissions, annotation=typing.Sequence[PermissionRequestPayload], direction="write"
650 ),
651 },
652 headers={
653 "content-type": "application/json",
654 },
655 request_options=request_options,
656 omit=OMIT,
657 )
658 try:
659 if 200 <= _response.status_code < 300:
660 return AsyncHttpResponse(response=_response, data=None)
661 if _response.status_code == 400:
662 raise BadRequestError(
663 headers=dict(_response.headers),
664 body=typing.cast(
665 typing.Any,
666 parse_obj_as(
667 type_=typing.Any, # type: ignore
668 object_=_response.json(),
669 ),
670 ),
671 )
672 if _response.status_code == 401:
673 raise UnauthorizedError(
674 headers=dict(_response.headers),
675 body=typing.cast(
676 typing.Any,
677 parse_obj_as(
678 type_=typing.Any, # type: ignore
679 object_=_response.json(),
680 ),
681 ),
682 )
683 if _response.status_code == 403:
684 raise ForbiddenError(
685 headers=dict(_response.headers),
686 body=typing.cast(
687 typing.Any,
688 parse_obj_as(
689 type_=typing.Any, # type: ignore
690 object_=_response.json(),
691 ),
692 ),
693 )
694 if _response.status_code == 404:
695 raise NotFoundError(
696 headers=dict(_response.headers),
697 body=typing.cast(
698 typing.Any,
699 parse_obj_as(
700 type_=typing.Any, # type: ignore
701 object_=_response.json(),
702 ),
703 ),
704 )
705 if _response.status_code == 429:
706 raise TooManyRequestsError(
707 headers=dict(_response.headers),
708 body=typing.cast(
709 typing.Any,
710 parse_obj_as(
711 type_=typing.Any, # type: ignore
712 object_=_response.json(),
713 ),
714 ),
715 )
716 _response_json = _response.json()
717 except JSONDecodeError:
718 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
719 except ValidationError as e:
720 raise ParsingError(
721 status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
722 )
723 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)