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

@@ -20,8 +20,8 @@ type SDK struct {
client *sdk.Client
}
func NewClient(config Config) (*SDK, error) {
client, err := sdk.NewClient(context.Background(),
func NewClient(ctx context.Context, config Config) (*SDK, error) {
client, err := sdk.NewClient(ctx,
sdk.WithServiceAccountToken(config.ServiceAccountToken),
sdk.WithIntegrationInfo(config.IntegrationName, config.IntegrationVersion),
)
@@ -34,8 +34,8 @@ func NewClient(config Config) (*SDK, error) {
}, nil
}
func (s *SDK) GetItemByID(vaultID, itemID string) (*model.Item, error) {
sdkItem, err := s.client.Items().Get(context.Background(), vaultID, itemID)
func (s *SDK) GetItemByID(ctx context.Context, vaultID, itemID string) (*model.Item, error) {
sdkItem, err := s.client.Items().Get(ctx, vaultID, itemID)
if err != nil {
return nil, fmt.Errorf("1password sdk error: %w", err)
}
@@ -45,9 +45,9 @@ func (s *SDK) GetItemByID(vaultID, itemID string) (*model.Item, error) {
return &item, nil
}
func (s *SDK) GetItemsByTitle(vaultID, itemTitle string) ([]model.Item, error) {
func (s *SDK) GetItemsByTitle(ctx context.Context, vaultID, itemTitle string) ([]model.Item, error) {
// Get all items in the vault
sdkItems, err := s.client.Items().List(context.Background(), vaultID)
sdkItems, err := s.client.Items().List(ctx, vaultID)
if err != nil {
return nil, fmt.Errorf("1password sdk error: %w", err)
}
@@ -65,8 +65,8 @@ func (s *SDK) GetItemsByTitle(vaultID, itemTitle string) ([]model.Item, error) {
return items, nil
}
func (s *SDK) GetFileContent(vaultID, itemID, fileID string) ([]byte, error) {
bytes, err := s.client.Items().Files().Read(context.Background(), vaultID, itemID, sdk.FileAttributes{
func (s *SDK) GetFileContent(ctx context.Context, vaultID, itemID, fileID string) ([]byte, error) {
bytes, err := s.client.Items().Files().Read(ctx, vaultID, itemID, sdk.FileAttributes{
ID: fileID,
})
if err != nil {
@@ -76,9 +76,9 @@ func (s *SDK) GetFileContent(vaultID, itemID, fileID string) ([]byte, error) {
return bytes, nil
}
func (s *SDK) GetVaultsByTitle(title string) ([]model.Vault, error) {
func (s *SDK) GetVaultsByTitle(ctx context.Context, title string) ([]model.Vault, error) {
// List all vaults
sdkVaults, err := s.client.Vaults().List(context.Background())
sdkVaults, err := s.client.Vaults().List(ctx)
if err != nil {
return nil, fmt.Errorf("1password sdk error: %w", err)
}

View File

@@ -54,7 +54,7 @@ func TestSDK_GetItemByID(t *testing.T) {
ItemsAPI: tc.mockItemAPI(),
},
}
item, err := client.GetItemByID("vault-id", "item-id")
item, err := client.GetItemByID(context.Background(), "vault-id", "item-id")
tc.check(t, item, err)
})
}
@@ -123,7 +123,7 @@ func TestSDK_GetItemsByTitle(t *testing.T) {
ItemsAPI: tc.mockItemAPI(),
},
}
items, err := client.GetItemsByTitle("vault-id", "item-title")
items, err := client.GetItemsByTitle(context.Background(), "vault-id", "item-title")
tc.check(t, items, err)
})
}
@@ -185,7 +185,7 @@ func TestSDK_GetFileContent(t *testing.T) {
ItemsAPI: tc.mockItemAPI(),
},
}
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)
})
}
@@ -262,7 +262,7 @@ func TestSDK_GetVaultsByTitle(t *testing.T) {
VaultsAPI: tc.mockVaultAPI(),
},
}
vault, err := client.GetVaultsByTitle(VaultTitleEmployee)
vault, err := client.GetVaultsByTitle(context.Background(), VaultTitleEmployee)
tc.check(t, vault, err)
})
}