-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatasets.go
More file actions
60 lines (49 loc) · 1.51 KB
/
datasets.go
File metadata and controls
60 lines (49 loc) · 1.51 KB
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
package gospice
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)
type RefreshMode string
const (
RefreshModeFull RefreshMode = "full"
RefreshModeAppend RefreshMode = "append"
)
type DatasetRefreshRequest struct {
RefreshSQL *string `json:"refresh_sql,omitempty"`
Mode *RefreshMode `json:"refresh_mode,omitempty"`
MaxJitter *string `json:"refresh_jitter_max,omitempty"`
}
func (c *SpiceClient) RefreshDataset(ctx context.Context, dataset string, opts *DatasetRefreshRequest) error {
var jsonData []byte
var err error
if opts == nil {
jsonData = []byte("{}")
} else {
jsonData, err = json.Marshal(opts)
if err != nil {
return fmt.Errorf("error marshaling DatasetRefreshRequest opts: %w", err)
}
}
body := bytes.NewBuffer(jsonData)
url := fmt.Sprintf("%s/v1/datasets/%s/acceleration/refresh", c.baseHttpUrl, dataset)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, body)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}
req = req.WithContext(c.traceHttpRequest(ctx, "RefreshDataset", req))
req.Header.Set("X-API-Key", c.apiKey)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("user-agent", c.userAgent)
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("error executing request: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusCreated {
return fmt.Errorf("POST %s failed with status=%d. body=%v", url, resp.StatusCode, body)
}
return nil
}