items.go

v1.0.0
Doc Versions Source
1
package bitwarden
2
3
import (
4
	"context"
5
	"net/url"
6
)
7
8
// ItemsService handles vault item operations.
9
type ItemsService struct {
10
	t *transport
11
}
12
13
// List returns vault items matching the given filters.
14
func (s *ItemsService) List(ctx context.Context, filter ListFilter) ([]Item, error) {
15
	q := listFilterToQuery(filter)
16
	data, err := s.t.get(ctx, "/list/object/items", q)
17
	if err != nil {
18
		return nil, err
19
	}
20
	return decodeListResponse[Item](data)
21
}
22
23
// Get returns a single vault item by ID.
24
func (s *ItemsService) Get(ctx context.Context, id string) (*Item, error) {
25
	data, err := s.t.get(ctx, "/object/item/"+id, nil)
26
	if err != nil {
27
		return nil, err
28
	}
29
	return decodeObjectResponsePtr[Item](data)
30
}
31
32
// Create creates a new vault item.
33
func (s *ItemsService) Create(ctx context.Context, item Item) (*Item, error) {
34
	data, err := s.t.post(ctx, "/object/item", item)
35
	if err != nil {
36
		return nil, err
37
	}
38
	return decodeObjectResponsePtr[Item](data)
39
}
40
41
// Update updates an existing vault item.
42
func (s *ItemsService) Update(ctx context.Context, id string, item Item) (*Item, error) {
43
	data, err := s.t.put(ctx, "/object/item/"+id, item)
44
	if err != nil {
45
		return nil, err
46
	}
47
	return decodeObjectResponsePtr[Item](data)
48
}
49
50
// Delete deletes a vault item (soft delete).
51
func (s *ItemsService) Delete(ctx context.Context, id string) error {
52
	_, err := s.t.del(ctx, "/object/item/"+id)
53
	return err
54
}
55
56
// Restore restores a soft-deleted vault item.
57
func (s *ItemsService) Restore(ctx context.Context, id string) error {
58
	_, err := s.t.post(ctx, "/restore/item/"+id, nil)
59
	return err
60
}
61
62
// Move moves a vault item to an organization with the given collection IDs.
63
func (s *ItemsService) Move(ctx context.Context, id, organizationID string, collectionIDs []string) error {
64
	_, err := s.t.post(ctx, "/move/"+id+"/"+organizationID, MoveRequest{CollectionIDs: collectionIDs})
65
	return err
66
}
67
68
func listFilterToQuery(f ListFilter) url.Values {
69
	q := url.Values{}
70
	if f.Search != "" {
71
		q.Set("search", f.Search)
72
	}
73
	if f.OrganizationID != "" {
74
		q.Set("organizationid", f.OrganizationID)
75
	}
76
	if f.CollectionID != "" {
77
		q.Set("collectionid", f.CollectionID)
78
	}
79
	if f.FolderID != "" {
80
		q.Set("folderid", f.FolderID)
81
	}
82
	if f.URL != "" {
83
		q.Set("url", f.URL)
84
	}
85
	if f.Trash {
86
		q.Set("trash", "true")
87
	}
88
	return q
89
}
90
91
func decodeObjectResponsePtr[T any](data []byte) (*T, error) {
92
	v, err := decodeObjectResponse[T](data)
93
	if err != nil {
94
		return nil, err
95
	}
96
	return &v, nil
97
}
98

Source Files