Skip to content

Commit 4a00b8b

Browse files
authored
Replace resty with net/http (#106)
* Replace resty with net/http * SetProxy is a noop when proxyURL is empty * Setup tranport to match default * Set request content type
1 parent 3c23c48 commit 4a00b8b

File tree

8 files changed

+368
-55
lines changed

8 files changed

+368
-55
lines changed

analytics.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"sync"
77
"time"
88

9-
"github.com/go-resty/resty/v2"
9+
"github.com/Flagsmith/flagsmith-go-client/v3/internal/flaghttp"
1010
)
1111

1212
const AnalyticsTimerInMilli = 10 * 1000
@@ -17,13 +17,13 @@ type analyticDataStore struct {
1717
data map[string]int
1818
}
1919
type AnalyticsProcessor struct {
20-
client *resty.Client
20+
client flaghttp.Client
2121
store *analyticDataStore
2222
endpoint string
2323
log Logger
2424
}
2525

26-
func NewAnalyticsProcessor(ctx context.Context, client *resty.Client, baseURL string, timerInMilli *int, log Logger) *AnalyticsProcessor {
26+
func NewAnalyticsProcessor(ctx context.Context, client flaghttp.Client, baseURL string, timerInMilli *int, log Logger) *AnalyticsProcessor {
2727
data := make(map[string]int)
2828
dataStore := analyticDataStore{data: data}
2929
tickerInterval := AnalyticsTimerInMilli

analytics_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import (
99
"testing"
1010
"time"
1111

12-
"github.com/go-resty/resty/v2"
1312
"github.com/stretchr/testify/assert"
13+
14+
"github.com/Flagsmith/flagsmith-go-client/v3/internal/flaghttp"
1415
)
1516

1617
const BaseURL = "http://localhost:8000/api/v1/"
@@ -38,7 +39,7 @@ func TestAnalytics(t *testing.T) {
3839
analyticsTimer := 10
3940

4041
// and, the http client
41-
client := resty.New()
42+
client := flaghttp.NewClient()
4243
client.SetHeader("X-Environment-Key", EnvironmentAPIKey)
4344

4445
// Now let's create the processor

client.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import (
88
"sync/atomic"
99
"time"
1010

11-
"github.com/go-resty/resty/v2"
12-
1311
"github.com/Flagsmith/flagsmith-go-client/v3/flagengine"
1412
"github.com/Flagsmith/flagsmith-go-client/v3/flagengine/environments"
1513
"github.com/Flagsmith/flagsmith-go-client/v3/flagengine/identities"
1614
"github.com/Flagsmith/flagsmith-go-client/v3/flagengine/segments"
15+
"github.com/Flagsmith/flagsmith-go-client/v3/internal/flaghttp"
1716

1817
. "github.com/Flagsmith/flagsmith-go-client/v3/flagengine/identities/traits"
1918
)
@@ -28,7 +27,7 @@ type Client struct {
2827
analyticsProcessor *AnalyticsProcessor
2928
defaultFlagHandler func(string) (Flag, error)
3029

31-
client *resty.Client
30+
client flaghttp.Client
3231
ctxLocalEval context.Context
3332
ctxAnalytics context.Context
3433
log Logger
@@ -39,7 +38,7 @@ func NewClient(apiKey string, options ...Option) *Client {
3938
c := &Client{
4039
apiKey: apiKey,
4140
config: defaultConfig(),
42-
client: resty.New(),
41+
client: flaghttp.NewClient(),
4342
}
4443

4544
c.client.SetHeaders(map[string]string{

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.19
44

55
require (
66
github.com/blang/semver/v4 v4.0.0
7-
github.com/go-resty/resty/v2 v2.10.0
87
github.com/google/uuid v1.3.1
98
github.com/stretchr/testify v1.8.4
109
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1
@@ -13,6 +12,5 @@ require (
1312
require (
1413
github.com/davecgh/go-spew v1.1.1 // indirect
1514
github.com/pmezard/go-difflib v1.0.0 // indirect
16-
golang.org/x/net v0.17.0 // indirect
1715
gopkg.in/yaml.v3 v3.0.1 // indirect
1816
)

go.sum

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,14 @@ github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM
22
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
33
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5-
github.com/go-resty/resty/v2 v2.10.0 h1:Qla4W/+TMmv0fOeeRqzEpXPLfTUnR5HZ1+lGs+CkiCo=
6-
github.com/go-resty/resty/v2 v2.10.0/go.mod h1:iiP/OpA0CkcL3IGt1O0+/SIItFUbkkyw5BGXiVdTu+A=
75
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
86
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
97
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
108
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
119
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
1210
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
13-
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
14-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
15-
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
16-
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
1711
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw=
1812
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
19-
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
20-
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
21-
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
22-
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
23-
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
24-
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
25-
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
26-
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
27-
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
28-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
29-
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
30-
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
31-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
32-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
33-
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
34-
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
35-
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
36-
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
37-
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
38-
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
39-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
40-
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
41-
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
42-
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
43-
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
44-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
45-
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
46-
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
47-
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
48-
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
49-
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
50-
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
51-
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
52-
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
53-
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
54-
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
55-
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
56-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
5713
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
5814
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
5915
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

internal/flaghttp/client.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package flaghttp
2+
3+
import (
4+
"net"
5+
"net/http"
6+
"net/url"
7+
"time"
8+
)
9+
10+
type Logger interface {
11+
Errorf(format string, v ...any)
12+
Warnf(format string, v ...any)
13+
Debugf(format string, v ...any)
14+
}
15+
16+
type Client interface {
17+
NewRequest() Request
18+
R() Request
19+
SetHeader(header, value string) Client
20+
SetHeaders(headers map[string]string) Client
21+
SetLogger(l Logger) Client
22+
SetProxy(proxyURL string) Client
23+
SetRetryCount(count int) Client
24+
SetRetryWaitTime(waitTime time.Duration) Client
25+
SetTimeout(timeout time.Duration) Client
26+
}
27+
28+
type client struct {
29+
logger Logger
30+
retryCount int
31+
retryWait time.Duration
32+
header http.Header
33+
transport *http.Transport
34+
}
35+
36+
func NewClient() Client {
37+
return &client{
38+
header: http.Header{},
39+
transport: &http.Transport{
40+
Proxy: http.ProxyFromEnvironment,
41+
DialContext: (&net.Dialer{
42+
Timeout: 30 * time.Second,
43+
KeepAlive: 30 * time.Second,
44+
}).DialContext,
45+
ForceAttemptHTTP2: true,
46+
MaxIdleConns: 100,
47+
IdleConnTimeout: 90 * time.Second,
48+
TLSHandshakeTimeout: 10 * time.Second,
49+
ExpectContinueTimeout: 1 * time.Second,
50+
},
51+
}
52+
}
53+
54+
func (c *client) NewRequest() Request {
55+
return c.R()
56+
}
57+
58+
func (c *client) R() Request {
59+
return &request{
60+
client: c,
61+
}
62+
}
63+
64+
func (c *client) SetHeader(header, value string) Client {
65+
c.header.Set(header, value)
66+
67+
return c
68+
}
69+
70+
func (c *client) SetHeaders(headers map[string]string) Client {
71+
for k, v := range headers {
72+
c.header.Set(k, v)
73+
}
74+
75+
return c
76+
}
77+
78+
func (c *client) SetLogger(l Logger) Client {
79+
c.logger = l
80+
81+
return c
82+
}
83+
84+
func (c *client) SetProxy(proxyURL string) Client {
85+
if proxyURL == "" {
86+
return c
87+
}
88+
89+
u, err := url.Parse(proxyURL)
90+
if err != nil {
91+
c.logger.Warnf("Failed to parse proxy URL: %s", err)
92+
return c
93+
}
94+
95+
c.transport.Proxy = http.ProxyURL(u)
96+
97+
return c
98+
}
99+
100+
func (c *client) SetRetryCount(count int) Client {
101+
c.retryCount = count
102+
103+
return c
104+
}
105+
106+
func (c *client) SetRetryWaitTime(waitTime time.Duration) Client {
107+
c.retryWait = waitTime
108+
109+
return c
110+
}
111+
112+
func (c *client) SetTimeout(timeout time.Duration) Client {
113+
c.transport.ResponseHeaderTimeout = timeout
114+
115+
return c
116+
}

0 commit comments

Comments
 (0)