-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
70 lines (61 loc) · 2.04 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
package quickwit
import (
"fmt"
"github.com/imroc/req/v3"
)
type ErrorMessage struct {
Message string `json:"message"`
}
// Error implements go error interface.
func (msg *ErrorMessage) Error() string {
return fmt.Sprintf("API Error: %s", msg.Message)
}
type QuickwitClient struct {
*req.Client
}
// endpoint is the root url of the quickwit server.
func NewQuickwitClient(endpoint string) *QuickwitClient {
return &QuickwitClient{
Client: req.C().
SetBaseURL(endpoint).
SetCommonErrorResult(&ErrorMessage{}).
EnableDumpEachRequest().
OnAfterResponse(func(client *req.Client, resp *req.Response) error {
if resp.Err != nil { // There is an underlying error, e.g. network error or unmarshal error.
return nil
}
if errMsg, ok := resp.ErrorResult().(*ErrorMessage); ok {
resp.Err = errMsg // Convert api error into go error
return nil
}
if !resp.IsSuccessState() {
// Neither a success response nor a error response, record details to help troubleshooting
resp.Err = fmt.Errorf("bad status: %s\nraw content:\n%s", resp.Status, resp.Dump())
}
return nil
}),
}
}
type SearchRequest struct {
Query string `json:"query"`
SearchFields []string `json:"search_field,omitempty"`
StartTimestamp int64 `json:"start_timestamp,omitempty"`
EndTimestamp int64 `json:"end_timestamp,omitempty"`
MaxHits uint64 `json:"max_hits"`
StartOffset uint64 `json:"start_offset,omitempty"`
SortByField string `json:"sort_by_field,omitempty"`
}
type SearchResponse struct {
NumHits uint64 `json:"num_hits"`
Hits []interface{} `json:"hits"`
ElapsedTimeMicros uint64 `json:"elapsed_time_micros"`
Aggregations interface{} `json:"aggregations,omitempty"`
}
func (c *QuickwitClient) Search(indexId string, searchRequest SearchRequest) (searchResponse *SearchResponse, err error) {
_, err = c.R().
SetPathParam("indexId", indexId).
SetBody(searchRequest).
SetSuccessResult(&searchResponse).
Post("/api/v1/{indexId}/search")
return
}