Use global context

This commit is contained in:
Volodymyr Zotov
2025-06-08 10:28:15 -05:00
parent ac06f8db13
commit efbe96e93a
17 changed files with 150 additions and 136 deletions

View File

@@ -1,6 +1,7 @@
package connect
import (
"context"
"fmt"
"github.com/1Password/connect-sdk-go/connect"
@@ -27,7 +28,7 @@ func NewClient(config Config) *Connect {
}
}
func (c *Connect) GetItemByID(vaultID, itemID string) (*model.Item, error) {
func (c *Connect) GetItemByID(ctx context.Context, vaultID, itemID string) (*model.Item, error) {
connectItem, err := c.client.GetItemByUUID(itemID, vaultID)
if err != nil {
return nil, fmt.Errorf("1password Connect error: %w", err)
@@ -38,7 +39,7 @@ func (c *Connect) GetItemByID(vaultID, itemID string) (*model.Item, error) {
return &item, nil
}
func (c *Connect) GetItemsByTitle(vaultID, itemTitle string) ([]model.Item, error) {
func (c *Connect) GetItemsByTitle(ctx context.Context, vaultID, itemTitle string) ([]model.Item, error) {
// Get all items in the vault with the specified title
connectItems, err := c.client.GetItemsByTitle(itemTitle, vaultID)
if err != nil {
@@ -55,7 +56,7 @@ func (c *Connect) GetItemsByTitle(vaultID, itemTitle string) ([]model.Item, erro
return items, nil
}
func (c *Connect) GetFileContent(vaultID, itemID, fileID string) ([]byte, error) {
func (c *Connect) GetFileContent(ctx context.Context, vaultID, itemID, fileID string) ([]byte, error) {
bytes, err := c.client.GetFileContent(&onepassword.File{
ContentPath: fmt.Sprintf("/v1/vaults/%s/items/%s/files/%s/content", vaultID, itemID, fileID),
})
@@ -66,7 +67,7 @@ func (c *Connect) GetFileContent(vaultID, itemID, fileID string) ([]byte, error)
return bytes, nil
}
func (c *Connect) GetVaultsByTitle(vaultQuery string) ([]model.Vault, error) {
func (c *Connect) GetVaultsByTitle(ctx context.Context, vaultQuery string) ([]model.Vault, error) {
connectVaults, err := c.client.GetVaultsByTitle(vaultQuery)
if err != nil {
return nil, fmt.Errorf("1password Connect error: %w", err)

View File

@@ -1,6 +1,7 @@
package connect
import (
"context"
"errors"
"testing"
@@ -48,7 +49,7 @@ func TestConnect_GetItemByID(t *testing.T) {
for description, tc := range testCases {
t.Run(description, func(t *testing.T) {
client := &Connect{client: tc.mockClient()}
item, err := client.GetItemByID("vault-id", "item-id")
item, err := client.GetItemByID(context.Background(), "vault-id", "item-id")
tc.check(t, item, err)
})
}
@@ -110,7 +111,7 @@ func TestConnect_GetItemsByTitle(t *testing.T) {
for description, tc := range testCases {
t.Run(description, func(t *testing.T) {
client := &Connect{client: tc.mockClient()}
items, err := client.GetItemsByTitle("vault-id", "item-title")
items, err := client.GetItemsByTitle(context.Background(), "vault-id", "item-title")
tc.check(t, items, err)
})
}
@@ -152,7 +153,7 @@ func TestConnect_GetFileContent(t *testing.T) {
for description, tc := range testCases {
t.Run(description, func(t *testing.T) {
client := &Connect{client: tc.mockClient()}
content, err := client.GetFileContent("vault-id", "item-id", "file-id")
content, err := client.GetFileContent(context.Background(), "vault-id", "item-id", "file-id")
tc.check(t, content, err)
})
}
@@ -224,7 +225,7 @@ func TestConnect_GetVaultsByTitle(t *testing.T) {
for description, tc := range testCases {
t.Run(description, func(t *testing.T) {
client := &Connect{client: tc.mockClient()}
vault, err := client.GetVaultsByTitle(VaultTitleEmployee)
vault, err := client.GetVaultsByTitle(context.Background(), VaultTitleEmployee)
tc.check(t, vault, err)
})
}