Skip to content

Commit 509b0ff

Browse files
Merge pull request #10 from spiegel-im-spiegel/fix-bugs
Add fetch package (breaking change)
2 parents 7db96eb + 98609bf commit 509b0ff

File tree

8 files changed

+50
-50
lines changed

8 files changed

+50
-50
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ import "github.com/spiegel-im-spiegel/openbd-api"
1717

1818
### Lookup openBD Book Data
1919

20-
```
20+
```go
2121
books, err := openbd.DefaultClient().LookupBook([]string{"9784797369915", "9784274069321"})
2222
```
2323

2424
## Entities for openBD
2525

2626
### Book type
2727

28-
```
28+
```go
2929
//Book is entity class of book info.
3030
type Book struct {
3131
Onix Onix `json:"onix"`

cli/openbd/facade/facade.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package facade
22

33
import (
4+
"context"
45
"fmt"
6+
"os"
7+
"os/signal"
58
"runtime"
69

710
"github.com/spf13/cobra"
@@ -70,13 +73,15 @@ func Execute(ui *rwi.RWI, args []string) (exit exitcode.ExitCode) {
7073

7174
//execution
7275
exit = exitcode.Normal
73-
if err := newRootCmd(ui, args).Execute(); err != nil {
76+
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
77+
defer cancel()
78+
if err := newRootCmd(ui, args).ExecuteContext(ctx); err != nil {
7479
exit = exitcode.Abnormal
7580
}
7681
return
7782
}
7883

79-
/* Copyright 2019 Spiegel
84+
/* Copyright 2019-2021 Spiegel
8085
*
8186
* Licensed under the Apache License, Version 2.0 (the "License");
8287
* you may not use this file except in compliance with the License.

cli/openbd/facade/lookup.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ func newLookupCmd(ui *rwi.RWI) *cobra.Command {
2121
}
2222

2323
if rawFlag {
24-
resp, err := openbd.DefaultClient().LookupBooksRaw(args)
24+
resp, err := openbd.DefaultClient().LookupBooksRawContext(cmd.Context(), args)
2525
if err != nil {
2626
return debugPrint(ui, err)
2727
}
2828
return debugPrint(ui, ui.OutputBytes(resp))
2929
}
3030

31-
bks, err := openbd.DefaultClient().LookupBooks(args)
31+
bks, err := openbd.DefaultClient().LookupBooksContext(cmd.Context(), args)
3232
if err != nil {
3333
return debugPrint(ui, err)
3434
}
@@ -44,7 +44,7 @@ func newLookupCmd(ui *rwi.RWI) *cobra.Command {
4444
return lookupCmd
4545
}
4646

47-
/* Copyright 2019,2020 Spiegel
47+
/* Copyright 2019-2021 Spiegel
4848
*
4949
* Licensed under the Apache License, Version 2.0 (the "License");
5050
* you may not use this file except in compliance with the License.

client.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ package openbd
33
import (
44
"context"
55
"fmt"
6-
"io"
7-
"net/http"
86
"net/url"
97
"strings"
108

119
"github.com/spiegel-im-spiegel/errs"
10+
"github.com/spiegel-im-spiegel/fetch"
1211
)
1312

1413
const (
@@ -18,21 +17,30 @@ const (
1817
//Client is http.Client for Aozora API Server
1918
type Client struct {
2019
server *Server
21-
client *http.Client
22-
ctx context.Context
20+
client fetch.Client
2321
}
2422

2523
//LookupBooksRaw gets books data (raw data)
2624
func (c *Client) LookupBooksRaw(ids []string) ([]byte, error) {
25+
return c.LookupBooksRawContext(context.Background(), ids)
26+
}
27+
28+
//LookupBooks gets books data (struct data)
29+
func (c *Client) LookupBooks(ids []string) ([]Book, error) {
30+
return c.LookupBooksContext(context.Background(), ids)
31+
}
32+
33+
//LookupBooksRawContext gets books data with context.Context. (raw data)
34+
func (c *Client) LookupBooksRawContext(ctx context.Context, ids []string) ([]byte, error) {
2735
params := url.Values{}
2836
params.Set("isbn", strings.Join(ids, ","))
29-
b, err := c.get(c.makeLookupCommand(params))
37+
b, err := c.get(ctx, c.makeLookupCommand(params))
3038
return b, errs.Wrap(err)
3139
}
3240

33-
//LookupBooks gets books data (struct data)
34-
func (c *Client) LookupBooks(ids []string) ([]Book, error) {
35-
b, err := c.LookupBooksRaw(ids)
41+
//LookupBooksRawContext gets books data with context.Context. (struct data)
42+
func (c *Client) LookupBooksContext(ctx context.Context, ids []string) ([]Book, error) {
43+
b, err := c.LookupBooksRawContext(ctx, ids)
3644
if err != nil {
3745
return nil, errs.Wrap(err)
3846
}
@@ -41,6 +49,9 @@ func (c *Client) LookupBooks(ids []string) ([]Book, error) {
4149
}
4250

4351
func (c *Client) makeLookupCommand(v url.Values) *url.URL {
52+
if c == nil {
53+
return nil
54+
}
4455
u := c.server.URL()
4556
u.Path = fmt.Sprintf("/%v/%v", c.apiDir(), "get")
4657
u.RawQuery = v.Encode()
@@ -51,28 +62,18 @@ func (c *Client) apiDir() string {
5162
return defaultAPIVersion
5263
}
5364

54-
func (c *Client) get(u *url.URL) ([]byte, error) {
55-
req, err := http.NewRequestWithContext(c.ctx, "GET", u.String(), nil)
56-
if err != nil {
57-
return nil, errs.Wrap(err, errs.WithContext("url", u.String()))
65+
func (c *Client) get(ctx context.Context, u *url.URL) ([]byte, error) {
66+
if c == nil {
67+
return nil, errs.Wrap(fetch.ErrNullPointer)
5868
}
59-
resp, err := c.client.Do(req)
69+
resp, err := c.client.Get(u, fetch.WithContext(ctx))
6070
if err != nil {
6171
return nil, errs.Wrap(err, errs.WithContext("url", u.String()))
6272
}
63-
defer resp.Body.Close()
64-
65-
if !(resp.StatusCode != 0 && resp.StatusCode < http.StatusBadRequest) {
66-
return nil, errs.Wrap(ErrHTTPStatus, errs.WithContext("url", u.String()), errs.WithContext("status", resp.Status))
67-
}
68-
body, err := io.ReadAll(resp.Body)
69-
if err != nil {
70-
return body, errs.Wrap(err, errs.WithContext("url", u.String()))
71-
}
72-
return body, nil
73+
return resp.DumpBodyAndClose()
7374
}
7475

75-
/* Copyright 2019 Spiegel
76+
/* Copyright 2019-2021 Spiegel
7677
*
7778
* Licensed under the Apache License, Version 2.0 (the "License");
7879
* you may not use this file except in compliance with the License.

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module github.com/spiegel-im-spiegel/openbd-api
22

3-
go 1.15
3+
go 1.16
44

55
require (
66
github.com/spf13/cobra v1.1.3
77
github.com/spiegel-im-spiegel/errs v1.0.2
8+
github.com/spiegel-im-spiegel/fetch v0.2.3
89
github.com/spiegel-im-spiegel/gocli v0.10.4
910
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
156156
github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
157157
github.com/spiegel-im-spiegel/errs v1.0.2 h1:v4amEwRDqRWjKHOILQnJSovYhZ4ZttEnBBXNXEzS6Sc=
158158
github.com/spiegel-im-spiegel/errs v1.0.2/go.mod h1:UoasJYYujMcdkbT9USv8dfZWoMyaY3btqQxoLJImw0A=
159+
github.com/spiegel-im-spiegel/fetch v0.2.3 h1:Zh5rHvOjfC81rxKvtUD21JT609smds+BRh+H84s8qEw=
160+
github.com/spiegel-im-spiegel/fetch v0.2.3/go.mod h1:ePIXxdC9OvSarXEO6HW1MgQwtBaKQo0qgDLOhKFXkQ0=
159161
github.com/spiegel-im-spiegel/gocli v0.10.4 h1:aoAWdiQ4hjNxmEod4EeTZTcjdCJcrNOwgHBs5BQYnEQ=
160162
github.com/spiegel-im-spiegel/gocli v0.10.4/go.mod h1:ffI3zoggRyLOZ+IIgaVN8WVMUwfIwfvCEd/0Yl/PZ98=
161163
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

server.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"net/http"
66
"net/url"
7+
8+
"github.com/spiegel-im-spiegel/fetch"
79
)
810

911
const (
@@ -63,37 +65,26 @@ func (s *Server) CreateClient(opts ...ClientOptFunc) *Client {
6365
if s == nil {
6466
s = New()
6567
}
66-
cli := &Client{
67-
server: s,
68-
client: nil,
69-
ctx: nil,
70-
}
68+
cli := &Client{server: s}
7169
for _, opt := range opts {
7270
opt(cli)
7371
}
7472
if cli.client == nil {
75-
cli.client = http.DefaultClient
76-
}
77-
if cli.ctx == nil {
78-
cli.ctx = context.Background()
73+
cli.client = fetch.New()
7974
}
8075
return cli
8176
}
8277

83-
//WithContext returns function for setting context.Context
78+
//WithContext is dummy function. Because this function is deprecated.
8479
func WithContext(ctx context.Context) ClientOptFunc {
85-
return func(c *Client) {
86-
if c != nil {
87-
c.ctx = ctx
88-
}
89-
}
80+
return func(c *Client) {}
9081
}
9182

9283
//WithHttpClient returns function for setting http.Client
9384
func WithHttpClient(client *http.Client) ClientOptFunc {
9485
return func(c *Client) {
9586
if c != nil {
96-
c.client = client
87+
c.client = fetch.New(fetch.WithHTTPClient(client))
9788
}
9889
}
9990
}
@@ -103,7 +94,7 @@ func DefaultClient() *Client {
10394
return New().CreateClient()
10495
}
10596

106-
/* Copyright 2019 Spiegel
97+
/* Copyright 2019-2021 Spiegel
10798
*
10899
* Licensed under the Apache License, Version 2.0 (the "License");
109100
* you may not use this file except in compliance with the License.

test-all.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
go mod verify || exit 1
33
go mod tidy -v || exit 1
44
depm list --json | docker run --rm -i sonatypecommunity/nancy:latest sleuth -n || exit 1
5-
docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:v1.37.1 golangci-lint run --enable gosec ./... || exit 1
5+
docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:v1.38.0 golangci-lint run --enable gosec ./... || exit 1
66
go test ./...

0 commit comments

Comments
 (0)