Skip to content

Commit 47e9cea

Browse files
Let the Doer Do it (#115)
* Let the Doer do it Signed-off-by: Steve Coffman <[email protected]> * Address review feedback Signed-off-by: Steve Coffman <[email protected]>
1 parent 59303a4 commit 47e9cea

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

graphql/client.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type Client interface {
4747
}
4848

4949
type client struct {
50-
httpClient *http.Client
50+
httpClient Doer
5151
endpoint string
5252
method string
5353
}
@@ -61,13 +61,20 @@ type client struct {
6161
//
6262
// The typical method of adding authentication headers is to wrap the client's
6363
// Transport to add those headers. See example/caller.go for an example.
64-
func NewClient(endpoint string, httpClient *http.Client) Client {
65-
if httpClient == nil {
64+
func NewClient(endpoint string, httpClient Doer) Client {
65+
if httpClient == nil || httpClient == (*http.Client)(nil) {
6666
httpClient = http.DefaultClient
6767
}
6868
return &client{httpClient, endpoint, http.MethodPost}
6969
}
7070

71+
// Doer encapsulates the methods from *http.Client needed by Client.
72+
// The methods should have behavior to match that of *http.Client
73+
// (or mocks for the same).
74+
type Doer interface {
75+
Do(*http.Request) (*http.Response, error)
76+
}
77+
7178
type payload struct {
7279
Query string `json:"query"`
7380
Variables interface{} `json:"variables,omitempty"`

0 commit comments

Comments
 (0)