Debian Codegraph @

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