Debian Codegraph @

src/auth0/management/roles/users/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_role_users_paginated_response_content import ListRoleUsersPaginatedResponseContent
20 from ...types.role_user import RoleUser
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 RawUsersClient:
28 def __init__(self, *, client_wrapper: SyncClientWrapper):
29 self._client_wrapper = client_wrapper
30
31 def list(
32 self,
33 id: str,
34 *,
35 from_: typing.Optional[str] = None,
36 take: typing.Optional[int] = 50,
37 request_options: typing.Optional[RequestOptions] = None,
38 ) -> SyncPager[RoleUser, ListRoleUsersPaginatedResponseContent]:
39 """
40 Retrieve list of users associated with a specific role. For Dashboard instructions, review [View Users Assigned to Roles](https://auth0.com/docs/manage-users/access-control/configure-core-rbac/roles/view-users-assigned-to-roles).
41
42 This endpoint supports two types of pagination:
43
44 - Offset pagination
45 - Checkpoint pagination
46
47 Checkpoint pagination must be used if you need to retrieve more than 1000 organization members.
48
49 **Checkpoint Pagination**
50
51 To search by checkpoint, use the following parameters:
52
53 - `from`: Optional id from which to start selection.
54 - `take`: The total amount of entries to retrieve when using the from parameter. Defaults to 50.
55
56 **Note**: The first time you call this endpoint using checkpoint pagination, omit the `from` parameter. If there are more results, a `next` value is included in the response. You can use this for subsequent API calls. When `next` is no longer included in the response, no pages are remaining.
57
58 Parameters
59 ----------
60 id : str
61 ID of the role to retrieve a list of users associated with.
62
63 from_ : typing.Optional[str]
64 Optional Id from which to start selection.
65
66 take : typing.Optional[int]
67 Number of results per page. Defaults to 50.
68
69 request_options : typing.Optional[RequestOptions]
70 Request-specific configuration.
71
72 Returns
73 -------
74 SyncPager[RoleUser, ListRoleUsersPaginatedResponseContent]
75 Role users successfully retrieved.
76 """
77 _response = self._client_wrapper.httpx_client.request(
78 f"roles/{encode_path_param(id)}/users",
79 method="GET",
80 params={
81 "from": from_,
82 "take": take,
83 },
84 request_options=request_options,
85 )
86 try:
87 if 200 <= _response.status_code < 300:
88 _parsed_response = typing.cast(
89 ListRoleUsersPaginatedResponseContent,
90 parse_obj_as(
91 type_=ListRoleUsersPaginatedResponseContent, # type: ignore
92 object_=_response.json(),
93 ),
94 )
95 _items = _parsed_response.users
96 _parsed_next = _parsed_response.next
97 _has_next = _parsed_next is not None and _parsed_next != ""
98 _get_next = lambda: self.list(
99 id,
100 from_=_parsed_next,
101 take=take,
102 request_options=request_options,
103 )
104 return SyncPager(has_next=_has_next, items=_items, get_next=_get_next, response=_parsed_response)
105 if _response.status_code == 400:
106 raise BadRequestError(
107 headers=dict(_response.headers),
108 body=typing.cast(
109 typing.Any,
110 parse_obj_as(
111 type_=typing.Any, # type: ignore
112 object_=_response.json(),
113 ),
114 ),
115 )
116 if _response.status_code == 401:
117 raise UnauthorizedError(
118 headers=dict(_response.headers),
119 body=typing.cast(
120 typing.Any,
121 parse_obj_as(
122 type_=typing.Any, # type: ignore
123 object_=_response.json(),
124 ),
125 ),
126 )
127 if _response.status_code == 403:
128 raise ForbiddenError(
129 headers=dict(_response.headers),
130 body=typing.cast(
131 typing.Any,
132 parse_obj_as(
133 type_=typing.Any, # type: ignore
134 object_=_response.json(),
135 ),
136 ),
137 )
138 if _response.status_code == 404:
139 raise NotFoundError(
140 headers=dict(_response.headers),
141 body=typing.cast(
142 typing.Any,
143 parse_obj_as(
144 type_=typing.Any, # type: ignore
145 object_=_response.json(),
146 ),
147 ),
148 )
149 if _response.status_code == 429:
150 raise TooManyRequestsError(
151 headers=dict(_response.headers),
152 body=typing.cast(
153 typing.Any,
154 parse_obj_as(
155 type_=typing.Any, # type: ignore
156 object_=_response.json(),
157 ),
158 ),
159 )
160 _response_json = _response.json()
161 except JSONDecodeError:
162 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
163 except ValidationError as e:
164 raise ParsingError(
165 status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
166 )
167 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
168
169 def assign(
170 self, id: str, *, users: typing.Sequence[str], request_options: typing.Optional[RequestOptions] = None
171 ) -> HttpResponse[None]:
172 """
173 Assign one or more users to an existing user role. To learn more, review [Role-Based Access Control](https://auth0.com/docs/manage-users/access-control/rbac).
174
175 **Note**: New roles cannot be created through this action.
176
177 Parameters
178 ----------
179 id : str
180 ID of the role to assign users to.
181
182 users : typing.Sequence[str]
183 user_id's of the users to assign the role to.
184
185 request_options : typing.Optional[RequestOptions]
186 Request-specific configuration.
187
188 Returns
189 -------
190 HttpResponse[None]
191 """
192 _response = self._client_wrapper.httpx_client.request(
193 f"roles/{encode_path_param(id)}/users",
194 method="POST",
195 json={
196 "users": users,
197 },
198 headers={
199 "content-type": "application/json",
200 },
201 request_options=request_options,
202 omit=OMIT,
203 )
204 try:
205 if 200 <= _response.status_code < 300:
206 return HttpResponse(response=_response, data=None)
207 if _response.status_code == 400:
208 raise BadRequestError(
209 headers=dict(_response.headers),
210 body=typing.cast(
211 typing.Any,
212 parse_obj_as(
213 type_=typing.Any, # type: ignore
214 object_=_response.json(),
215 ),
216 ),
217 )
218 if _response.status_code == 401:
219 raise UnauthorizedError(
220 headers=dict(_response.headers),
221 body=typing.cast(
222 typing.Any,
223 parse_obj_as(
224 type_=typing.Any, # type: ignore
225 object_=_response.json(),
226 ),
227 ),
228 )
229 if _response.status_code == 403:
230 raise ForbiddenError(
231 headers=dict(_response.headers),
232 body=typing.cast(
233 typing.Any,
234 parse_obj_as(
235 type_=typing.Any, # type: ignore
236 object_=_response.json(),
237 ),
238 ),
239 )
240 if _response.status_code == 404:
241 raise NotFoundError(
242 headers=dict(_response.headers),
243 body=typing.cast(
244 typing.Any,
245 parse_obj_as(
246 type_=typing.Any, # type: ignore
247 object_=_response.json(),
248 ),
249 ),
250 )
251 if _response.status_code == 429:
252 raise TooManyRequestsError(
253 headers=dict(_response.headers),
254 body=typing.cast(
255 typing.Any,
256 parse_obj_as(
257 type_=typing.Any, # type: ignore
258 object_=_response.json(),
259 ),
260 ),
261 )
262 _response_json = _response.json()
263 except JSONDecodeError:
264 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
265 except ValidationError as e:
266 raise ParsingError(
267 status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
268 )
269 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
270
271
272 class AsyncRawUsersClient:
273 def __init__(self, *, client_wrapper: AsyncClientWrapper):
274 self._client_wrapper = client_wrapper
275
276 async def list(
277 self,
278 id: str,
279 *,
280 from_: typing.Optional[str] = None,
281 take: typing.Optional[int] = 50,
282 request_options: typing.Optional[RequestOptions] = None,
283 ) -> AsyncPager[RoleUser, ListRoleUsersPaginatedResponseContent]:
284 """
285 Retrieve list of users associated with a specific role. For Dashboard instructions, review [View Users Assigned to Roles](https://auth0.com/docs/manage-users/access-control/configure-core-rbac/roles/view-users-assigned-to-roles).
286
287 This endpoint supports two types of pagination:
288
289 - Offset pagination
290 - Checkpoint pagination
291
292 Checkpoint pagination must be used if you need to retrieve more than 1000 organization members.
293
294 **Checkpoint Pagination**
295
296 To search by checkpoint, use the following parameters:
297
298 - `from`: Optional id from which to start selection.
299 - `take`: The total amount of entries to retrieve when using the from parameter. Defaults to 50.
300
301 **Note**: The first time you call this endpoint using checkpoint pagination, omit the `from` parameter. If there are more results, a `next` value is included in the response. You can use this for subsequent API calls. When `next` is no longer included in the response, no pages are remaining.
302
303 Parameters
304 ----------
305 id : str
306 ID of the role to retrieve a list of users associated with.
307
308 from_ : typing.Optional[str]
309 Optional Id from which to start selection.
310
311 take : typing.Optional[int]
312 Number of results per page. Defaults to 50.
313
314 request_options : typing.Optional[RequestOptions]
315 Request-specific configuration.
316
317 Returns
318 -------
319 AsyncPager[RoleUser, ListRoleUsersPaginatedResponseContent]
320 Role users successfully retrieved.
321 """
322 _response = await self._client_wrapper.httpx_client.request(
323 f"roles/{encode_path_param(id)}/users",
324 method="GET",
325 params={
326 "from": from_,
327 "take": take,
328 },
329 request_options=request_options,
330 )
331 try:
332 if 200 <= _response.status_code < 300:
333 _parsed_response = typing.cast(
334 ListRoleUsersPaginatedResponseContent,
335 parse_obj_as(
336 type_=ListRoleUsersPaginatedResponseContent, # type: ignore
337 object_=_response.json(),
338 ),
339 )
340 _items = _parsed_response.users
341 _parsed_next = _parsed_response.next
342 _has_next = _parsed_next is not None and _parsed_next != ""
343
344 async def _get_next():
345 return await self.list(
346 id,
347 from_=_parsed_next,
348 take=take,
349 request_options=request_options,
350 )
351
352 return AsyncPager(has_next=_has_next, items=_items, get_next=_get_next, response=_parsed_response)
353 if _response.status_code == 400:
354 raise BadRequestError(
355 headers=dict(_response.headers),
356 body=typing.cast(
357 typing.Any,
358 parse_obj_as(
359 type_=typing.Any, # type: ignore
360 object_=_response.json(),
361 ),
362 ),
363 )
364 if _response.status_code == 401:
365 raise UnauthorizedError(
366 headers=dict(_response.headers),
367 body=typing.cast(
368 typing.Any,
369 parse_obj_as(
370 type_=typing.Any, # type: ignore
371 object_=_response.json(),
372 ),
373 ),
374 )
375 if _response.status_code == 403:
376 raise ForbiddenError(
377 headers=dict(_response.headers),
378 body=typing.cast(
379 typing.Any,
380 parse_obj_as(
381 type_=typing.Any, # type: ignore
382 object_=_response.json(),
383 ),
384 ),
385 )
386 if _response.status_code == 404:
387 raise NotFoundError(
388 headers=dict(_response.headers),
389 body=typing.cast(
390 typing.Any,
391 parse_obj_as(
392 type_=typing.Any, # type: ignore
393 object_=_response.json(),
394 ),
395 ),
396 )
397 if _response.status_code == 429:
398 raise TooManyRequestsError(
399 headers=dict(_response.headers),
400 body=typing.cast(
401 typing.Any,
402 parse_obj_as(
403 type_=typing.Any, # type: ignore
404 object_=_response.json(),
405 ),
406 ),
407 )
408 _response_json = _response.json()
409 except JSONDecodeError:
410 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
411 except ValidationError as e:
412 raise ParsingError(
413 status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
414 )
415 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
416
417 async def assign(
418 self, id: str, *, users: typing.Sequence[str], request_options: typing.Optional[RequestOptions] = None
419 ) -> AsyncHttpResponse[None]:
420 """
421 Assign one or more users to an existing user role. To learn more, review [Role-Based Access Control](https://auth0.com/docs/manage-users/access-control/rbac).
422
423 **Note**: New roles cannot be created through this action.
424
425 Parameters
426 ----------
427 id : str
428 ID of the role to assign users to.
429
430 users : typing.Sequence[str]
431 user_id's of the users to assign the role to.
432
433 request_options : typing.Optional[RequestOptions]
434 Request-specific configuration.
435
436 Returns
437 -------
438 AsyncHttpResponse[None]
439 """
440 _response = await self._client_wrapper.httpx_client.request(
441 f"roles/{encode_path_param(id)}/users",
442 method="POST",
443 json={
444 "users": users,
445 },
446 headers={
447 "content-type": "application/json",
448 },
449 request_options=request_options,
450 omit=OMIT,
451 )
452 try:
453 if 200 <= _response.status_code < 300:
454 return AsyncHttpResponse(response=_response, data=None)
455 if _response.status_code == 400:
456 raise BadRequestError(
457 headers=dict(_response.headers),
458 body=typing.cast(
459 typing.Any,
460 parse_obj_as(
461 type_=typing.Any, # type: ignore
462 object_=_response.json(),
463 ),
464 ),
465 )
466 if _response.status_code == 401:
467 raise UnauthorizedError(
468 headers=dict(_response.headers),
469 body=typing.cast(
470 typing.Any,
471 parse_obj_as(
472 type_=typing.Any, # type: ignore
473 object_=_response.json(),
474 ),
475 ),
476 )
477 if _response.status_code == 403:
478 raise ForbiddenError(
479 headers=dict(_response.headers),
480 body=typing.cast(
481 typing.Any,
482 parse_obj_as(
483 type_=typing.Any, # type: ignore
484 object_=_response.json(),
485 ),
486 ),
487 )
488 if _response.status_code == 404:
489 raise NotFoundError(
490 headers=dict(_response.headers),
491 body=typing.cast(
492 typing.Any,
493 parse_obj_as(
494 type_=typing.Any, # type: ignore
495 object_=_response.json(),
496 ),
497 ),
498 )
499 if _response.status_code == 429:
500 raise TooManyRequestsError(
501 headers=dict(_response.headers),
502 body=typing.cast(
503 typing.Any,
504 parse_obj_as(
505 type_=typing.Any, # type: ignore
506 object_=_response.json(),
507 ),
508 ),
509 )
510 _response_json = _response.json()
511 except JSONDecodeError:
512 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
513 except ValidationError as e:
514 raise ParsingError(
515 status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
516 )
517 raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)