This repository has been archived by the owner on Oct 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrequestor.go
124 lines (103 loc) · 2.59 KB
/
requestor.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
119
120
121
122
123
124
package ldclient
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/facebookgo/httpcontrol"
"github.com/gregjones/httpcache"
)
// SDK endpoints
const (
LatestFlagsPath = "/sdk/latest-flags"
LatestSegmentsPath = "/sdk/latest-segments"
LatestAllPath = "/sdk/latest-all"
)
type requestor struct {
sdkKey string
httpClient *http.Client
config Config
}
func newRequestor(sdkKey string, config Config) *requestor {
baseTransport := httpcontrol.Transport{
RequestTimeout: config.Timeout,
DialTimeout: config.Timeout,
DialKeepAlive: 1 * time.Minute,
MaxTries: 3,
}
cachingTransport := &httpcache.Transport{
Cache: httpcache.NewMemoryCache(),
MarkCachedResponses: true,
Transport: &baseTransport,
}
httpClient := cachingTransport.Client()
httpRequestor := requestor{
sdkKey: sdkKey,
httpClient: httpClient,
config: config,
}
return &httpRequestor
}
func (r *requestor) requestAll() (allData, bool, error) {
var data allData
body, cached, err := r.makeRequest(LatestAllPath)
if err != nil {
return allData{}, false, err
}
if cached {
return allData{}, true, nil
}
jsonErr := json.Unmarshal(body, &data)
if jsonErr != nil {
return allData{}, false, jsonErr
}
return data, cached, nil
}
func (r *requestor) requestResource(kind VersionedDataKind, key string) (VersionedData, error) {
var resource string
switch kind {
case SegmentVersionedDataKind{}:
resource = LatestSegmentsPath + "/" + key
case FeatureFlagVersionedDataKind{}:
resource = LatestFlagsPath + "/" + key
default:
return nil, fmt.Errorf("unexpected item type: %s", kind)
}
body, _, err := r.makeRequest(resource)
if err != nil {
return nil, err
}
item := kind.GetDefaultItem().(VersionedData)
err = json.Unmarshal(body, item)
if err != nil {
return nil, err
}
return item, nil
}
func (r *requestor) makeRequest(resource string) ([]byte, bool, error) {
req, reqErr := http.NewRequest("GET", r.config.BaseUri+resource, nil)
if reqErr != nil {
return nil, false, reqErr
}
url := req.URL.String()
req.Header.Add("Authorization", r.sdkKey)
req.Header.Add("User-Agent", r.config.UserAgent)
res, resErr := r.httpClient.Do(req)
if resErr != nil {
return nil, false, resErr
}
defer func() {
_, _ = ioutil.ReadAll(res.Body)
_ = res.Body.Close()
}()
if err := checkForHttpError(res.StatusCode, url); err != nil {
return nil, false, err
}
cached := res.Header.Get(httpcache.XFromCache) != ""
body, ioErr := ioutil.ReadAll(res.Body)
if ioErr != nil {
return nil, false, ioErr
}
return body, cached, nil
}