forked from vulncheck-oss/sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cursor_index.go
74 lines (59 loc) · 1.99 KB
/
cursor_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
package sdk
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
type IndexMetaCursor struct {
Timestamp string `json:"timestamp"`
Index string `json:"index"`
Limit int `json:"limit"`
TotalDocuments int `json:"total_documents"`
Sort string `json:"sort"`
Parameters []IndexMetaParameters `json:"parameters"`
Order string `json:"order"`
Cursor string `json:"cursor"`
NextCursor string `json:"next_cursor"`
PrevCursor string `json:"prev_cursor"` // this may not exist
}
type IndexCursorResponse struct {
Benchmark float64 `json:"_benchmark"`
Meta IndexMetaCursor `json:"_meta"`
Data []interface{} `json:"data"`
}
// https://docs.vulncheck.com/api/cursor
func (c *Client) GetCursorIndex(index string, cursor string, queryParameters ...IndexQueryParameters) (responseJSON *IndexCursorResponse, err error) {
client := &http.Client{}
req, err := http.NewRequest("GET", c.GetUrl()+"/v3/index/"+url.QueryEscape(index)+"/cursor", nil)
if err != nil {
panic(err)
}
c.SetAuthHeader(req)
query := req.URL.Query()
setIndexQueryParameters(query, queryParameters...)
if cursor != "" {
query.Add("cursor", cursor)
}
req.URL.RawQuery = query.Encode()
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
var metaError MetaError
_ = json.NewDecoder(resp.Body).Decode(&metaError)
return nil, fmt.Errorf("error: %v", metaError.Errors)
}
_ = json.NewDecoder(resp.Body).Decode(&responseJSON)
return responseJSON, nil
}
// Strings representation of the response
func (r IndexCursorResponse) String() string {
return fmt.Sprintf("Benchmark: %f\nMeta: %v\nData: %v\n", r.Benchmark, r.Meta, r.Data)
}
// GetData - Returns the data from the response
func (r IndexCursorResponse) GetData() []interface{} {
return r.Data
}