Skip to content

Commit 8c63269

Browse files
committed
simplified interface
1 parent 8d0ea84 commit 8c63269

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

client.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@ type Client struct {
1818
httpClient *http.Client
1919
}
2020

21-
type ClientOption interface {
22-
apply(*Client)
23-
}
24-
25-
func NewClient(ctx context.Context, endpoint string, opts ...ClientOption) (*Client, error) {
21+
// NewClient makes a new Client capable of making GraphQL requests.
22+
func NewClient(endpoint string, opts ...ClientOption) (*Client, error) {
2623
c := &Client{
2724
endpoint: endpoint,
2825
}
29-
for _, o := range opts {
30-
o.apply(c)
26+
for _, optionFunc := range opts {
27+
optionFunc(c)
3128
}
3229
if c.httpClient == nil {
3330
c.httpClient = http.DefaultClient
@@ -109,18 +106,16 @@ func (c *Client) Run(ctx context.Context, request *Request, response interface{}
109106
return nil
110107
}
111108

112-
type httpClientOption struct {
113-
hc *http.Client
114-
}
115-
116-
func (o httpClientOption) apply(c *Client) {
117-
c.httpClient = o.hc
109+
// WithHTTPClient specifies the underlying http.Client to use when
110+
// making requests.
111+
func WithHTTPClient(httpclient *http.Client) ClientOption {
112+
return ClientOption(func(client *Client) {
113+
client.httpClient = httpclient
114+
})
118115
}
119116

120-
// WithHTTPClient specifies the http.Client that requests will use.
121-
func WithHTTPClient(client *http.Client) ClientOption {
122-
return httpClientOption{client}
123-
}
117+
// ClientOption is a function that modifies the client in some way.
118+
type ClientOption func(*Client)
124119

125120
type graphErr struct {
126121
Message string

client_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestWithClient(t *testing.T) {
2727
}
2828

2929
ctx := context.Background()
30-
client, err := NewClient(ctx, "", WithHTTPClient(testClient))
30+
client, err := NewClient("", WithHTTPClient(testClient))
3131
is.NoErr(err)
3232

3333
req := NewRequest(``)
@@ -53,7 +53,7 @@ func TestDo(t *testing.T) {
5353
defer srv.Close()
5454

5555
ctx := context.Background()
56-
client, err := NewClient(ctx, srv.URL)
56+
client, err := NewClient(srv.URL)
5757
is.NoErr(err)
5858

5959
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
@@ -82,7 +82,7 @@ func TestDoErr(t *testing.T) {
8282
defer srv.Close()
8383

8484
ctx := context.Background()
85-
client, err := NewClient(ctx, srv.URL)
85+
client, err := NewClient(srv.URL)
8686
is.NoErr(err)
8787

8888
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
@@ -110,7 +110,7 @@ func TestDoNoResponse(t *testing.T) {
110110
defer srv.Close()
111111

112112
ctx := context.Background()
113-
client, err := NewClient(ctx, srv.URL)
113+
client, err := NewClient(srv.URL)
114114
is.NoErr(err)
115115

116116
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
@@ -136,7 +136,7 @@ func TestQuery(t *testing.T) {
136136
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
137137
defer cancel()
138138

139-
client, err := NewClient(ctx, srv.URL)
139+
client, err := NewClient(srv.URL)
140140
is.NoErr(err)
141141

142142
req := NewRequest("query {}")
@@ -180,7 +180,7 @@ func TestFile(t *testing.T) {
180180
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
181181
defer cancel()
182182

183-
client, err := NewClient(ctx, srv.URL)
183+
client, err := NewClient(srv.URL)
184184
is.NoErr(err)
185185

186186
f := strings.NewReader(`This is a file`)

0 commit comments

Comments
 (0)