github.com/anthropics/anthropic-sdk-go@v1.70.1/betaorganizationserviceaccountworkspace.go (about) 1 // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. 2 3 package anthropic 4 5 import ( 6 "context" 7 "errors" 8 "fmt" 9 "net/http" 10 "net/url" 11 "slices" 12 13 "github.com/anthropics/anthropic-sdk-go/internal/apijson" 14 "github.com/anthropics/anthropic-sdk-go/internal/apiquery" 15 "github.com/anthropics/anthropic-sdk-go/internal/requestconfig" 16 "github.com/anthropics/anthropic-sdk-go/option" 17 "github.com/anthropics/anthropic-sdk-go/packages/pagination" 18 "github.com/anthropics/anthropic-sdk-go/packages/param" 19 "github.com/anthropics/anthropic-sdk-go/packages/respjson" 20 "github.com/anthropics/anthropic-sdk-go/shared/constant" 21 ) 22 23 // BetaOrganizationServiceAccountWorkspaceService contains methods and other 24 // services that help with interacting with the anthropic API. 25 // 26 // Note, unlike clients, this service does not read variables from the environment 27 // automatically. You should not instantiate this service directly, and instead use 28 // the [NewBetaOrganizationServiceAccountWorkspaceService] method instead. 29 type BetaOrganizationServiceAccountWorkspaceService struct { 30 Options []option.RequestOption 31 } 32 33 // NewBetaOrganizationServiceAccountWorkspaceService generates a new service that 34 // applies the given options to each request. These options are applied after the 35 // parent client's options (if there is one), and before any request-specific 36 // options. 37 func NewBetaOrganizationServiceAccountWorkspaceService(opts ...option.RequestOption) (r BetaOrganizationServiceAccountWorkspaceService) { 38 r = BetaOrganizationServiceAccountWorkspaceService{} 39 r.Options = opts 40 return 41 } 42 43 // **Requires an OAuth access token with the `org:admin` scope**, from 44 // `ant auth login --scope org:admin` or a workload identity federation rule; Admin 45 // API keys are not accepted. See 46 // [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api). 47 // 48 // List the workspaces a service account is a member of. 49 // 50 // Each entry includes the service account's `workspace_role` in that workspace. 51 // Use `limit` and the `next_page` cursor to paginate. When the service account has 52 // no explicit default-workspace membership, the implicit (`implicit: true`) 53 // membership is returned as the first entry on the first page; with `limit=1` the 54 // first page may return up to 2 entries (the implicit entry plus one explicit 55 // membership) so a pagination cursor can be derived. Memberships are returned only 56 // while the service account is active. Without a `page` cursor, an archived 57 // service account returns an empty list. A `page` cursor that does not match an 58 // active membership returns a 400 invalid-request error. A cursor stops matching 59 // when the membership is removed, the workspace is deleted, or the service account 60 // is archived. Restart pagination from the first page to recover. 61 func (r *BetaOrganizationServiceAccountWorkspaceService) List(ctx context.Context, serviceAccountID string, params BetaOrganizationServiceAccountWorkspaceListParams, opts ...option.RequestOption) (res *pagination.PageCursor[BetaServiceAccountWorkspaceMember], err error) { 62 var raw *http.Response 63 for _, v := range params.Betas { 64 opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v))) 65 } 66 opts = slices.Concat(r.Options, opts) 67 opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...) 68 if serviceAccountID == "" { 69 err = errors.New("missing required service_account_id parameter") 70 return nil, err 71 } 72 path := fmt.Sprintf("v1/organizations/service_accounts/%s/workspaces?beta=true", serviceAccountID) 73 cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, params, &res, opts...) 74 if err != nil { 75 return nil, err 76 } 77 err = cfg.Execute() 78 if err != nil { 79 return nil, err 80 } 81 res.SetPageConfig(cfg, raw) 82 return res, nil 83 } 84 85 // **Requires an OAuth access token with the `org:admin` scope**, from 86 // `ant auth login --scope org:admin` or a workload identity federation rule; Admin 87 // API keys are not accepted. See 88 // [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api). 89 // 90 // List the workspaces a service account is a member of. 91 // 92 // Each entry includes the service account's `workspace_role` in that workspace. 93 // Use `limit` and the `next_page` cursor to paginate. When the service account has 94 // no explicit default-workspace membership, the implicit (`implicit: true`) 95 // membership is returned as the first entry on the first page; with `limit=1` the 96 // first page may return up to 2 entries (the implicit entry plus one explicit 97 // membership) so a pagination cursor can be derived. Memberships are returned only 98 // while the service account is active. Without a `page` cursor, an archived 99 // service account returns an empty list. A `page` cursor that does not match an 100 // active membership returns a 400 invalid-request error. A cursor stops matching 101 // when the membership is removed, the workspace is deleted, or the service account 102 // is archived. Restart pagination from the first page to recover. 103 func (r *BetaOrganizationServiceAccountWorkspaceService) ListAutoPaging(ctx context.Context, serviceAccountID string, params BetaOrganizationServiceAccountWorkspaceListParams, opts ...option.RequestOption) *pagination.PageCursorAutoPager[BetaServiceAccountWorkspaceMember] { 104 return pagination.NewPageCursorAutoPager(r.List(ctx, serviceAccountID, params, opts...)) 105 } 106 107 // **Requires an OAuth access token with the `org:admin` scope**, from 108 // `ant auth login --scope org:admin` or a workload identity federation rule; Admin 109 // API keys are not accepted. See 110 // [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api). 111 // 112 // Add a service account to a workspace with the given `workspace_role`. 113 // 114 // Mirror of `POST /workspaces/{workspace_id}/service_accounts`, addressed from the 115 // service-account side; both create the same membership. If the service account is 116 // already an explicit member of the workspace, its `workspace_role` is replaced 117 // with the value supplied here. Archived workspaces return 400. Archived service 118 // accounts cannot be added and are rejected. 119 func (r *BetaOrganizationServiceAccountWorkspaceService) Add(ctx context.Context, serviceAccountID string, params BetaOrganizationServiceAccountWorkspaceAddParams, opts ...option.RequestOption) (res *BetaServiceAccountWorkspaceMember, err error) { 120 for _, v := range params.Betas { 121 opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v))) 122 } 123 opts = slices.Concat(r.Options, opts) 124 if serviceAccountID == "" { 125 err = errors.New("missing required service_account_id parameter") 126 return nil, err 127 } 128 path := fmt.Sprintf("v1/organizations/service_accounts/%s/workspaces?beta=true", serviceAccountID) 129 err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...) 130 return res, err 131 } 132 133 // **Requires an OAuth access token with the `org:admin` scope**, from 134 // `ant auth login --scope org:admin` or a workload identity federation rule; Admin 135 // API keys are not accepted. See 136 // [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api). 137 // 138 // Remove a service account from a workspace. 139 // 140 // Mirror of 141 // `DELETE /workspaces/{workspace_id}/service_accounts/{service_account_id}`, 142 // addressed from the service-account side. Removal is idempotent (returns 200 even 143 // if the membership was already removed). A DELETE against the implicit 144 // default-workspace membership returns 200 but is a no-op and the membership 145 // persists; deleting an explicit default-workspace row reverts to the implicit 146 // `workspace_user` membership. Archived workspaces return 400. 147 func (r *BetaOrganizationServiceAccountWorkspaceService) Remove(ctx context.Context, workspaceID string, params BetaOrganizationServiceAccountWorkspaceRemoveParams, opts ...option.RequestOption) (res *BetaOrganizationServiceAccountWorkspaceRemoveResponse, err error) { 148 for _, v := range params.Betas { 149 opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v))) 150 } 151 opts = slices.Concat(r.Options, opts) 152 if params.ServiceAccountID == "" { 153 err = errors.New("missing required service_account_id parameter") 154 return nil, err 155 } 156 if workspaceID == "" { 157 err = errors.New("missing required workspace_id parameter") 158 return nil, err 159 } 160 path := fmt.Sprintf("v1/organizations/service_accounts/%s/workspaces/%s?beta=true", params.ServiceAccountID, workspaceID) 161 err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, &res, opts...) 162 return res, err 163 } 164 165 type BetaOrganizationServiceAccountWorkspaceRemoveResponse struct { 166 // Tagged service account ID (`svac_...`) named in the delete request. Removal is 167 // idempotent; see the endpoint description for the implicit-membership no-op. 168 ServiceAccountID string `json:"service_account_id" api:"required"` 169 Type constant.ServiceAccountWorkspaceMemberDeleted `json:"type" default:"service_account_workspace_member_deleted"` 170 // Tagged workspace ID (`wrkspc_...`) named in the delete request. 171 WorkspaceID string `json:"workspace_id" api:"required"` 172 // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. 173 JSON struct { 174 ServiceAccountID respjson.Field 175 Type respjson.Field 176 WorkspaceID respjson.Field 177 ExtraFields map[string]respjson.Field 178 raw string 179 } `json:"-"` 180 } 181 182 // Returns the unmodified JSON received from the API 183 func (r BetaOrganizationServiceAccountWorkspaceRemoveResponse) RawJSON() string { return r.JSON.raw } 184 func (r *BetaOrganizationServiceAccountWorkspaceRemoveResponse) UnmarshalJSON(data []byte) error { 185 return apijson.UnmarshalRoot(data, r) 186 } 187 188 type BetaOrganizationServiceAccountWorkspaceListParams struct { 189 // Opaque cursor from a previous response's `next_page`. 190 Page param.Opt[string] `query:"page,omitzero" json:"-"` 191 // Number of results per page. 192 Limit param.Opt[int64] `query:"limit,omitzero" json:"-"` 193 // Optional header to specify the beta version(s) you want to use. 194 Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"` 195 paramObj 196 } 197 198 // URLQuery serializes [BetaOrganizationServiceAccountWorkspaceListParams]'s query 199 // parameters as `url.Values`. 200 func (r BetaOrganizationServiceAccountWorkspaceListParams) URLQuery() (v url.Values, err error) { 201 return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{ 202 ArrayFormat: apiquery.ArrayQueryFormatBrackets, 203 NestedFormat: apiquery.NestedQueryFormatBrackets, 204 }) 205 } 206 207 type BetaOrganizationServiceAccountWorkspaceAddParams struct { 208 // Tagged workspace ID to add the service account to. 209 WorkspaceID string `json:"workspace_id" api:"required"` 210 // Role to assign to the service account in this workspace. 211 // 212 // Any of "workspace_admin", "workspace_developer", 213 // "workspace_restricted_developer", "workspace_user". 214 WorkspaceRole BetaNoBillingWorkspaceRole `json:"workspace_role,omitzero" api:"required"` 215 // Optional header to specify the beta version(s) you want to use. 216 Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"` 217 paramObj 218 } 219 220 func (r BetaOrganizationServiceAccountWorkspaceAddParams) MarshalJSON() (data []byte, err error) { 221 type shadow BetaOrganizationServiceAccountWorkspaceAddParams 222 return param.MarshalObject(r, (*shadow)(&r)) 223 } 224 func (r *BetaOrganizationServiceAccountWorkspaceAddParams) UnmarshalJSON(data []byte) error { 225 return apijson.UnmarshalRoot(data, r) 226 } 227 228 type BetaOrganizationServiceAccountWorkspaceRemoveParams struct { 229 // ID of the service account. 230 ServiceAccountID string `path:"service_account_id" api:"required" json:"-"` 231 // Optional header to specify the beta version(s) you want to use. 232 Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"` 233 paramObj 234 }