Skip to content

Commit 33af60d

Browse files
Merge pull request #2 from spiegel-im-spiegel/fix-bugs
Update CreateClient() function
2 parents 59bf8d2 + 9662f63 commit 33af60d

File tree

2 files changed

+38
-72
lines changed

2 files changed

+38
-72
lines changed

client_test.go

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,12 @@
11
package openbd
22

33
import (
4+
"context"
45
"net/http"
56
"net/url"
67
"testing"
78
)
89

9-
// func TestLookupBooksRaw(t *testing.T) {
10-
// testCases := []struct {
11-
// ids []string
12-
// }{
13-
// {ids: nil},
14-
// {ids: []string{}},
15-
// {ids: []string{"foo"}},
16-
// {ids: []string{"9784535585744"}},
17-
// {ids: []string{"9784535585744", "9784797369915"}},
18-
// {ids: []string{"9784535585744", "foo"}},
19-
// {ids: []string{"9784535585744&isbn=9784797369915"}},
20-
// }
21-
//
22-
// for _, tc := range testCases {
23-
// b, err := DefaultClient().LookupBooksRaw(tc.ids)
24-
// if err != nil {
25-
// t.Errorf("Client.LookupBooksRaw() is \"%v\", want nil", err)
26-
// fmt.Printf("error info: %+v\n", err)
27-
// continue
28-
// }
29-
// fmt.Printf("response: %+v\n", string(b))
30-
// }
31-
// }
32-
//
33-
// func TestLookupBooks(t *testing.T) {
34-
// testCases := []struct {
35-
// ids []string
36-
// valid []bool
37-
// }{
38-
// {ids: nil, valid: []bool{false}},
39-
// {ids: []string{}, valid: []bool{false}},
40-
// {ids: []string{"foo"}, valid: []bool{false}},
41-
// {ids: []string{"9784535585744"}, valid: []bool{true}},
42-
// {ids: []string{"9784535585744", "9784797369915"}, valid: []bool{true, true}},
43-
// {ids: []string{"9784535585744", "foo"}, valid: []bool{true, false}},
44-
// {ids: []string{"9784535585744&isbn=9784797369915"}, valid: []bool{false}},
45-
// }
46-
//
47-
// for _, tc := range testCases {
48-
// bks, err := DefaultClient().LookupBooks(tc.ids)
49-
// if err != nil {
50-
// t.Errorf("Client.LookupBooks() is \"%v\", want nil", err)
51-
// fmt.Printf("error info: %+v\n", err)
52-
// continue
53-
// }
54-
// if len(bks) != len(tc.valid) {
55-
// t.Errorf("Count of Client.LookupBooks() is %v, want %v", len(bks), len(tc.valid))
56-
// continue
57-
// }
58-
// for i, bk := range bks {
59-
// if bk.Valid() != tc.valid[i] {
60-
// t.Errorf("Book[%d] is %v, want %v", i, bk.Valid(), tc.valid[i])
61-
// continue
62-
// }
63-
// if bk.Valid() {
64-
// id := bk.Id()
65-
// if id != tc.ids[i] {
66-
// t.Errorf("Book[%d] is %v, want %v", i, id, tc.ids[i])
67-
// }
68-
// }
69-
// }
70-
// }
71-
// }
72-
7310
func TestMakeLookupCommand(t *testing.T) {
7411
testCases := []struct {
7512
v url.Values
@@ -87,7 +24,7 @@ func TestMakeLookupCommand(t *testing.T) {
8724
if u.String() != tc.str {
8825
t.Errorf("Client.MakeLookupCommand() is \"%v\", want \"%v\"", u.String(), tc.str)
8926
}
90-
u = (*Server)(nil).CreateClient(nil, &http.Client{}).MakeLookupCommand(tc.v)
27+
u = (*Server)(nil).CreateClient(WithContext(context.Background()), WithHttpCilent(&http.Client{})).MakeLookupCommand(tc.v)
9128
if u.String() != tc.str {
9229
t.Errorf("Client.MakeLookupCommand() is \"%v\", want \"%v\"", u.String(), tc.str)
9330
}

server.go

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,52 @@ func (s *Server) URL() *url.URL {
5454
return &url.URL{Scheme: s.scheme, Host: s.name}
5555
}
5656

57+
//ClientOptFunc is self-referential function for functional options pattern
58+
type ClientOptFunc func(*Client)
59+
5760
//CreateClient returns new Client instance
58-
func (s *Server) CreateClient(ctx context.Context, client *http.Client) *Client {
61+
func (s *Server) CreateClient(opts ...ClientOptFunc) *Client {
5962
if s == nil {
6063
s = New()
6164
}
62-
if client == nil {
63-
client = http.DefaultClient
65+
cli := &Client{
66+
server: s,
67+
client: nil,
68+
ctx: nil,
69+
}
70+
for _, opt := range opts {
71+
opt(cli)
72+
}
73+
if cli.client == nil {
74+
cli.client = http.DefaultClient
75+
}
76+
if cli.ctx == nil {
77+
cli.ctx = context.Background()
78+
}
79+
return cli
80+
}
81+
82+
//WithContext returns function for setting context.Context
83+
func WithContext(ctx context.Context) ClientOptFunc {
84+
return func(c *Client) {
85+
if c != nil {
86+
c.ctx = ctx
87+
}
6488
}
65-
if ctx == nil {
66-
ctx = context.Background()
89+
}
90+
91+
//WithHttpCilent returns function for setting http.Client
92+
func WithHttpCilent(client *http.Client) ClientOptFunc {
93+
return func(c *Client) {
94+
if c != nil {
95+
c.client = client
96+
}
6797
}
68-
return &Client{server: s, client: client, ctx: ctx}
6998
}
7099

71100
//DefaultClient returns new Client instance with default setting
72101
func DefaultClient() *Client {
73-
return New().CreateClient(nil, nil)
102+
return New().CreateClient()
74103
}
75104

76105
/* Copyright 2019 Spiegel

0 commit comments

Comments
 (0)