diff --git a/client.go b/client.go index 2b7e04b..ab95837 100644 --- a/client.go +++ b/client.go @@ -8,14 +8,12 @@ import ( // Client represents an instance of the 1Password Go SDK client. type Client struct { - config internal.ClientConfig - Secrets SecretsAPI - Items ItemsAPI - Vaults VaultsAPI + config internal.ClientConfig + Items ItemsSource + Vaults VaultsSource } func initAPIs(client *Client, inner internal.InnerClient) { - client.Secrets = NewSecretsSource(inner) - client.Items = NewItemsSource(inner) - client.Vaults = NewVaultsSource(inner) + client.Items = *NewItemsSource(inner) + client.Vaults = *NewVaultsSource(inner) } diff --git a/items.go b/items.go index cb6e117..ff2e1c8 100644 --- a/items.go +++ b/items.go @@ -9,35 +9,18 @@ import ( "github.com/1password/onepassword-sdk-go/internal" ) -// The Items API holds all operations the SDK client can perform on 1Password items. -type ItemsAPI interface { - // Create a new item - Create(ctx context.Context, params ItemCreateParams) (Item, error) - - // Get an item by vault and item ID - Get(ctx context.Context, vaultId string, itemId string) (Item, error) - - // Update an existing item. - Put(ctx context.Context, item Item) (Item, error) - - // Delete an item. - Delete(ctx context.Context, vaultId string, itemId string) error - - // List all items - ListAll(ctx context.Context, vaultId string) (*Iterator[ItemOverview], error) -} - type ItemsSource struct { - internal.InnerClient + innerClient internal.InnerClient + Secrets ItemsSecretsSource } func NewItemsSource(inner internal.InnerClient) *ItemsSource { - return &ItemsSource{inner} + return &ItemsSource{innerClient: inner, Secrets: *NewItemsSecretsSource(inner)} } // Create a new item func (s ItemsSource) Create(ctx context.Context, params ItemCreateParams) (Item, error) { - resultString, err := clientInvoke(ctx, s.InnerClient, "ItemsCreate", map[string]interface{}{ + resultString, err := clientInvoke(ctx, s.innerClient, "ItemsCreate", map[string]interface{}{ "params": params, }) if err != nil { @@ -53,7 +36,7 @@ func (s ItemsSource) Create(ctx context.Context, params ItemCreateParams) (Item, // Get an item by vault and item ID func (s ItemsSource) Get(ctx context.Context, vaultId string, itemId string) (Item, error) { - resultString, err := clientInvoke(ctx, s.InnerClient, "ItemsGet", map[string]interface{}{ + resultString, err := clientInvoke(ctx, s.innerClient, "ItemsGet", map[string]interface{}{ "vault_id": vaultId, "item_id": itemId, }) @@ -70,7 +53,7 @@ func (s ItemsSource) Get(ctx context.Context, vaultId string, itemId string) (It // Update an existing item. func (s ItemsSource) Put(ctx context.Context, item Item) (Item, error) { - resultString, err := clientInvoke(ctx, s.InnerClient, "ItemsPut", map[string]interface{}{ + resultString, err := clientInvoke(ctx, s.innerClient, "ItemsPut", map[string]interface{}{ "item": item, }) if err != nil { @@ -86,7 +69,7 @@ func (s ItemsSource) Put(ctx context.Context, item Item) (Item, error) { // Delete an item. func (s ItemsSource) Delete(ctx context.Context, vaultId string, itemId string) error { - _, err := clientInvoke(ctx, s.InnerClient, "ItemsDelete", map[string]interface{}{ + _, err := clientInvoke(ctx, s.innerClient, "ItemsDelete", map[string]interface{}{ "vault_id": vaultId, "item_id": itemId, }) @@ -95,7 +78,7 @@ func (s ItemsSource) Delete(ctx context.Context, vaultId string, itemId string) // List all items func (s ItemsSource) ListAll(ctx context.Context, vaultId string) (*Iterator[ItemOverview], error) { - resultString, err := clientInvoke(ctx, s.InnerClient, "ItemsListAll", map[string]interface{}{ + resultString, err := clientInvoke(ctx, s.innerClient, "ItemsListAll", map[string]interface{}{ "vault_id": vaultId, }) if err != nil { diff --git a/items_secrets.go b/items_secrets.go new file mode 100644 index 0000000..81bb852 --- /dev/null +++ b/items_secrets.go @@ -0,0 +1,35 @@ +// Code generated by op-codegen - DO NOT EDIT + +package onepassword + +import ( + "context" + "encoding/json" + + "github.com/1password/onepassword-sdk-go/internal" +) + +// client.Items.Secrets.Resolve(...) +type ItemsSecretsSource struct { + internal.InnerClient +} + +func NewItemsSecretsSource(inner internal.InnerClient) *ItemsSecretsSource { + return &ItemsSecretsSource{inner} +} + +// Resolve returns the secret the provided secret reference points to. +func (s ItemsSecretsSource) Resolve(ctx context.Context, secretReference string) (string, error) { + resultString, err := clientInvoke(ctx, s.InnerClient, "SecretsResolve", map[string]interface{}{ + "secret_reference": secretReference, + }) + if err != nil { + return "", err + } + var result string + err = json.Unmarshal([]byte(*resultString), &result) + if err != nil { + return "", err + } + return result, nil +} diff --git a/secrets.go b/secrets.go deleted file mode 100644 index d0f330d..0000000 --- a/secrets.go +++ /dev/null @@ -1,94 +0,0 @@ -// Code generated by op-codegen - DO NOT EDIT - -package onepassword - -import ( - "context" - "encoding/json" - - "github.com/1password/onepassword-sdk-go/internal" -) - -// The Secrets API includes all operations the SDK client can perform on secrets. -// Use secret reference URIs to securely load secrets from 1Password: op:///[/]/ -type SecretsAPI interface { - // Resolve returns the secret the provided secret reference points to. - Resolve(ctx context.Context, secretReference string) (string, error) -} - -type SecretsSource struct { - internal.InnerClient -} - -func NewSecretsSource(inner internal.InnerClient) *SecretsSource { - return &SecretsSource{inner} -} - -type secretsUtil struct{} - -var Secrets = secretsUtil{} - -// Resolve returns the secret the provided secret reference points to. -func (s SecretsSource) Resolve(ctx context.Context, secretReference string) (string, error) { - resultString, err := clientInvoke(ctx, s.InnerClient, "SecretsResolve", map[string]interface{}{ - "secret_reference": secretReference, - }) - if err != nil { - return "", err - } - var result string - err = json.Unmarshal([]byte(*resultString), &result) - if err != nil { - return "", err - } - return result, nil -} - -// Validate the secret reference to ensure there are no syntax errors. -func (s secretsUtil) ValidateSecretReference(ctx context.Context, secretReference string) error { - core, err := internal.GetSharedCore() - if err != nil { - return err - } - - _, err = core.Invoke(ctx, internal.InvokeConfig{ - Invocation: internal.Invocation{ - Parameters: internal.Parameters{ - MethodName: "ValidateSecretReference", - SerializedParams: map[string]interface{}{"secret_reference": secretReference}, - }, - }, - }) - - return err -} - -func (s secretsUtil) GeneratePassword(ctx context.Context, recipe PasswordRecipe) (GeneratePasswordResponse, error) { - core, err := internal.GetSharedCore() - if err != nil { - return GeneratePasswordResponse{}, err - } - - resultString, err := core.Invoke(ctx, internal.InvokeConfig{ - Invocation: internal.Invocation{ - Parameters: internal.Parameters{ - MethodName: "GeneratePassword", - SerializedParams: map[string]interface{}{"recipe": recipe}, - }, - }, - }) - - if err != nil { - return GeneratePasswordResponse{}, err - } - - var result GeneratePasswordResponse - - err = json.Unmarshal([]byte(*resultString), &result) - - if err != nil { - return GeneratePasswordResponse{}, err - } - - return result, nil -}