response: simplify Decode
This commit was moved from ipfs/go-ipfs-http-client@adbfda4c1c6ee037df3f91ca73f60de1f4620d67
Łukasz Magiera committed
Feb 20, 2019 at 16:50 UTC
ad844e3d0b0221333c04896ed80f11afed301495
3 files changed
+5
-19
client/httpapi/api.go
+1
-1
@@ -53,7 +53,7 @@ func NewLocalApi() (iface.CoreAPI, error) {
53
func NewPathApi(ipfspath string) (iface.CoreAPI, error) {
54
a, err := ApiAddr(ipfspath)
55
if err != nil {
56
- if err == os.ErrNotExist {
56
+ if os.IsNotExist(err) {
57
err = nil
58
}
59
return nil, err
client/httpapi/requestbuilder.go
+1
-1
@@ -110,5 +110,5 @@ func (r *RequestBuilder) Exec(ctx context.Context, res interface{}) error {
110
return lateErr
111
}
112
113
- return httpRes.Decode(res)
113
+ return httpRes.decode(res)
114
}
client/httpapi/response.go
+3
-17
@@ -59,28 +59,14 @@ func (r *Response) Cancel() error {
59
return nil
60
}
61
62
-func (r *Response) Decode(dec interface{}) error {
62
+// Decode reads request body and decodes it as json
63
+func (r *Response) decode(dec interface{}) error {
64
defer r.Close()
65
if r.Error != nil {
66
return r.Error
67
}
68
68
- n := 0
69
- var err error
70
- for {
71
- err = json.NewDecoder(r.Output).Decode(dec)
72
- if err != nil {
73
- break
74
- }
75
- n++
76
- }
77
-
78
- // Decode expects at least one result. For calls where zero results are valid,
79
- // use Send and construct json Decoder manually.
80
- if n > 0 && err == io.EOF {
81
- err = nil
82
- }
83
- return err
69
+ return json.NewDecoder(r.Output).Decode(dec)
70
}
71
72
type Error struct {