github.com/anthropics/anthropic-sdk-go@v1.70.1/betaorganizationapikey.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  	"encoding/json"
     8  	"errors"
     9  	"fmt"
    10  	"net/http"
    11  	"net/url"
    12  	"slices"
    13  	"time"
    14  
    15  	"github.com/anthropics/anthropic-sdk-go/internal/apijson"
    16  	"github.com/anthropics/anthropic-sdk-go/internal/apiquery"
    17  	"github.com/anthropics/anthropic-sdk-go/internal/requestconfig"
    18  	"github.com/anthropics/anthropic-sdk-go/option"
    19  	"github.com/anthropics/anthropic-sdk-go/packages/pagination"
    20  	"github.com/anthropics/anthropic-sdk-go/packages/param"
    21  	"github.com/anthropics/anthropic-sdk-go/packages/respjson"
    22  	"github.com/anthropics/anthropic-sdk-go/shared/constant"
    23  )
    24  
    25  // BetaOrganizationAPIKeyService contains methods and other services that help with
    26  // interacting with the anthropic API.
    27  //
    28  // Note, unlike clients, this service does not read variables from the environment
    29  // automatically. You should not instantiate this service directly, and instead use
    30  // the [NewBetaOrganizationAPIKeyService] method instead.
    31  type BetaOrganizationAPIKeyService struct {
    32  	Options []option.RequestOption
    33  }
    34  
    35  // NewBetaOrganizationAPIKeyService generates a new service that applies the given
    36  // options to each request. These options are applied after the parent client's
    37  // options (if there is one), and before any request-specific options.
    38  func NewBetaOrganizationAPIKeyService(opts ...option.RequestOption) (r BetaOrganizationAPIKeyService) {
    39  	r = BetaOrganizationAPIKeyService{}
    40  	r.Options = opts
    41  	return
    42  }
    43  
    44  // Get API Key
    45  func (r *BetaOrganizationAPIKeyService) Get(ctx context.Context, apiKeyID string, opts ...option.RequestOption) (res *BetaAPIKey, err error) {
    46  	opts = slices.Concat(r.Options, opts)
    47  	if apiKeyID == "" {
    48  		err = errors.New("missing required api_key_id parameter")
    49  		return nil, err
    50  	}
    51  	path := fmt.Sprintf("v1/organizations/api_keys/%s?beta=true", apiKeyID)
    52  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
    53  	return res, err
    54  }
    55  
    56  // Update API Key
    57  func (r *BetaOrganizationAPIKeyService) Update(ctx context.Context, apiKeyID string, body BetaOrganizationAPIKeyUpdateParams, opts ...option.RequestOption) (res *BetaAPIKey, err error) {
    58  	opts = slices.Concat(r.Options, opts)
    59  	if apiKeyID == "" {
    60  		err = errors.New("missing required api_key_id parameter")
    61  		return nil, err
    62  	}
    63  	path := fmt.Sprintf("v1/organizations/api_keys/%s?beta=true", apiKeyID)
    64  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
    65  	return res, err
    66  }
    67  
    68  // List API Keys
    69  func (r *BetaOrganizationAPIKeyService) List(ctx context.Context, query BetaOrganizationAPIKeyListParams, opts ...option.RequestOption) (res *pagination.Page[BetaAPIKey], err error) {
    70  	var raw *http.Response
    71  	opts = slices.Concat(r.Options, opts)
    72  	opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
    73  	path := "v1/organizations/api_keys?beta=true"
    74  	cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	err = cfg.Execute()
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  	res.SetPageConfig(cfg, raw)
    83  	return res, nil
    84  }
    85  
    86  // List API Keys
    87  func (r *BetaOrganizationAPIKeyService) ListAutoPaging(ctx context.Context, query BetaOrganizationAPIKeyListParams, opts ...option.RequestOption) *pagination.PageAutoPager[BetaAPIKey] {
    88  	return pagination.NewPageAutoPager(r.List(ctx, query, opts...))
    89  }
    90  
    91  type BetaAPIKey struct {
    92  	// ID of the API key.
    93  	ID string `json:"id" api:"required"`
    94  	// RFC 3339 datetime string indicating when the API Key was created.
    95  	CreatedAt time.Time `json:"created_at" api:"required" format:"date-time"`
    96  	// The ID and type of the actor that created the API key, or `null` when the
    97  	// creator is not recorded (legacy, workload-identity-federated, or system-created
    98  	// keys).
    99  	CreatedBy BetaAPIKeyCreatedBy `json:"created_by" api:"required"`
   100  	// RFC 3339 datetime string indicating when the API Key expires, or `null` if it
   101  	// never expires.
   102  	ExpiresAt time.Time `json:"expires_at" api:"required" format:"date-time"`
   103  	// Name of the API key.
   104  	Name string `json:"name" api:"required"`
   105  	// Partially redacted hint for the API key.
   106  	PartialKeyHint string `json:"partial_key_hint" api:"required"`
   107  	// The principal the API key acts as (a User or a Service Account), or `null` if
   108  	// the API key is not bound to a principal.
   109  	Principal BetaAPIKeyPrincipalUnion `json:"principal" api:"required"`
   110  	// Where the API key belongs: its Workspace
   111  	// (`{"type": "workspace", "workspace_id": "wrkspc_..."}`, with the Workspace's
   112  	// real ID even when it is the organization's default Workspace), or the
   113  	// organization (`{"type": "organization"}`) for a principal-bound API key that has
   114  	// no Workspace.
   115  	Scope BetaAPIKeyScopeUnion `json:"scope" api:"required"`
   116  	// Status of the API key.
   117  	//
   118  	// Any of "active", "archived", "expired", "inactive".
   119  	Status BetaAPIKeyStatus `json:"status" api:"required"`
   120  	// Object type.
   121  	//
   122  	// For API Keys, this is always `"api_key"`.
   123  	Type constant.APIKey `json:"type" default:"api_key"`
   124  	// Deprecated: use `scope` instead. ID of the Workspace associated with the API
   125  	// key, or `null` if the API key belongs to the default Workspace. Also `null` for
   126  	// a principal-bound API key that has no Workspace; `scope` tells the two apart.
   127  	//
   128  	// Deprecated: Use `scope` instead. `workspace_id` is `null` both for an API key in
   129  	// the default Workspace and for a principal-bound API key that has no Workspace.
   130  	WorkspaceID string `json:"workspace_id" api:"required"`
   131  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   132  	JSON struct {
   133  		ID             respjson.Field
   134  		CreatedAt      respjson.Field
   135  		CreatedBy      respjson.Field
   136  		ExpiresAt      respjson.Field
   137  		Name           respjson.Field
   138  		PartialKeyHint respjson.Field
   139  		Principal      respjson.Field
   140  		Scope          respjson.Field
   141  		Status         respjson.Field
   142  		Type           respjson.Field
   143  		WorkspaceID    respjson.Field
   144  		ExtraFields    map[string]respjson.Field
   145  		raw            string
   146  	} `json:"-"`
   147  }
   148  
   149  // Returns the unmodified JSON received from the API
   150  func (r BetaAPIKey) RawJSON() string { return r.JSON.raw }
   151  func (r *BetaAPIKey) UnmarshalJSON(data []byte) error {
   152  	return apijson.UnmarshalRoot(data, r)
   153  }
   154  
   155  // BetaAPIKeyPrincipalUnion contains all possible properties and values from
   156  // [BetaAPIKeyUserActor], [BetaAPIKeyServiceAccountActor].
   157  //
   158  // Use the [BetaAPIKeyPrincipalUnion.AsAny] method to switch on the variant.
   159  //
   160  // Use the methods beginning with 'As' to cast the union to one of its variants.
   161  type BetaAPIKeyPrincipalUnion struct {
   162  	// Any of "user_actor", "service_account_actor".
   163  	Type string `json:"type"`
   164  	// This field is from variant [BetaAPIKeyUserActor].
   165  	UserID string `json:"user_id"`
   166  	// This field is from variant [BetaAPIKeyServiceAccountActor].
   167  	ServiceAccountID string `json:"service_account_id"`
   168  	JSON             struct {
   169  		Type             respjson.Field
   170  		UserID           respjson.Field
   171  		ServiceAccountID respjson.Field
   172  		raw              string
   173  	} `json:"-"`
   174  }
   175  
   176  // anyBetaAPIKeyPrincipal is implemented by each variant of
   177  // [BetaAPIKeyPrincipalUnion] to add type safety for the return type of
   178  // [BetaAPIKeyPrincipalUnion.AsAny]
   179  type anyBetaAPIKeyPrincipal interface {
   180  	implBetaAPIKeyPrincipalUnion()
   181  }
   182  
   183  func (BetaAPIKeyUserActor) implBetaAPIKeyPrincipalUnion()           {}
   184  func (BetaAPIKeyServiceAccountActor) implBetaAPIKeyPrincipalUnion() {}
   185  
   186  // Use the following switch statement to find the correct variant
   187  //
   188  //	switch variant := BetaAPIKeyPrincipalUnion.AsAny().(type) {
   189  //	case anthropic.BetaAPIKeyUserActor:
   190  //	case anthropic.BetaAPIKeyServiceAccountActor:
   191  //	default:
   192  //	  fmt.Errorf("no variant present")
   193  //	}
   194  func (u BetaAPIKeyPrincipalUnion) AsAny() anyBetaAPIKeyPrincipal {
   195  	switch u.Type {
   196  	case "user_actor":
   197  		return u.AsUserActor()
   198  	case "service_account_actor":
   199  		return u.AsServiceAccountActor()
   200  	}
   201  	return nil
   202  }
   203  
   204  func (u BetaAPIKeyPrincipalUnion) AsUserActor() (v BetaAPIKeyUserActor) {
   205  	apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
   206  	return
   207  }
   208  
   209  func (u BetaAPIKeyPrincipalUnion) AsServiceAccountActor() (v BetaAPIKeyServiceAccountActor) {
   210  	apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
   211  	return
   212  }
   213  
   214  // Returns the unmodified JSON received from the API
   215  func (u BetaAPIKeyPrincipalUnion) RawJSON() string { return u.JSON.raw }
   216  
   217  func (r *BetaAPIKeyPrincipalUnion) UnmarshalJSON(data []byte) error {
   218  	return apijson.UnmarshalRoot(data, r)
   219  }
   220  
   221  // BetaAPIKeyScopeUnion contains all possible properties and values from
   222  // [BetaAPIKeyOrganizationScope], [BetaAPIKeyWorkspaceScope].
   223  //
   224  // Use the [BetaAPIKeyScopeUnion.AsAny] method to switch on the variant.
   225  //
   226  // Use the methods beginning with 'As' to cast the union to one of its variants.
   227  type BetaAPIKeyScopeUnion struct {
   228  	// Any of "organization", "workspace".
   229  	Type string `json:"type"`
   230  	// This field is from variant [BetaAPIKeyWorkspaceScope].
   231  	WorkspaceID string `json:"workspace_id"`
   232  	JSON        struct {
   233  		Type        respjson.Field
   234  		WorkspaceID respjson.Field
   235  		raw         string
   236  	} `json:"-"`
   237  }
   238  
   239  // anyBetaAPIKeyScope is implemented by each variant of [BetaAPIKeyScopeUnion] to
   240  // add type safety for the return type of [BetaAPIKeyScopeUnion.AsAny]
   241  type anyBetaAPIKeyScope interface {
   242  	implBetaAPIKeyScopeUnion()
   243  }
   244  
   245  func (BetaAPIKeyOrganizationScope) implBetaAPIKeyScopeUnion() {}
   246  func (BetaAPIKeyWorkspaceScope) implBetaAPIKeyScopeUnion()    {}
   247  
   248  // Use the following switch statement to find the correct variant
   249  //
   250  //	switch variant := BetaAPIKeyScopeUnion.AsAny().(type) {
   251  //	case anthropic.BetaAPIKeyOrganizationScope:
   252  //	case anthropic.BetaAPIKeyWorkspaceScope:
   253  //	default:
   254  //	  fmt.Errorf("no variant present")
   255  //	}
   256  func (u BetaAPIKeyScopeUnion) AsAny() anyBetaAPIKeyScope {
   257  	switch u.Type {
   258  	case "organization":
   259  		return u.AsOrganization()
   260  	case "workspace":
   261  		return u.AsWorkspace()
   262  	}
   263  	return nil
   264  }
   265  
   266  func (u BetaAPIKeyScopeUnion) AsOrganization() (v BetaAPIKeyOrganizationScope) {
   267  	apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
   268  	return
   269  }
   270  
   271  func (u BetaAPIKeyScopeUnion) AsWorkspace() (v BetaAPIKeyWorkspaceScope) {
   272  	apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
   273  	return
   274  }
   275  
   276  // Returns the unmodified JSON received from the API
   277  func (u BetaAPIKeyScopeUnion) RawJSON() string { return u.JSON.raw }
   278  
   279  func (r *BetaAPIKeyScopeUnion) UnmarshalJSON(data []byte) error {
   280  	return apijson.UnmarshalRoot(data, r)
   281  }
   282  
   283  // Status of the API key.
   284  type BetaAPIKeyStatus string
   285  
   286  const (
   287  	BetaAPIKeyStatusActive   BetaAPIKeyStatus = "active"
   288  	BetaAPIKeyStatusArchived BetaAPIKeyStatus = "archived"
   289  	BetaAPIKeyStatusExpired  BetaAPIKeyStatus = "expired"
   290  	BetaAPIKeyStatusInactive BetaAPIKeyStatus = "inactive"
   291  )
   292  
   293  type BetaAPIKeyCreatedBy struct {
   294  	// ID of the actor that created the object.
   295  	ID string `json:"id" api:"required"`
   296  	// Type of the actor that created the object.
   297  	//
   298  	// Any of "service_account", "user".
   299  	Type BetaAPIKeyCreatedByType `json:"type" api:"required"`
   300  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   301  	JSON struct {
   302  		ID          respjson.Field
   303  		Type        respjson.Field
   304  		ExtraFields map[string]respjson.Field
   305  		raw         string
   306  	} `json:"-"`
   307  }
   308  
   309  // Returns the unmodified JSON received from the API
   310  func (r BetaAPIKeyCreatedBy) RawJSON() string { return r.JSON.raw }
   311  func (r *BetaAPIKeyCreatedBy) UnmarshalJSON(data []byte) error {
   312  	return apijson.UnmarshalRoot(data, r)
   313  }
   314  
   315  // Type of the actor that created the object.
   316  type BetaAPIKeyCreatedByType string
   317  
   318  const (
   319  	BetaAPIKeyCreatedByTypeServiceAccount BetaAPIKeyCreatedByType = "service_account"
   320  	BetaAPIKeyCreatedByTypeUser           BetaAPIKeyCreatedByType = "user"
   321  )
   322  
   323  type BetaAPIKeyOrganizationScope struct {
   324  	// Scope type. Always `"organization"`: the API key has no Workspace. Only a
   325  	// principal-bound API key can have this scope.
   326  	Type constant.Organization `json:"type" default:"organization"`
   327  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   328  	JSON struct {
   329  		Type        respjson.Field
   330  		ExtraFields map[string]respjson.Field
   331  		raw         string
   332  	} `json:"-"`
   333  }
   334  
   335  // Returns the unmodified JSON received from the API
   336  func (r BetaAPIKeyOrganizationScope) RawJSON() string { return r.JSON.raw }
   337  func (r *BetaAPIKeyOrganizationScope) UnmarshalJSON(data []byte) error {
   338  	return apijson.UnmarshalRoot(data, r)
   339  }
   340  
   341  type BetaAPIKeyServiceAccountActor struct {
   342  	// ID of the Service Account the API key acts as.
   343  	ServiceAccountID string `json:"service_account_id" api:"required"`
   344  	// Principal type. Always `"service_account_actor"` for a Service Account.
   345  	Type constant.ServiceAccountActor `json:"type" default:"service_account_actor"`
   346  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   347  	JSON struct {
   348  		ServiceAccountID respjson.Field
   349  		Type             respjson.Field
   350  		ExtraFields      map[string]respjson.Field
   351  		raw              string
   352  	} `json:"-"`
   353  }
   354  
   355  // Returns the unmodified JSON received from the API
   356  func (r BetaAPIKeyServiceAccountActor) RawJSON() string { return r.JSON.raw }
   357  func (r *BetaAPIKeyServiceAccountActor) UnmarshalJSON(data []byte) error {
   358  	return apijson.UnmarshalRoot(data, r)
   359  }
   360  
   361  type BetaAPIKeyUserActor struct {
   362  	// Principal type. Always `"user_actor"` for a User.
   363  	Type constant.UserActor `json:"type" default:"user_actor"`
   364  	// ID of the User the API key acts as.
   365  	UserID string `json:"user_id" api:"required"`
   366  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   367  	JSON struct {
   368  		Type        respjson.Field
   369  		UserID      respjson.Field
   370  		ExtraFields map[string]respjson.Field
   371  		raw         string
   372  	} `json:"-"`
   373  }
   374  
   375  // Returns the unmodified JSON received from the API
   376  func (r BetaAPIKeyUserActor) RawJSON() string { return r.JSON.raw }
   377  func (r *BetaAPIKeyUserActor) UnmarshalJSON(data []byte) error {
   378  	return apijson.UnmarshalRoot(data, r)
   379  }
   380  
   381  type BetaAPIKeyWorkspaceScope struct {
   382  	// Scope type. Always `"workspace"`: the API key belongs to one Workspace.
   383  	Type constant.Workspace `json:"type" default:"workspace"`
   384  	// ID of the Workspace the API key belongs to. Unlike the deprecated top-level
   385  	// `workspace_id`, this is the Workspace's real ID even for the organization's
   386  	// default Workspace.
   387  	WorkspaceID string `json:"workspace_id" api:"required"`
   388  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   389  	JSON struct {
   390  		Type        respjson.Field
   391  		WorkspaceID respjson.Field
   392  		ExtraFields map[string]respjson.Field
   393  		raw         string
   394  	} `json:"-"`
   395  }
   396  
   397  // Returns the unmodified JSON received from the API
   398  func (r BetaAPIKeyWorkspaceScope) RawJSON() string { return r.JSON.raw }
   399  func (r *BetaAPIKeyWorkspaceScope) UnmarshalJSON(data []byte) error {
   400  	return apijson.UnmarshalRoot(data, r)
   401  }
   402  
   403  type BetaOrganizationAPIKeyUpdateParams struct {
   404  	// Name of the API key.
   405  	Name param.Opt[string] `json:"name,omitzero"`
   406  	// Status of the API key.
   407  	//
   408  	// Any of "active", "archived", "inactive".
   409  	Status BetaOrganizationAPIKeyUpdateParamsStatus `json:"status,omitzero"`
   410  	paramObj
   411  }
   412  
   413  func (r BetaOrganizationAPIKeyUpdateParams) MarshalJSON() (data []byte, err error) {
   414  	type shadow BetaOrganizationAPIKeyUpdateParams
   415  	return param.MarshalObject(r, (*shadow)(&r))
   416  }
   417  func (r *BetaOrganizationAPIKeyUpdateParams) UnmarshalJSON(data []byte) error {
   418  	return apijson.UnmarshalRoot(data, r)
   419  }
   420  
   421  // Status of the API key.
   422  type BetaOrganizationAPIKeyUpdateParamsStatus string
   423  
   424  const (
   425  	BetaOrganizationAPIKeyUpdateParamsStatusActive   BetaOrganizationAPIKeyUpdateParamsStatus = "active"
   426  	BetaOrganizationAPIKeyUpdateParamsStatusArchived BetaOrganizationAPIKeyUpdateParamsStatus = "archived"
   427  	BetaOrganizationAPIKeyUpdateParamsStatusInactive BetaOrganizationAPIKeyUpdateParamsStatus = "inactive"
   428  )
   429  
   430  type BetaOrganizationAPIKeyListParams struct {
   431  	// Filter by the ID of the User who created the object.
   432  	CreatedByUserID param.Opt[string] `query:"created_by_user_id,omitzero" json:"-"`
   433  	// Filter by Workspace ID.
   434  	WorkspaceID param.Opt[string] `query:"workspace_id,omitzero" json:"-"`
   435  	// ID of the object to use as a cursor for pagination. When provided, returns the
   436  	// page of results immediately after this object.
   437  	AfterID param.Opt[string] `query:"after_id,omitzero" json:"-"`
   438  	// ID of the object to use as a cursor for pagination. When provided, returns the
   439  	// page of results immediately before this object.
   440  	BeforeID param.Opt[string] `query:"before_id,omitzero" json:"-"`
   441  	// Number of items to return per page.
   442  	//
   443  	// Defaults to `20`. Ranges from `1` to `1000`.
   444  	Limit param.Opt[int64] `query:"limit,omitzero" json:"-"`
   445  	// Filter by API key status.
   446  	//
   447  	// Any of "active", "archived", "expired", "inactive".
   448  	Status BetaOrganizationAPIKeyListParamsStatus `query:"status,omitzero" json:"-"`
   449  	paramObj
   450  }
   451  
   452  // URLQuery serializes [BetaOrganizationAPIKeyListParams]'s query parameters as
   453  // `url.Values`.
   454  func (r BetaOrganizationAPIKeyListParams) URLQuery() (v url.Values, err error) {
   455  	return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
   456  		ArrayFormat:  apiquery.ArrayQueryFormatBrackets,
   457  		NestedFormat: apiquery.NestedQueryFormatBrackets,
   458  	})
   459  }
   460  
   461  // Filter by API key status.
   462  type BetaOrganizationAPIKeyListParamsStatus string
   463  
   464  const (
   465  	BetaOrganizationAPIKeyListParamsStatusActive   BetaOrganizationAPIKeyListParamsStatus = "active"
   466  	BetaOrganizationAPIKeyListParamsStatusArchived BetaOrganizationAPIKeyListParamsStatus = "archived"
   467  	BetaOrganizationAPIKeyListParamsStatusExpired  BetaOrganizationAPIKeyListParamsStatus = "expired"
   468  	BetaOrganizationAPIKeyListParamsStatusInactive BetaOrganizationAPIKeyListParamsStatus = "inactive"
   469  )