-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfeature_api.go
74 lines (60 loc) · 1.72 KB
/
feature_api.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 growthbook
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/growthbook/growthbook-golang/internal/condition"
)
type FeatureApiResponse struct {
Status int `json:"status"`
Features FeatureMap `json:"features"`
DateUpdated time.Time `json:"dateUpdated"`
SavedGroups condition.SavedGroups `json:"savedGroups"`
EncryptedFeatures string `json:"encryptedFeatures"`
SseSupport bool
Etag string
}
const userAgent = "Growhthbook Go SDK client"
func (c *Client) CallFeatureApi(ctx context.Context, etag string) (*FeatureApiResponse, error) {
apiResp := FeatureApiResponse{}
apiUrl := c.data.getApiUrl()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiUrl, nil)
if err != nil {
return nil, err
}
setReqHeaders(req, etag)
resp, err := c.data.httpClient.Do(req)
if err != nil {
return nil, err
}
apiResp.Status = resp.StatusCode
apiResp.Etag = resp.Header.Get("etag")
apiResp.SseSupport = resp.Header.Get("x-sse-support") == "enabled"
if resp.StatusCode == 304 {
return &apiResp, nil
}
if resp.StatusCode != 200 {
return &apiResp, fmt.Errorf("Error loading features, code: %d", resp.StatusCode)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return &apiResp, err
}
c.logger.Info("Loading features")
err = json.Unmarshal(body, &apiResp)
if err != nil {
c.logger.Error("Error parsing features response", "error", err)
return &apiResp, err
}
return &apiResp, err
}
func setReqHeaders(req *http.Request, etag string) {
req.Header.Set("User-Agent", userAgent)
if etag != "" {
req.Header.Add("If-None-Match", etag)
}
}