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]>
This commit is contained in:
+10
-3
@@ -47,7 +47,7 @@ type Client interface {
|
||||
}
|
||||
|
||||
type client struct {
|
||||
httpClient *http.Client
|
||||
httpClient Doer
|
||||
endpoint string
|
||||
method string
|
||||
}
|
||||
@@ -61,13 +61,20 @@ type client struct {
|
||||
//
|
||||
// The typical method of adding authentication headers is to wrap the client's
|
||||
// Transport to add those headers. See example/caller.go for an example.
|
||||
func NewClient(endpoint string, httpClient *http.Client) Client {
|
||||
if httpClient == nil {
|
||||
func NewClient(endpoint string, httpClient Doer) Client {
|
||||
if httpClient == nil || httpClient == (*http.Client)(nil) {
|
||||
httpClient = http.DefaultClient
|
||||
}
|
||||
return &client{httpClient, endpoint, http.MethodPost}
|
||||
}
|
||||
|
||||
// Doer encapsulates the methods from *http.Client needed by Client.
|
||||
// The methods should have behavior to match that of *http.Client
|
||||
// (or mocks for the same).
|
||||
type Doer interface {
|
||||
Do(*http.Request) (*http.Response, error)
|
||||
}
|
||||
|
||||
type payload struct {
|
||||
Query string `json:"query"`
|
||||
Variables interface{} `json:"variables,omitempty"`
|
||||
|
||||
Reference in New Issue
Block a user