forked from meilisearch/meilisearch-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_index.go
118 lines (108 loc) · 3.13 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
package meilisearch
import (
"net/http"
"strconv"
)
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 *TaskInfo, err error) {
request := &CreateIndexRequest{
UID: config.Uid,
PrimaryKey: config.PrimaryKey,
}
resp = &TaskInfo{}
req := internalRequest{
endpoint: "/indexes",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: request,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "CreateIndex",
}
if err := c.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) GetIndexes(param *IndexesQuery) (resp *IndexesResults, err error) {
resp = &IndexesResults{}
req := internalRequest{
endpoint: "/indexes",
method: http.MethodGet,
withRequest: nil,
withResponse: &resp,
withQueryParams: map[string]string{},
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetIndexes",
}
if param != nil && param.Limit != 0 {
req.withQueryParams["limit"] = strconv.FormatInt(param.Limit, 10)
}
if param != nil && param.Offset != 0 {
req.withQueryParams["offset"] = strconv.FormatInt(param.Offset, 10)
}
if err := c.executeRequest(req); err != nil {
return nil, err
}
for i := range resp.Results {
resp.Results[i].client = c
}
return resp, nil
}
func (c *Client) GetRawIndexes(param *IndexesQuery) (resp map[string]interface{}, err error) {
resp = map[string]interface{}{}
req := internalRequest{
endpoint: "/indexes",
method: http.MethodGet,
withRequest: nil,
withResponse: &resp,
withQueryParams: map[string]string{},
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetRawIndexes",
}
if param != nil && param.Limit != 0 {
req.withQueryParams["limit"] = strconv.FormatInt(param.Limit, 10)
}
if param != nil && param.Offset != 0 {
req.withQueryParams["offset"] = strconv.FormatInt(param.Offset, 10)
}
if err := c.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) DeleteIndex(uid string) (resp *TaskInfo, err error) {
resp = &TaskInfo{}
req := internalRequest{
endpoint: "/indexes/" + uid,
method: http.MethodDelete,
withRequest: nil,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "DeleteIndex",
}
if err := c.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}