-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.go
141 lines (112 loc) · 2.98 KB
/
index.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
package meili
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
// MigrateIndexes adds or removes indexes and settings to ensure
// the requested ones exist
// func (c *Client) MigrateIndexes
// IndexSettings define how Meili handles each index
type IndexSettings struct {
PrimaryKey string
DistinctFields []string
}
// CreateIndex creates a new unique index with the given settings
func (c *Client) CreateIndex(ctx context.Context, name, uid string, is *IndexSettings) error {
key, err := c.getMasterOrPrivateKey()
if err != nil {
return err
}
data := map[string]interface{}{
"uid": uid,
"name": name,
}
if is != nil {
if is.PrimaryKey != "" {
data["primaryKey"] = is.PrimaryKey
}
}
body, err := json.Marshal(data)
if err != nil {
return err
}
r, err := http.NewRequest(http.MethodPost, c.getRoute(listIndexRoute), bytes.NewReader(body))
if err != nil {
return err
}
r = r.WithContext(ctx)
r.Header.Set("X-Meili-API-Key", key)
resp, err := c.makeRequest(r)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
return fmt.Errorf("meili: could not create index, got an unexpected status code '%d' (wanted a 201)", resp.StatusCode)
}
return nil
}
// a ListIndexElement is returned from (c *Client).ListIndexes
type ListIndexElement struct {
Name string `json:"name"`
UID string `json:"uid"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
PrimaryKey string `json:"primaryKey,omitempty"`
}
const listIndexRoute = "/indexes"
// ListIndexes lists all Indexes within the connected Meili instance
func (c *Client) ListIndexes(ctx context.Context) ([]ListIndexElement, error) {
key, err := c.getMasterOrPrivateKey()
if err != nil {
return nil, err
}
r, err := http.NewRequest(http.MethodGet, c.getRoute(listIndexRoute), nil)
if err != nil {
return nil, err
}
r = r.WithContext(ctx)
r.Header.Set("X-Meili-API-Key", key)
resp, err := c.makeRequest(r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var out []ListIndexElement
dec := json.NewDecoder(resp.Body)
dec.DisallowUnknownFields()
err = dec.Decode(&out)
if err != nil {
return nil, err
}
return out, nil
}
const deleteIndexRoutePrefix = "/indexes/"
func (c *Client) DeleteIndex(ctx context.Context, uid string) error {
key, err := c.getMasterOrPrivateKey()
if err != nil {
return err
}
r, err := http.NewRequest(http.MethodDelete, c.getRoute(deleteIndexRoutePrefix+uid), nil)
if err != nil {
return err
}
r = r.WithContext(ctx)
r.Header.Set("X-Meili-API-Key", key)
resp, err := c.makeRequest(r)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("meili: could not delete index, got an unexpected status code '%d' (wanted a 204)", resp.StatusCode)
}
return nil
}
func (c *Client) SetIndexSettings(ctx context.Context, uid string, is *IndexSettings) error {
return nil
}