Debian Codegraph @

src/anthropic/resources/beta/organization/service_accounts/workspaces.py

raw
1 from __future__ import annotations
2
3 from typing import List, Optional
4
5 import httpx2
6
7 from ....._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
8 from ....._utils import is_given, path_template, strip_not_given
9 from ....._compat import cached_property
10 from ....._resource import SyncAPIResource, AsyncAPIResource
11 from ....._response import (
12 to_raw_response_wrapper,
13 to_streamed_response_wrapper,
14 async_to_raw_response_wrapper,
15 async_to_streamed_response_wrapper,
16 )
17 from .....pagination import SyncPageCursor, AsyncPageCursor
18 from ....._base_client import AsyncPaginator, make_request_options
19 from .....types.beta.organization import BetaNoBillingWorkspaceRole
20 from .....types.anthropic_beta_param import AnthropicBetaParam
21 from .....types.beta.organization.beta_no_billing_workspace_role import BetaNoBillingWorkspaceRole
22 from .....types.beta.organization.beta_service_account_workspace_member import BetaServiceAccountWorkspaceMember
23 from .....types.beta.organization.service_accounts.workspace_remove_response import WorkspaceRemoveResponse
24
25 __all__ = ["Workspaces", "AsyncWorkspaces"]
26
27
28 class Workspaces(SyncAPIResource):
29 @cached_property
30 def with_raw_response(self) -> WorkspacesWithRawResponse:
31 """
32 This property can be used as a prefix for any HTTP method call to return
33 the raw response object instead of the parsed content.
34
35 For more information, see https://www.github.com/anthropics/anthropic-sdk-python#accessing-raw-response-data-eg-headers
36 """
37 return WorkspacesWithRawResponse(self)
38
39 @cached_property
40 def with_streaming_response(self) -> WorkspacesWithStreamingResponse:
41 """
42 An alternative to `.with_raw_response` that doesn't eagerly read the response body.
43
44 For more information, see https://www.github.com/anthropics/anthropic-sdk-python#with_streaming_response
45 """
46 return WorkspacesWithStreamingResponse(self)
47
48 def list(
49 self,
50 service_account_id: str,
51 *,
52 limit: int | Omit = omit,
53 page: Optional[str] | Omit = omit,
54 betas: List[AnthropicBetaParam] | Omit = omit,
55 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
56 # The extra values given here take precedence over values defined on the client or passed to this method.
57 extra_headers: Headers | None = None,
58 extra_query: Query | None = None,
59 extra_body: Body | None = None,
60 timeout: float | httpx2.Timeout | None | NotGiven = not_given,
61 ) -> SyncPageCursor[BetaServiceAccountWorkspaceMember]:
62 """
63 **Requires an OAuth access token with the `org:admin` scope**, from
64 `ant auth login --scope org:admin` or a workload identity federation rule; Admin
65 API keys are not accepted. See
66 [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api).
67
68 List the workspaces a service account is a member of.
69
70 Each entry includes the service account's `workspace_role` in that workspace.
71 Use `limit` and the `next_page` cursor to paginate. When the service account has
72 no explicit default-workspace membership, the implicit (`implicit: true`)
73 membership is returned as the first entry on the first page; with `limit=1` the
74 first page may return up to 2 entries (the implicit entry plus one explicit
75 membership) so a pagination cursor can be derived. Memberships are returned only
76 while the service account is active. Without a `page` cursor, an archived
77 service account returns an empty list. A `page` cursor that does not match an
78 active membership returns a 400 invalid-request error. A cursor stops matching
79 when the membership is removed, the workspace is deleted, or the service account
80 is archived. Restart pagination from the first page to recover.
81
82 Args:
83 service_account_id: ID of the service account.
84
85 limit: Number of results per page.
86
87 page: Opaque cursor from a previous response's `next_page`.
88
89 betas: Optional header to specify the beta version(s) you want to use.
90
91 extra_headers: Send extra headers
92
93 extra_query: Add additional query parameters to the request
94
95 extra_body: Add additional JSON properties to the request
96
97 timeout: Override the client-level default timeout for this request, in seconds
98 """
99 if not service_account_id:
100 raise ValueError(f"Expected a non-empty value for `service_account_id` but received {service_account_id!r}")
101 extra_headers = {
102 **strip_not_given({"anthropic-beta": ",".join(str(e) for e in betas) if is_given(betas) else not_given}),
103 **(extra_headers or {}),
104 }
105 return self._get_api_list(
106 path_template(
107 "/v1/organizations/service_accounts/{service_account_id}/workspaces?beta=true",
108 service_account_id=service_account_id,
109 ),
110 page=SyncPageCursor[BetaServiceAccountWorkspaceMember],
111 options=make_request_options(
112 extra_headers=extra_headers,
113 extra_query=extra_query,
114 extra_body=extra_body,
115 timeout=timeout,
116 query={
117 "limit": limit,
118 "page": page,
119 },
120 ),
121 model=BetaServiceAccountWorkspaceMember,
122 )
123
124 def add(
125 self,
126 service_account_id: str,
127 *,
128 workspace_id: str,
129 workspace_role: BetaNoBillingWorkspaceRole,
130 betas: List[AnthropicBetaParam] | Omit = omit,
131 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
132 # The extra values given here take precedence over values defined on the client or passed to this method.
133 extra_headers: Headers | None = None,
134 extra_query: Query | None = None,
135 extra_body: Body | None = None,
136 timeout: float | httpx2.Timeout | None | NotGiven = not_given,
137 ) -> BetaServiceAccountWorkspaceMember:
138 """
139 **Requires an OAuth access token with the `org:admin` scope**, from
140 `ant auth login --scope org:admin` or a workload identity federation rule; Admin
141 API keys are not accepted. See
142 [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api).
143
144 Add a service account to a workspace with the given `workspace_role`.
145
146 Mirror of `POST /workspaces/{workspace_id}/service_accounts`, addressed from the
147 service-account side; both create the same membership. If the service account is
148 already an explicit member of the workspace, its `workspace_role` is replaced
149 with the value supplied here. Archived workspaces return 400. Archived service
150 accounts cannot be added and are rejected.
151
152 Args:
153 service_account_id: ID of the service account.
154
155 workspace_id: Tagged workspace ID to add the service account to.
156
157 workspace_role: Role to assign to the service account in this workspace.
158
159 betas: Optional header to specify the beta version(s) you want to use.
160
161 extra_headers: Send extra headers
162
163 extra_query: Add additional query parameters to the request
164
165 extra_body: Add additional JSON properties to the request
166
167 timeout: Override the client-level default timeout for this request, in seconds
168 """
169 if not service_account_id:
170 raise ValueError(f"Expected a non-empty value for `service_account_id` but received {service_account_id!r}")
171 extra_headers = {
172 **strip_not_given({"anthropic-beta": ",".join(str(e) for e in betas) if is_given(betas) else not_given}),
173 **(extra_headers or {}),
174 }
175 return self._post(
176 path_template(
177 "/v1/organizations/service_accounts/{service_account_id}/workspaces?beta=true",
178 service_account_id=service_account_id,
179 ),
180 body={
181 "workspace_id": workspace_id,
182 "workspace_role": workspace_role,
183 },
184 options=make_request_options(
185 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
186 ),
187 cast_to=BetaServiceAccountWorkspaceMember,
188 )
189
190 def remove(
191 self,
192 workspace_id: str,
193 *,
194 service_account_id: str,
195 betas: List[AnthropicBetaParam] | Omit = omit,
196 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
197 # The extra values given here take precedence over values defined on the client or passed to this method.
198 extra_headers: Headers | None = None,
199 extra_query: Query | None = None,
200 extra_body: Body | None = None,
201 timeout: float | httpx2.Timeout | None | NotGiven = not_given,
202 ) -> WorkspaceRemoveResponse:
203 """
204 **Requires an OAuth access token with the `org:admin` scope**, from
205 `ant auth login --scope org:admin` or a workload identity federation rule; Admin
206 API keys are not accepted. See
207 [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api).
208
209 Remove a service account from a workspace.
210
211 Mirror of
212 `DELETE /workspaces/{workspace_id}/service_accounts/{service_account_id}`,
213 addressed from the service-account side. Removal is idempotent (returns 200 even
214 if the membership was already removed). A DELETE against the implicit
215 default-workspace membership returns 200 but is a no-op and the membership
216 persists; deleting an explicit default-workspace row reverts to the implicit
217 `workspace_user` membership. Archived workspaces return 400.
218
219 Args:
220 service_account_id: ID of the service account.
221
222 workspace_id: ID of the workspace.
223
224 betas: Optional header to specify the beta version(s) you want to use.
225
226 extra_headers: Send extra headers
227
228 extra_query: Add additional query parameters to the request
229
230 extra_body: Add additional JSON properties to the request
231
232 timeout: Override the client-level default timeout for this request, in seconds
233 """
234 if not service_account_id:
235 raise ValueError(f"Expected a non-empty value for `service_account_id` but received {service_account_id!r}")
236 if not workspace_id:
237 raise ValueError(f"Expected a non-empty value for `workspace_id` but received {workspace_id!r}")
238 extra_headers = {
239 **strip_not_given({"anthropic-beta": ",".join(str(e) for e in betas) if is_given(betas) else not_given}),
240 **(extra_headers or {}),
241 }
242 return self._delete(
243 path_template(
244 "/v1/organizations/service_accounts/{service_account_id}/workspaces/{workspace_id}?beta=true",
245 service_account_id=service_account_id,
246 workspace_id=workspace_id,
247 ),
248 options=make_request_options(
249 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
250 ),
251 cast_to=WorkspaceRemoveResponse,
252 )
253
254
255 class AsyncWorkspaces(AsyncAPIResource):
256 @cached_property
257 def with_raw_response(self) -> AsyncWorkspacesWithRawResponse:
258 """
259 This property can be used as a prefix for any HTTP method call to return
260 the raw response object instead of the parsed content.
261
262 For more information, see https://www.github.com/anthropics/anthropic-sdk-python#accessing-raw-response-data-eg-headers
263 """
264 return AsyncWorkspacesWithRawResponse(self)
265
266 @cached_property
267 def with_streaming_response(self) -> AsyncWorkspacesWithStreamingResponse:
268 """
269 An alternative to `.with_raw_response` that doesn't eagerly read the response body.
270
271 For more information, see https://www.github.com/anthropics/anthropic-sdk-python#with_streaming_response
272 """
273 return AsyncWorkspacesWithStreamingResponse(self)
274
275 def list(
276 self,
277 service_account_id: str,
278 *,
279 limit: int | Omit = omit,
280 page: Optional[str] | Omit = omit,
281 betas: List[AnthropicBetaParam] | Omit = omit,
282 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
283 # The extra values given here take precedence over values defined on the client or passed to this method.
284 extra_headers: Headers | None = None,
285 extra_query: Query | None = None,
286 extra_body: Body | None = None,
287 timeout: float | httpx2.Timeout | None | NotGiven = not_given,
288 ) -> AsyncPaginator[BetaServiceAccountWorkspaceMember, AsyncPageCursor[BetaServiceAccountWorkspaceMember]]:
289 """
290 **Requires an OAuth access token with the `org:admin` scope**, from
291 `ant auth login --scope org:admin` or a workload identity federation rule; Admin
292 API keys are not accepted. See
293 [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api).
294
295 List the workspaces a service account is a member of.
296
297 Each entry includes the service account's `workspace_role` in that workspace.
298 Use `limit` and the `next_page` cursor to paginate. When the service account has
299 no explicit default-workspace membership, the implicit (`implicit: true`)
300 membership is returned as the first entry on the first page; with `limit=1` the
301 first page may return up to 2 entries (the implicit entry plus one explicit
302 membership) so a pagination cursor can be derived. Memberships are returned only
303 while the service account is active. Without a `page` cursor, an archived
304 service account returns an empty list. A `page` cursor that does not match an
305 active membership returns a 400 invalid-request error. A cursor stops matching
306 when the membership is removed, the workspace is deleted, or the service account
307 is archived. Restart pagination from the first page to recover.
308
309 Args:
310 service_account_id: ID of the service account.
311
312 limit: Number of results per page.
313
314 page: Opaque cursor from a previous response's `next_page`.
315
316 betas: Optional header to specify the beta version(s) you want to use.
317
318 extra_headers: Send extra headers
319
320 extra_query: Add additional query parameters to the request
321
322 extra_body: Add additional JSON properties to the request
323
324 timeout: Override the client-level default timeout for this request, in seconds
325 """
326 if not service_account_id:
327 raise ValueError(f"Expected a non-empty value for `service_account_id` but received {service_account_id!r}")
328 extra_headers = {
329 **strip_not_given({"anthropic-beta": ",".join(str(e) for e in betas) if is_given(betas) else not_given}),
330 **(extra_headers or {}),
331 }
332 return self._get_api_list(
333 path_template(
334 "/v1/organizations/service_accounts/{service_account_id}/workspaces?beta=true",
335 service_account_id=service_account_id,
336 ),
337 page=AsyncPageCursor[BetaServiceAccountWorkspaceMember],
338 options=make_request_options(
339 extra_headers=extra_headers,
340 extra_query=extra_query,
341 extra_body=extra_body,
342 timeout=timeout,
343 query={
344 "limit": limit,
345 "page": page,
346 },
347 ),
348 model=BetaServiceAccountWorkspaceMember,
349 )
350
351 async def add(
352 self,
353 service_account_id: str,
354 *,
355 workspace_id: str,
356 workspace_role: BetaNoBillingWorkspaceRole,
357 betas: List[AnthropicBetaParam] | Omit = omit,
358 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
359 # The extra values given here take precedence over values defined on the client or passed to this method.
360 extra_headers: Headers | None = None,
361 extra_query: Query | None = None,
362 extra_body: Body | None = None,
363 timeout: float | httpx2.Timeout | None | NotGiven = not_given,
364 ) -> BetaServiceAccountWorkspaceMember:
365 """
366 **Requires an OAuth access token with the `org:admin` scope**, from
367 `ant auth login --scope org:admin` or a workload identity federation rule; Admin
368 API keys are not accepted. See
369 [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api).
370
371 Add a service account to a workspace with the given `workspace_role`.
372
373 Mirror of `POST /workspaces/{workspace_id}/service_accounts`, addressed from the
374 service-account side; both create the same membership. If the service account is
375 already an explicit member of the workspace, its `workspace_role` is replaced
376 with the value supplied here. Archived workspaces return 400. Archived service
377 accounts cannot be added and are rejected.
378
379 Args:
380 service_account_id: ID of the service account.
381
382 workspace_id: Tagged workspace ID to add the service account to.
383
384 workspace_role: Role to assign to the service account in this workspace.
385
386 betas: Optional header to specify the beta version(s) you want to use.
387
388 extra_headers: Send extra headers
389
390 extra_query: Add additional query parameters to the request
391
392 extra_body: Add additional JSON properties to the request
393
394 timeout: Override the client-level default timeout for this request, in seconds
395 """
396 if not service_account_id:
397 raise ValueError(f"Expected a non-empty value for `service_account_id` but received {service_account_id!r}")
398 extra_headers = {
399 **strip_not_given({"anthropic-beta": ",".join(str(e) for e in betas) if is_given(betas) else not_given}),
400 **(extra_headers or {}),
401 }
402 return await self._post(
403 path_template(
404 "/v1/organizations/service_accounts/{service_account_id}/workspaces?beta=true",
405 service_account_id=service_account_id,
406 ),
407 body={
408 "workspace_id": workspace_id,
409 "workspace_role": workspace_role,
410 },
411 options=make_request_options(
412 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
413 ),
414 cast_to=BetaServiceAccountWorkspaceMember,
415 )
416
417 async def remove(
418 self,
419 workspace_id: str,
420 *,
421 service_account_id: str,
422 betas: List[AnthropicBetaParam] | Omit = omit,
423 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
424 # The extra values given here take precedence over values defined on the client or passed to this method.
425 extra_headers: Headers | None = None,
426 extra_query: Query | None = None,
427 extra_body: Body | None = None,
428 timeout: float | httpx2.Timeout | None | NotGiven = not_given,
429 ) -> WorkspaceRemoveResponse:
430 """
431 **Requires an OAuth access token with the `org:admin` scope**, from
432 `ant auth login --scope org:admin` or a workload identity federation rule; Admin
433 API keys are not accepted. See
434 [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api).
435
436 Remove a service account from a workspace.
437
438 Mirror of
439 `DELETE /workspaces/{workspace_id}/service_accounts/{service_account_id}`,
440 addressed from the service-account side. Removal is idempotent (returns 200 even
441 if the membership was already removed). A DELETE against the implicit
442 default-workspace membership returns 200 but is a no-op and the membership
443 persists; deleting an explicit default-workspace row reverts to the implicit
444 `workspace_user` membership. Archived workspaces return 400.
445
446 Args:
447 service_account_id: ID of the service account.
448
449 workspace_id: ID of the workspace.
450
451 betas: Optional header to specify the beta version(s) you want to use.
452
453 extra_headers: Send extra headers
454
455 extra_query: Add additional query parameters to the request
456
457 extra_body: Add additional JSON properties to the request
458
459 timeout: Override the client-level default timeout for this request, in seconds
460 """
461 if not service_account_id:
462 raise ValueError(f"Expected a non-empty value for `service_account_id` but received {service_account_id!r}")
463 if not workspace_id:
464 raise ValueError(f"Expected a non-empty value for `workspace_id` but received {workspace_id!r}")
465 extra_headers = {
466 **strip_not_given({"anthropic-beta": ",".join(str(e) for e in betas) if is_given(betas) else not_given}),
467 **(extra_headers or {}),
468 }
469 return await self._delete(
470 path_template(
471 "/v1/organizations/service_accounts/{service_account_id}/workspaces/{workspace_id}?beta=true",
472 service_account_id=service_account_id,
473 workspace_id=workspace_id,
474 ),
475 options=make_request_options(
476 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
477 ),
478 cast_to=WorkspaceRemoveResponse,
479 )
480
481
482 class WorkspacesWithRawResponse:
483 def __init__(self, workspaces: Workspaces) -> None:
484 self._workspaces = workspaces
485
486 self.list = to_raw_response_wrapper(
487 workspaces.list,
488 )
489 self.add = to_raw_response_wrapper(
490 workspaces.add,
491 )
492 self.remove = to_raw_response_wrapper(
493 workspaces.remove,
494 )
495
496
497 class AsyncWorkspacesWithRawResponse:
498 def __init__(self, workspaces: AsyncWorkspaces) -> None:
499 self._workspaces = workspaces
500
501 self.list = async_to_raw_response_wrapper(
502 workspaces.list,
503 )
504 self.add = async_to_raw_response_wrapper(
505 workspaces.add,
506 )
507 self.remove = async_to_raw_response_wrapper(
508 workspaces.remove,
509 )
510
511
512 class WorkspacesWithStreamingResponse:
513 def __init__(self, workspaces: Workspaces) -> None:
514 self._workspaces = workspaces
515
516 self.list = to_streamed_response_wrapper(
517 workspaces.list,
518 )
519 self.add = to_streamed_response_wrapper(
520 workspaces.add,
521 )
522 self.remove = to_streamed_response_wrapper(
523 workspaces.remove,
524 )
525
526
527 class AsyncWorkspacesWithStreamingResponse:
528 def __init__(self, workspaces: AsyncWorkspaces) -> None:
529 self._workspaces = workspaces
530
531 self.list = async_to_streamed_response_wrapper(
532 workspaces.list,
533 )
534 self.add = async_to_streamed_response_wrapper(
535 workspaces.add,
536 )
537 self.remove = async_to_streamed_response_wrapper(
538 workspaces.remove,
539 )