Skip to content

Commit d98d108

Browse files
committed
Add default headers to HTTP client
1 parent 0cb8ce0 commit d98d108

File tree

5 files changed

+22
-8
lines changed

5 files changed

+22
-8
lines changed

clients/coinmarketcap.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"encoding/json"
55
"fmt"
66
"strconv"
7+
8+
"github.com/Pallinder/go-randomdata"
79
)
810

911
// coinmarketcap API urls
@@ -63,7 +65,7 @@ func (cac CoinmarketcapAPIClient) GetSymbols(
6365
"sortBy": sortBy,
6466
"sortType": sortType,
6567
}
66-
res, err := cac.httpClient.MakeGetRequest(CryptoListingAPIURL, queryParams)
68+
res, err := cac.httpClient.MakeGetRequest(CryptoListingAPIURL, queryParams, getDefaultHeaders())
6769
if err != nil {
6870
return nil, err
6971
}
@@ -81,7 +83,7 @@ func (cac CoinmarketcapAPIClient) GetCoinIDBySymbol(symbol string) (int, error)
8183
queryParams := map[string]string{
8284
"symbol": symbol,
8385
}
84-
res, err := cac.httpClient.MakeGetRequest(CryptoMapAPIURL, queryParams)
86+
res, err := cac.httpClient.MakeGetRequest(CryptoMapAPIURL, queryParams, getDefaultHeaders())
8587
if err != nil {
8688
return -1, err
8789
}
@@ -103,7 +105,7 @@ func (cac CoinmarketcapAPIClient) GetSymbolChart(
103105
"id": fmt.Sprintf("%d", symbolID),
104106
"range": rangeType,
105107
}
106-
res, err := cac.httpClient.MakeGetRequest(CryptoChartAPIURL, queryParams)
108+
res, err := cac.httpClient.MakeGetRequest(CryptoChartAPIURL, queryParams, getDefaultHeaders())
107109
if err != nil {
108110
return map[string]valuesChart{}, err
109111
}
@@ -115,6 +117,12 @@ func (cac CoinmarketcapAPIClient) GetSymbolChart(
115117
return responseData.Data.Points, nil
116118
}
117119

120+
func getDefaultHeaders() map[string]string {
121+
return map[string]string{
122+
"User-Agent": randomdata.UserAgentString(),
123+
}
124+
}
125+
118126
// CryptoChartResponse is a data structure with points to draw a chart
119127
type CryptoChartResponse struct {
120128
Data timestampsChart `json:"data"`

clients/http.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type customHTTPClient struct {
2121
func (chc customHTTPClient) MakeGetRequest(
2222
urlPath string,
2323
params map[string]string,
24+
headers map[string]string,
2425
) (*http.Response, error) {
2526
finalURL, err := addQueryParams(urlPath, params)
2627
if err != nil {
@@ -35,6 +36,9 @@ func (chc customHTTPClient) MakeGetRequest(
3536
if err != nil {
3637
return nil, err
3738
}
39+
for k, v := range headers {
40+
req.Header.Set(k, v)
41+
}
3842
res, err := chc.httpClient.Do(req)
3943
if err != nil {
4044
return nil, err

clients/http_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestMakeGetRequest(t *testing.T) {
8989
defer patches.Reset()
9090

9191
httpClient := getHTTPClient()
92-
res, err := httpClient.MakeGetRequest(expectedURL, expectedParams)
92+
res, err := httpClient.MakeGetRequest(expectedURL, expectedParams, getDefaultHeaders())
9393

9494
if err != nil {
9595
t.Fatalf("Err has to be nil but err is %v", err)

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ require (
1919
github.com/inconshreveable/mousetrap v1.1.0 // indirect
2020
github.com/mattn/go-colorable v0.1.13 // indirect
2121
github.com/mattn/go-isatty v0.0.16 // indirect
22-
github.com/mattn/go-runewidth v0.0.13 // indirect
22+
github.com/mattn/go-runewidth v0.0.15 // indirect
2323
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
2424
github.com/pmezard/go-difflib v1.0.0 // indirect
25-
github.com/rivo/uniseg v0.2.0 // indirect
25+
github.com/rivo/uniseg v0.4.7 // indirect
2626
github.com/spf13/pflag v1.0.5 // indirect
2727
golang.org/x/sys v0.5.0 // indirect
2828
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
2222
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
2323
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
2424
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
25-
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
2625
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
26+
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
27+
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
2728
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
2829
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
2930
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
3031
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
3132
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3233
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
33-
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
3434
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
35+
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
36+
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
3537
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
3638
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
3739
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=

0 commit comments

Comments
 (0)