-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.go
188 lines (152 loc) · 5.33 KB
/
collection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package sajari
import (
"context"
"errors"
"fmt"
"strings"
"google.golang.org/grpc/codes"
"code.sajari.com/sdk-go/internal/openapi"
)
var (
// ErrNoSuchCollection is returned when a collection was requested but there
// is no such collection.
ErrNoSuchCollection = errors.New("no such collection")
// ErrNoSuchCollectionDefaultPipeline is returned when a collection default
// pipeline was requested but there is no such default.
ErrNoSuchCollectionDefaultPipeline = errors.New("no such collection default pipeline")
)
// A Collection stores the records that can be searched.
type Collection = openapi.Collection
// GetCollection gets a collection identified by the provided ID.
//
// If there is no such collection matching the given ID this method returns an
// error wrapping ErrNoSuchCollection.
func (c *Client) GetCollection(ctx context.Context, id string) (*Collection, error) {
if !c.v4 {
return nil, errors.New("not supported on non-v4 endpoints")
}
if id == "" {
return nil, errors.New("collection id cannot be empty")
}
ctx = context.WithValue(ctx, openapi.ContextBasicAuth, c.openAPI.auth)
req := c.openAPI.client.CollectionsApi.GetCollection(ctx, id)
collection, _, err := req.Execute()
if err != nil {
if ok, err := collectionsHandleGenericOpenAPIError(err, ErrNoSuchCollection); ok {
return nil, err
}
return nil, fmt.Errorf("could not get collection: %w", err)
}
return &collection, nil
}
// UpdateCollectionOpt is a type which defines options to update a collection.
type UpdateCollectionOpt func(c *openapi.Collection, updateMask map[string]struct{})
// SetCollectionDisplayName is a collection mutation that set a collection's
// display name.
func SetCollectionDisplayName(displayName string) UpdateCollectionOpt {
return func(c *openapi.Collection, updateMask map[string]struct{}) {
c.DisplayName = displayName
updateMask["display_name"] = struct{}{}
}
}
// SetAuthorizedQueryDomains is a collection mutation that set a collection's
// authorized query domains.
func SetAuthorizedQueryDomains(domains []string) UpdateCollectionOpt {
return func(c *openapi.Collection, updateMask map[string]struct{}) {
c.AuthorizedQueryDomains = &domains
updateMask["authorized_query_domains"] = struct{}{}
}
}
// UpdateCollection updates a collection identified by the provided ID.
//
// If there is no such collection matching the given ID this method returns an
// error wrapping ErrNoSuchCollection.
func (c *Client) UpdateCollection(ctx context.Context, id string, opts ...UpdateCollectionOpt) error {
if !c.v4 {
return errors.New("not supported on non-v4 endpoints")
}
if id == "" {
return errors.New("collection id cannot be empty")
}
col := &openapi.Collection{}
updateMask := map[string]struct{}{}
for _, opt := range opts {
opt(col, updateMask)
}
um := make([]string, 0, len(updateMask))
for f := range updateMask {
um = append(um, f)
}
ctx = context.WithValue(ctx, openapi.ContextBasicAuth, c.openAPI.auth)
req := c.openAPI.client.CollectionsApi.
UpdateCollection(ctx, id).
Collection(*col).
UpdateMask(strings.Join(um, ","))
_, _, err := req.Execute()
if err != nil {
if ok, err := collectionsHandleGenericOpenAPIError(err, ErrNoSuchCollection); ok {
return err
}
return fmt.Errorf("could not update collection: %w", err)
}
return nil
}
// DeleteCollection removes a collection identified by the provided ID.
//
// If there is no such collection matching the given ID this method returns an
// error wrapping ErrNoSuchCollection.
func (c *Client) DeleteCollection(ctx context.Context, id string) error {
if !c.v4 {
return errors.New("not supported on non-v4 endpoints")
}
ctx = context.WithValue(ctx, openapi.ContextBasicAuth, c.openAPI.auth)
_, _, err := c.openAPI.client.CollectionsApi.DeleteCollection(ctx, id).Execute()
if err != nil {
if ok, err := collectionsHandleGenericOpenAPIError(err, ErrNoSuchCollection); ok {
return err
}
return fmt.Errorf("could not delete collection: %w", err)
}
return nil
}
// GetDefaultPipeline gets the default pipeline for a collection.
func (c *Client) GetDefaultPipeline(ctx context.Context, id string, typ PipelineType) (string, error) {
if !c.v4 {
return "", errors.New("not supported on non-v4 endpoints")
}
if id == "" {
return "", errors.New("collection id cannot be empty")
}
if typ == "" {
return "", errors.New("type cannot be empty")
}
ctx = context.WithValue(ctx, openapi.ContextBasicAuth, c.openAPI.auth)
req := c.openAPI.client.PipelinesApi.
GetDefaultPipeline(ctx, id).
Type_(string(typ))
resp, _, err := req.Execute()
if err != nil {
if ok, err := collectionsHandleGenericOpenAPIError(err, ErrNoSuchCollectionDefaultPipeline); ok {
return "", err
}
return "", fmt.Errorf("could not get default pipeline: %w", err)
}
return resp.GetPipeline(), nil
}
// collectionsHandleGenericOpenAPIError handles generic OpenAPI errors in the
// context of collections. E.g. 404 is converted to errNotFound.
func collectionsHandleGenericOpenAPIError(err error, errNotFound error) (handled bool, rerr error) {
switch x := err.(type) {
case openapi.GenericOpenAPIError:
m := x.Model()
if m, ok := m.(openapi.Error); ok {
switch codes.Code(m.GetCode()) {
case codes.NotFound:
return true, fmt.Errorf("%w", errNotFound)
default:
return true, fmt.Errorf("%s: %w", m.GetMessage(), err)
}
}
}
return false, nil
}