forked from meilisearch/meilisearch-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_index.go
123 lines (113 loc) · 3.28 KB
/
client_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
package meilisearch
import "net/http"
func (c *Client) Index(uid string) *Index {
return newIndex(c, uid)
}
func (c *Client) GetIndex(uid string) (resp *Index, err error) {
return newIndex(c, uid).FetchInfo()
}
func (c *Client) GetRawIndex(uid string) (resp map[string]interface{}, err error) {
resp = map[string]interface{}{}
req := internalRequest{
endpoint: "/indexes/" + uid,
method: http.MethodGet,
withRequest: nil,
withResponse: &resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetRawIndex",
}
if err := c.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) CreateIndex(config *IndexConfig) (resp *Index, err error) {
request := &CreateIndexRequest{
UID: config.Uid,
PrimaryKey: config.PrimaryKey,
}
resp = newIndex(c, config.Uid)
req := internalRequest{
endpoint: "/indexes",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: request,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusCreated},
functionName: "CreateIndex",
}
if err := c.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) GetAllIndexes() (resp []*Index, err error) {
resp = []*Index{}
req := internalRequest{
endpoint: "/indexes",
method: http.MethodGet,
withRequest: nil,
withResponse: &resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetAllIndexes",
}
if err := c.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) GetAllRawIndexes() (resp []map[string]interface{}, err error) {
resp = []map[string]interface{}{}
req := internalRequest{
endpoint: "/indexes",
method: http.MethodGet,
withRequest: nil,
withResponse: &resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetAllRawIndexes",
}
if err := c.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) GetOrCreateIndex(config *IndexConfig) (resp *Index, err error) {
resp, err = c.GetIndex(config.Uid)
if err == nil {
return resp, err
}
return c.CreateIndex(config)
}
func (c *Client) DeleteIndex(uid string) (ok bool, err error) {
req := internalRequest{
endpoint: "/indexes/" + uid,
method: http.MethodDelete,
withRequest: nil,
withResponse: nil,
acceptedStatusCodes: []int{http.StatusNoContent},
functionName: "DeleteIndex",
}
// err is not nil if status code is not 204 StatusNoContent
if err := c.executeRequest(req); err != nil {
return false, err
}
return true, nil
}
func (c *Client) DeleteIndexIfExists(uid string) (ok bool, err error) {
req := internalRequest{
endpoint: "/indexes/" + uid,
method: http.MethodDelete,
withRequest: nil,
withResponse: nil,
acceptedStatusCodes: []int{http.StatusNoContent},
functionName: "DeleteIndex",
}
// err is not nil if status code is not 204 StatusNoContent
if err := c.executeRequest(req); err != nil {
if err.(*Error).MeilisearchApiError.Code != "index_not_found" {
return false, err
}
return false, nil
}
return true, nil
}