Skip to content

Commit d4eecd8

Browse files
authoredOct 14, 2019
Merge pull request #5 from spiegel-im-spiegel/fix-bugs
Refactoring
2 parents de59f8a + f0c5dc6 commit d4eecd8

7 files changed

+43
-43
lines changed
 

‎client.go

+28-29
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
const (
16-
APIVersion = "api/v0.1"
16+
defaultAPIDir = "api/v0.1"
1717
)
1818

1919
//Client is http.Client for Aozora API Server
@@ -32,7 +32,7 @@ func (c *Client) SearchBooksRaw(opts ...SearchBooksParamsFunc) ([]byte, error) {
3232
for _, opt := range opts {
3333
opt(params)
3434
}
35-
b, err := c.get(c.MakeSearchCommand(TargetBooks, params))
35+
b, err := c.get(c.makeSearchCommand(TargetBooks, params))
3636
return b, errs.Wrap(err, "")
3737
}
3838

@@ -109,7 +109,7 @@ func (c *Client) SearchPersonsRaw(opts ...SearchPersonsParamsFunc) ([]byte, erro
109109
for _, opt := range opts {
110110
opt(params)
111111
}
112-
b, err := c.get(c.MakeSearchCommand(TargetPersons, params))
112+
b, err := c.get(c.makeSearchCommand(TargetPersons, params))
113113
return b, errs.Wrap(err, "")
114114
}
115115

@@ -141,7 +141,7 @@ func (c *Client) SearchWorkersRaw(opts ...SearchWorkersParamsFunc) ([]byte, erro
141141
for _, opt := range opts {
142142
opt(params)
143143
}
144-
b, err := c.get(c.MakeSearchCommand(TargetWorkers, params))
144+
b, err := c.get(c.makeSearchCommand(TargetWorkers, params))
145145
return b, errs.Wrap(err, "")
146146
}
147147

@@ -166,7 +166,7 @@ func WithWorkerName(name string) SearchWorkersParamsFunc {
166166

167167
//LookupBookRaw gets book data (raw data)
168168
func (c *Client) LookupBookRaw(id int) ([]byte, error) {
169-
b, err := c.get(c.MakeLookupCommand(TargetBooks, id))
169+
b, err := c.get(c.makeLookupCommand(TargetBooks, id))
170170
return b, errs.Wrap(err, "")
171171
}
172172

@@ -182,19 +182,19 @@ func (c *Client) LookupBook(id int) (*Book, error) {
182182

183183
//LookupBookCardRaw gets book card info (HTML page data)
184184
func (c *Client) LookupBookCardRaw(id int) ([]byte, error) {
185-
b, err := c.get(c.MakeCardCommand(id))
185+
b, err := c.get(c.makeCardCommand(id))
186186
return b, errs.Wrap(err, "")
187187
}
188188

189189
//LookupBookContentRaw gets book content (plain or HTML formatted text data)
190190
func (c *Client) LookupBookContentRaw(id int, f Format) ([]byte, error) {
191-
b, err := c.get(c.MakeContentCommand(id, f))
191+
b, err := c.get(c.makeContentCommand(id, f))
192192
return b, errs.Wrap(err, "")
193193
}
194194

195195
//LookupPersonRaw gets person data (raw data)
196196
func (c *Client) LookupPersonRaw(id int) ([]byte, error) {
197-
b, err := c.get(c.MakeLookupCommand(TargetPersons, id))
197+
b, err := c.get(c.makeLookupCommand(TargetPersons, id))
198198
return b, errs.Wrap(err, "")
199199
}
200200

@@ -210,7 +210,7 @@ func (c *Client) LookupPerson(id int) (*Person, error) {
210210

211211
//LookupWorker gets worker data (raw data)
212212
func (c *Client) LookupWorkerRaw(id int) ([]byte, error) {
213-
b, err := c.get(c.MakeLookupCommand(TargetWorkers, id))
213+
b, err := c.get(c.makeLookupCommand(TargetWorkers, id))
214214
return b, errs.Wrap(err, "")
215215
}
216216

@@ -226,7 +226,7 @@ func (c *Client) LookupWorker(id int) (*Worker, error) {
226226

227227
//RankingRaw gets ranking data (raw data)
228228
func (c *Client) RankingRaw(tm time.Time) ([]byte, error) {
229-
b, err := c.get(c.MakeRankingCommand(tm))
229+
b, err := c.get(c.makeRankingCommand(tm))
230230
return b, errs.Wrap(err, "")
231231
}
232232

@@ -240,60 +240,59 @@ func (c *Client) Ranking(tm time.Time) (Ranking, error) {
240240
return ranking, errs.Wrap(err, "")
241241
}
242242

243-
//MakeSearchCommand returns URI for search command
244-
func (c *Client) MakeSearchCommand(t Target, v url.Values) *url.URL {
243+
func (c *Client) makeSearchCommand(t Target, v url.Values) *url.URL {
245244
u := c.server.URL()
246-
u.Path = fmt.Sprintf("/%v/%v", APIVersion, t)
245+
u.Path = fmt.Sprintf("/%v/%v", c.apiDir(), t)
247246
u.RawQuery = v.Encode()
248247
return u
249248
}
250249

251-
//MakeLookupCommand returns URI for lookup command
252-
func (c *Client) MakeLookupCommand(t Target, id int) *url.URL {
250+
func (c *Client) makeLookupCommand(t Target, id int) *url.URL {
253251
u := c.server.URL()
254-
u.Path = fmt.Sprintf("/%v/%v/%v", APIVersion, t, strconv.Itoa(id))
252+
u.Path = fmt.Sprintf("/%v/%v/%v", c.apiDir(), t, strconv.Itoa(id))
255253
return u
256254
}
257255

258-
//MakeLookupCommand returns URI for lookup command
259-
func (c *Client) MakeCardCommand(id int) *url.URL {
260-
u := c.MakeLookupCommand(TargetBooks, id)
256+
func (c *Client) makeCardCommand(id int) *url.URL {
257+
u := c.makeLookupCommand(TargetBooks, id)
261258
u.Path = u.Path + "/card"
262259
return u
263260
}
264261

265-
//MakeLookupCommand returns URI for lookup command
266-
func (c *Client) MakeContentCommand(id int, f Format) *url.URL {
267-
u := c.MakeLookupCommand(TargetBooks, id)
262+
func (c *Client) makeContentCommand(id int, f Format) *url.URL {
263+
u := c.makeLookupCommand(TargetBooks, id)
268264
u.Path = u.Path + "/content"
269265
u.RawQuery = (url.Values{"format": {f.String()}}).Encode()
270266
return u
271267
}
272268

273-
//MakeLookupCommand returns URI for lookup ranking info command
274-
func (c *Client) MakeRankingCommand(tm time.Time) *url.URL {
269+
func (c *Client) makeRankingCommand(tm time.Time) *url.URL {
275270
u := c.server.URL()
276-
u.Path = fmt.Sprintf("/%v/%v/%v/%v", APIVersion, TargetRanking, "xhtml", tm.Format("2006/01"))
271+
u.Path = fmt.Sprintf("/%v/%v/%v/%v", c.apiDir(), TargetRanking, "xhtml", tm.Format("2006/01"))
277272
return u
278273
}
279274

275+
func (c *Client) apiDir() string {
276+
return defaultAPIDir
277+
}
278+
280279
func (c *Client) get(u *url.URL) ([]byte, error) {
281280
req, err := http.NewRequestWithContext(c.ctx, "GET", u.String(), nil)
282281
if err != nil {
283-
return nil, errs.Wrap(err, "", errs.WithParam("url", u.String()))
282+
return nil, errs.Wrap(err, "", errs.WithContext("url", u.String()))
284283
}
285284
resp, err := c.client.Do(req)
286285
if err != nil {
287-
return nil, errs.Wrap(err, "", errs.WithParam("url", u.String()))
286+
return nil, errs.Wrap(err, "", errs.WithContext("url", u.String()))
288287
}
289288
defer resp.Body.Close()
290289

291290
if !(resp.StatusCode != 0 && resp.StatusCode < http.StatusBadRequest) {
292-
return nil, errs.Wrap(ErrHTTPStatus, "", errs.WithParam("url", u.String()), errs.WithParam("status", resp.Status))
291+
return nil, errs.Wrap(ErrHTTPStatus, "", errs.WithContext("url", u.String()), errs.WithContext("status", resp.Status))
293292
}
294293
body, err := ioutil.ReadAll(resp.Body)
295294
if err != nil {
296-
return body, errs.Wrap(err, "", errs.WithParam("url", u.String()))
295+
return body, errs.Wrap(err, "", errs.WithContext("url", u.String()))
297296
}
298297
return body, nil
299298
}

‎client_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestMakeSearchCommand(t *testing.T) {
2828
}
2929

3030
for _, tc := range testCases {
31-
u := DefaultClient().MakeSearchCommand(tc.t, tc.v)
31+
u := DefaultClient().makeSearchCommand(tc.t, tc.v)
3232
if u.String() != tc.str {
3333
t.Errorf("Client.MakeSearchCommand() is \"%v\", want \"%v\"", u.String(), tc.str)
3434
}
@@ -47,7 +47,7 @@ func TestMakeLookupCommand(t *testing.T) {
4747
}
4848

4949
for _, tc := range testCases {
50-
u := (*Server)(nil).CreateClient(WithContext(context.Background()), WithHttpClient(&http.Client{})).MakeLookupCommand(tc.t, tc.id)
50+
u := (*Server)(nil).CreateClient(WithContext(context.Background()), WithHttpClient(&http.Client{})).makeLookupCommand(tc.t, tc.id)
5151
if u.String() != tc.str {
5252
t.Errorf("Client.MakeLookupCommand() is \"%v\", want \"%v\"", u.String(), tc.str)
5353
}
@@ -63,7 +63,7 @@ func TestMakeCardCommand(t *testing.T) {
6363
}
6464

6565
for _, tc := range testCases {
66-
u := DefaultClient().MakeCardCommand(tc.id)
66+
u := DefaultClient().makeCardCommand(tc.id)
6767
if u.String() != tc.str {
6868
t.Errorf("Client.MakeCardCommand() is \"%v\", want \"%v\"", u.String(), tc.str)
6969
}
@@ -82,9 +82,9 @@ func TestMakeContentCommand(t *testing.T) {
8282
}
8383

8484
for _, tc := range testCases {
85-
u := DefaultClient().MakeContentCommand(tc.id, tc.f)
85+
u := DefaultClient().makeContentCommand(tc.id, tc.f)
8686
if u.String() != tc.str {
87-
t.Errorf("Client.MakeContentCommand() is \"%v\", want \"%v\"", u.String(), tc.str)
87+
t.Errorf("Client.makeContentCommand() is \"%v\", want \"%v\"", u.String(), tc.str)
8888
}
8989
}
9090
}
@@ -98,7 +98,7 @@ func TestMakeRankingCommand(t *testing.T) {
9898
}
9999

100100
for _, tc := range testCases {
101-
u := DefaultClient().MakeRankingCommand(tc.tm)
101+
u := DefaultClient().makeRankingCommand(tc.tm)
102102
if u.String() != tc.str {
103103
t.Errorf("Client.MakeRankingCommand() is \"%v\", want \"%v\"", u.String(), tc.str)
104104
}

‎go.mod

+1-1
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

+2-2
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

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
)
88

99
const (
10-
DefaultHost = "www.aozorahack.net"
10+
defaultScheme = "http"
11+
defaultHost = "www.aozorahack.net"
1112
)
1213

1314
//Server is informations of Aozora API
@@ -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: "http", name: DefaultHost}
25+
server := &Server{scheme: defaultScheme, name: defaultHost}
2526
for _, opt := range opts {
2627
opt(server)
2728
}

‎server_test.go

+2-2
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: "http://" + DefaultHost},
11-
{s: New(), str: "http://" + 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

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (t *Date) UnmarshalJSON(b []byte) error {
3838
var lastErr error
3939
for _, tmplt := range timeTemplate {
4040
if tm, err := time.Parse(tmplt, s); err != nil {
41-
lastErr = errs.Wrap(err, "", errs.WithParam("time_string", s), errs.WithParam("time_template", tmplt))
41+
lastErr = errs.Wrap(err, "", errs.WithContext("time_string", s), errs.WithContext("time_template", tmplt))
4242
} else {
4343
*t = Date{tm}
4444
return nil

0 commit comments

Comments
 (0)
Please sign in to comment.