-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
142 lines (116 loc) · 3.03 KB
/
client.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
package meili
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
)
// ErrNoKeysProvided occurs when instantiating a meili client with
// no access keys set and keys not disabled
var ErrNoKeysProvided = errors.New("meili: no key provided and WithNoKeys not set")
// a ClientOption is used to configure the client with less-standard options
type ClientOption func(c *Client) error
// DefaultClientOptions are applied automatically
// but can be easily overridden by user supplied ClientOptions
var DefaultClientOptions = []ClientOption{
WithHTTPClient(http.DefaultClient),
}
func WithNoKeys() ClientOption {
return func(c *Client) error {
if !c.hasNoKeys() {
return errors.New("meili: cannot set WithNoKeys if any keys are provided")
}
c.allowNoKeys = true
return nil
}
}
// WithMasterKey allows the Client to access all MeiliSearch routes
func WithMasterKey(masterKey string) ClientOption {
return func(c *Client) error {
if c.allowNoKeys {
return errors.New("meili: cannot set a master key if WithNoKeys was explicitly set")
}
c.masterKey = masterKey
return nil
}
}
// WithPrivateKey allows for access to all MeiliSearch except for the ability to manage keys
func WithPrivateKey(privateKey string) ClientOption {
return func(c *Client) error {
if c.allowNoKeys {
return errors.New("meili: cannot set a private key if WithNoKeys was explicitly set")
}
c.privateKey = privateKey
return nil
}
}
// WithPublicKey only allows for the ability to search
func WithPublicKey(publicKey string) ClientOption {
return func(c *Client) error {
if c.allowNoKeys {
return errors.New("meili: cannot set a public key if WithNoKeys was explicitly set")
}
c.publicKey = publicKey
return nil
}
}
func WithHTTPClient(httpClient *http.Client) ClientOption {
return func(c *Client) error {
c.httpClient = httpClient
return nil
}
}
// Client is the core meili search client
type Client struct {
httpClient *http.Client
address string
addressURL url.URL
allowNoKeys bool
masterKey string
privateKey string
publicKey string
}
func NewClient(address string, opts ...ClientOption) (*Client, error) {
addressURL, err := url.Parse(address)
if err != nil {
return nil, fmt.Errorf("meili: could not parse passed address %q: %w", address, err)
}
c := &Client{
address: address,
addressURL: *addressURL,
}
for _, opt := range DefaultClientOptions {
err := opt(c)
if err != nil {
return nil, err
}
}
for _, opt := range opts {
err := opt(c)
if err != nil {
return nil, err
}
}
if !c.allowNoKeys {
if c.masterKey == "" && c.publicKey == "" && c.privateKey == "" {
return nil, ErrNoKeysProvided
}
}
return c, nil
}
// WipeForTests is a convienient function to wipe every index in the connected Meili instance.
// this can be very dangerous.
func (c *Client) WipeForTests() error {
indexes, err := c.ListIndexes(context.TODO())
if err != nil {
return err
}
for _, i := range indexes {
err = c.DeleteIndex(context.TODO(), i.UID)
if err != nil {
return err
}
}
return nil
}