Skip to content

Commit df1c2e6

Browse files
Refactoring
1 parent 43a5e9e commit df1c2e6

File tree

7 files changed

+28
-24
lines changed

7 files changed

+28
-24
lines changed

client.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
const (
15-
APIVersion = "v1"
15+
defaultAPIVersion = "v1"
1616
)
1717

1818
//Client is http.Client for Aozora API Server
@@ -26,7 +26,7 @@ type Client struct {
2626
func (c *Client) LookupBooksRaw(ids []string) ([]byte, error) {
2727
params := url.Values{}
2828
params.Set("isbn", strings.Join(ids, ","))
29-
b, err := c.get(c.MakeLookupCommand(params))
29+
b, err := c.get(c.makeLookupCommand(params))
3030
return b, errs.Wrap(err, "")
3131
}
3232

@@ -40,31 +40,34 @@ func (c *Client) LookupBooks(ids []string) ([]Book, error) {
4040
return books, errs.Wrap(err, "")
4141
}
4242

43-
//MakeLookupCommand returns URI for lookup command
44-
func (c *Client) MakeLookupCommand(v url.Values) *url.URL {
43+
func (c *Client) makeLookupCommand(v url.Values) *url.URL {
4544
u := c.server.URL()
46-
u.Path = fmt.Sprintf("/%v/%v", APIVersion, "get")
45+
u.Path = fmt.Sprintf("/%v/%v", c.apiDir(), "get")
4746
u.RawQuery = v.Encode()
4847
return u
4948
}
5049

50+
func (c *Client) apiDir() string {
51+
return defaultAPIVersion
52+
}
53+
5154
func (c *Client) get(u *url.URL) ([]byte, error) {
5255
req, err := http.NewRequestWithContext(c.ctx, "GET", u.String(), nil)
5356
if err != nil {
54-
return nil, errs.Wrap(err, "", errs.WithParam("url", u.String()))
57+
return nil, errs.Wrap(err, "", errs.WithContext("url", u.String()))
5558
}
5659
resp, err := c.client.Do(req)
5760
if err != nil {
58-
return nil, errs.Wrap(err, "", errs.WithParam("url", u.String()))
61+
return nil, errs.Wrap(err, "", errs.WithContext("url", u.String()))
5962
}
6063
defer resp.Body.Close()
6164

6265
if !(resp.StatusCode != 0 && resp.StatusCode < http.StatusBadRequest) {
63-
return nil, errs.Wrap(ErrHTTPStatus, "", errs.WithParam("url", u.String()), errs.WithParam("status", resp.Status))
66+
return nil, errs.Wrap(ErrHTTPStatus, "", errs.WithContext("url", u.String()), errs.WithContext("status", resp.Status))
6467
}
6568
body, err := ioutil.ReadAll(resp.Body)
6669
if err != nil {
67-
return body, errs.Wrap(err, "", errs.WithParam("url", u.String()))
70+
return body, errs.Wrap(err, "", errs.WithContext("url", u.String()))
6871
}
6972
return body, nil
7073
}

client_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"testing"
88
)
99

10-
func TestMakeLookupCommand(t *testing.T) {
10+
func TestmakeLookupCommand(t *testing.T) {
1111
testCases := []struct {
1212
v url.Values
1313
str string
@@ -20,13 +20,13 @@ func TestMakeLookupCommand(t *testing.T) {
2020
}
2121

2222
for _, tc := range testCases {
23-
u := DefaultClient().MakeLookupCommand(tc.v)
23+
u := DefaultClient().makeLookupCommand(tc.v)
2424
if u.String() != tc.str {
25-
t.Errorf("Client.MakeLookupCommand() is \"%v\", want \"%v\"", u.String(), tc.str)
25+
t.Errorf("Client.makeLookupCommand() is \"%v\", want \"%v\"", u.String(), tc.str)
2626
}
27-
u = (*Server)(nil).CreateClient(WithContext(context.Background()), WithHttpClient(&http.Client{})).MakeLookupCommand(tc.v)
27+
u = (*Server)(nil).CreateClient(WithContext(context.Background()), WithHttpClient(&http.Client{})).makeLookupCommand(tc.v)
2828
if u.String() != tc.str {
29-
t.Errorf("Client.MakeLookupCommand() is \"%v\", want \"%v\"", u.String(), tc.str)
29+
t.Errorf("Client.makeLookupCommand() is \"%v\", want \"%v\"", u.String(), tc.str)
3030
}
3131
}
3232
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ go 1.13
44

55
require (
66
github.com/spf13/cobra v0.0.5
7-
github.com/spiegel-im-spiegel/errs v0.2.2
7+
github.com/spiegel-im-spiegel/errs v0.3.1
88
github.com/spiegel-im-spiegel/gocli v0.10.1
99
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb6
2222
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
2323
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
2424
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
25-
github.com/spiegel-im-spiegel/errs v0.2.2 h1:ltRYJzh2Vn59HfySjQCex1vOji/dH0xJV6jyzBCeClg=
26-
github.com/spiegel-im-spiegel/errs v0.2.2/go.mod h1:NwHSe6m3oAhRj2bAkkbzz9xAffIDNcP9uTdyJd9fJNs=
25+
github.com/spiegel-im-spiegel/errs v0.3.1 h1:GheZPMUx4o+UxShr1cOjKO5FGIJl4bcr7AY7bp1crUs=
26+
github.com/spiegel-im-spiegel/errs v0.3.1/go.mod h1:NwHSe6m3oAhRj2bAkkbzz9xAffIDNcP9uTdyJd9fJNs=
2727
github.com/spiegel-im-spiegel/gocli v0.10.1 h1:XWyq4dKFp2xTjiH2P2bxPZyi0++4IHp9HOksgGdfwpA=
2828
github.com/spiegel-im-spiegel/gocli v0.10.1/go.mod h1:9vRvly2giutJ2sAtQjrw570p9ulJA3twgKlurmnj12g=
2929
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=

server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
)
88

99
const (
10-
DefaultHost = "api.openbd.jp"
10+
defaultScheme = "https"
11+
defaultHost = "api.openbd.jp"
1112
)
1213

1314
//Server is informations of openBD
@@ -21,7 +22,7 @@ type ServerOptFunc func(*Server)
2122

2223
//New returns new Server instance
2324
func New(opts ...ServerOptFunc) *Server {
24-
server := &Server{scheme: "https", name: DefaultHost}
25+
server := &Server{scheme: defaultScheme, name: defaultHost}
2526
for _, opt := range opts {
2627
opt(server)
2728
}

server_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ func TestServer(t *testing.T) {
77
s *Server
88
str string
99
}{
10-
{s: (*Server)(nil), str: "https://" + DefaultHost},
11-
{s: New(), str: "https://" + DefaultHost},
10+
{s: (*Server)(nil), str: defaultScheme + "://" + defaultHost},
11+
{s: New(), str: defaultScheme + "://" + defaultHost},
1212
{s: New(WithScheme("foo"), WithServerName("bar")), str: "foo://bar"},
1313
}
1414

values-date.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (t *Date) UnmarshalJSON(b []byte) error {
4747
if strings.Contains(s, ":") {
4848
for _, tmplt := range timeTemplate1 {
4949
if tm, err := time.Parse(tmplt, s); err != nil {
50-
lastErr = errs.Wrap(err, "", errs.WithParam("time_string", s), errs.WithParam("time_template", tmplt))
50+
lastErr = errs.Wrap(err, "", errs.WithContext("time_string", s), errs.WithContext("time_template", tmplt))
5151
} else {
5252
*t = Date{tm}
5353
return nil
@@ -57,7 +57,7 @@ func (t *Date) UnmarshalJSON(b []byte) error {
5757
}
5858
for _, tmplt := range timeTemplate2 {
5959
if tm, err := time.Parse(tmplt, s); err != nil {
60-
lastErr = errs.Wrap(err, "", errs.WithParam("time_string", s), errs.WithParam("time_template", tmplt))
60+
lastErr = errs.Wrap(err, "", errs.WithContext("time_string", s), errs.WithContext("time_template", tmplt))
6161
} else {
6262
*t = Date{tm}
6363
return nil
@@ -67,7 +67,7 @@ func (t *Date) UnmarshalJSON(b []byte) error {
6767
}
6868
for _, tmplt := range timeTemplate3 {
6969
if tm, err := time.Parse(tmplt, s); err != nil {
70-
lastErr = errs.Wrap(err, "", errs.WithParam("time_string", s), errs.WithParam("time_template", tmplt))
70+
lastErr = errs.Wrap(err, "", errs.WithContext("time_string", s), errs.WithContext("time_template", tmplt))
7171
} else {
7272
*t = Date{tm}
7373
return nil

0 commit comments

Comments
 (0)