Skip to content

Commit 70f7e86

Browse files
ref(*): pass io.ReadCloser instead of string body (#11)
1 parent aba8426 commit 70f7e86

13 files changed

Lines changed: 222 additions & 208 deletions

File tree

apps/apps.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"io"
8+
"io/ioutil"
79
"strconv"
810
"strings"
911

@@ -56,14 +58,18 @@ func New(c *deis.Client, id string) (api.App, error) {
5658
}
5759
}
5860

59-
resBody, err := c.BasicRequest("POST", "/v2/apps/", body)
60-
61+
res, err := c.Request("POST", "/v2/apps/", body)
6162
if err != nil {
6263
return api.App{}, err
6364
}
65+
// Fix json.Decoder bug in <go1.7
66+
defer func() {
67+
io.Copy(ioutil.Discard, res.Body)
68+
res.Body.Close()
69+
}()
6470

6571
app := api.App{}
66-
if err = json.Unmarshal([]byte(resBody), &app); err != nil {
72+
if err = json.NewDecoder(res.Body).Decode(&app); err != nil {
6773
return api.App{}, err
6874
}
6975

@@ -77,15 +83,19 @@ func New(c *deis.Client, id string) (api.App, error) {
7783
func Get(c *deis.Client, appID string) (api.App, error) {
7884
u := fmt.Sprintf("/v2/apps/%s/", appID)
7985

80-
body, err := c.BasicRequest("GET", u, nil)
81-
86+
res, err := c.Request("GET", u, nil)
8287
if err != nil {
8388
return api.App{}, err
8489
}
90+
// Fix json.Decoder bug in <go1.7
91+
defer func() {
92+
io.Copy(ioutil.Discard, res.Body)
93+
res.Body.Close()
94+
}()
8595

8696
app := api.App{}
8797

88-
if err = json.Unmarshal([]byte(body), &app); err != nil {
98+
if err = json.NewDecoder(res.Body).Decode(&app); err != nil {
8999
return api.App{}, err
90100
}
91101

@@ -103,15 +113,19 @@ func Logs(c *deis.Client, appID string, lines int) (string, error) {
103113
u += "?log_lines=" + strconv.Itoa(lines)
104114
}
105115

106-
body, err := c.BasicRequest("GET", u, nil)
107-
108-
if err != nil || len(body) < 1 {
116+
res, err := c.Request("GET", u, nil)
117+
if err != nil {
118+
return "", ErrNoLogs
119+
}
120+
defer res.Body.Close()
109121

122+
body, err := ioutil.ReadAll(res.Body)
123+
if err != nil || len(body) < 3 {
110124
return "", ErrNoLogs
111125
}
112126

113127
// We need to trim a few characters off the front and end of the string
114-
return body[2 : len(body)-1], nil
128+
return string(body[2 : len(body)-1]), nil
115129
}
116130

117131
// Run one time command in an app.
@@ -125,26 +139,28 @@ func Run(c *deis.Client, appID string, command string) (api.AppRunResponse, erro
125139

126140
u := fmt.Sprintf("/v2/apps/%s/run", appID)
127141

128-
resBody, err := c.BasicRequest("POST", u, body)
129-
142+
res, err := c.Request("POST", u, body)
130143
if err != nil {
131144
return api.AppRunResponse{}, err
132145
}
133146

134-
res := api.AppRunResponse{}
147+
arr := api.AppRunResponse{}
135148

136-
if err = json.Unmarshal([]byte(resBody), &res); err != nil {
149+
if err = json.NewDecoder(res.Body).Decode(&arr); err != nil {
137150
return api.AppRunResponse{}, err
138151
}
139152

140-
return res, nil
153+
return arr, nil
141154
}
142155

143156
// Delete an app.
144157
func Delete(c *deis.Client, appID string) error {
145158
u := fmt.Sprintf("/v2/apps/%s/", appID)
146159

147-
_, err := c.BasicRequest("DELETE", u, nil)
160+
res, err := c.Request("DELETE", u, nil)
161+
if err == nil {
162+
res.Body.Close()
163+
}
148164
return err
149165
}
150166

@@ -159,6 +175,9 @@ func Transfer(c *deis.Client, appID string, username string) error {
159175
return err
160176
}
161177

162-
_, err = c.BasicRequest("POST", u, body)
178+
res, err := c.Request("POST", u, body)
179+
if err == nil {
180+
res.Body.Close()
181+
}
163182
return err
164183
}

auth/auth.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package auth
22

33
import (
44
"encoding/json"
5+
"io"
6+
"io/ioutil"
57

68
deis "github.com/deis/controller-sdk-go"
79
"github.com/deis/controller-sdk-go/api"
@@ -16,7 +18,10 @@ func Register(c *deis.Client, username, password, email string) error {
1618
return err
1719
}
1820

19-
_, err = c.BasicRequest("POST", "/v2/auth/register/", body)
21+
res, err := c.Request("POST", "/v2/auth/register/", body)
22+
if err == nil {
23+
res.Body.Close()
24+
}
2025
return err
2126
}
2227

@@ -29,14 +34,18 @@ func Login(c *deis.Client, username, password string) (string, error) {
2934
return "", err
3035
}
3136

32-
body, err := c.BasicRequest("POST", "/v2/auth/login/", reqBody)
33-
37+
res, err := c.Request("POST", "/v2/auth/login/", reqBody)
3438
if err != nil {
3539
return "", err
3640
}
41+
// Fix json.Decoder bug in <go1.7
42+
defer func() {
43+
io.Copy(ioutil.Discard, res.Body)
44+
res.Body.Close()
45+
}()
3746

3847
token := api.AuthLoginResponse{}
39-
if err = json.Unmarshal([]byte(body), &token); err != nil {
48+
if err = json.NewDecoder(res.Body).Decode(&token); err != nil {
4049
return "", err
4150
}
4251

@@ -57,7 +66,10 @@ func Delete(c *deis.Client, username string) error {
5766
}
5867
}
5968

60-
_, err = c.BasicRequest("DELETE", "/v2/auth/cancel/", body)
69+
res, err := c.Request("DELETE", "/v2/auth/cancel/", body)
70+
if err == nil {
71+
res.Body.Close()
72+
}
6173
return err
6274
}
6375

@@ -76,18 +88,22 @@ func Regenerate(c *deis.Client, username string, all bool) (string, error) {
7688
return "", err
7789
}
7890

79-
body, err := c.BasicRequest("POST", "/v2/auth/tokens/", reqBody)
80-
91+
res, err := c.Request("POST", "/v2/auth/tokens/", reqBody)
8192
if err != nil {
8293
return "", err
8394
}
95+
// Fix json.Decoder bug in <go1.7
96+
defer func() {
97+
io.Copy(ioutil.Discard, res.Body)
98+
res.Body.Close()
99+
}()
84100

85101
if all == true {
86102
return "", nil
87103
}
88104

89105
token := api.AuthRegenerateResponse{}
90-
if err = json.Unmarshal([]byte(body), &token); err != nil {
106+
if err = json.NewDecoder(res.Body).Decode(&token); err != nil {
91107
return "", err
92108
}
93109

@@ -108,6 +124,9 @@ func Passwd(c *deis.Client, username, password, newPassword string) error {
108124
return err
109125
}
110126

111-
_, err = c.BasicRequest("POST", "/v2/auth/passwd/", body)
127+
res, err := c.Request("POST", "/v2/auth/passwd/", body)
128+
if err == nil {
129+
res.Body.Close()
130+
}
112131
return err
113132
}

auth/auth_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package auth
22

33
import (
4+
"errors"
45
"fmt"
56
"io/ioutil"
67
"net/http"
@@ -254,7 +255,7 @@ func TestDeleteUserApp(t *testing.T) {
254255
err = Delete(deis, "admin")
255256
// should be a 409 Conflict
256257

257-
expected := fmt.Errorf("\n%s %s\n\n", "409", "Conflict")
258+
expected := errors.New("409 Conflict")
258259
if reflect.DeepEqual(err, expected) == false {
259260
t.Errorf("got '%s' but expected '%s'", err, expected)
260261
}

builds/builds.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package builds
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
7+
"io/ioutil"
68

79
deis "github.com/deis/controller-sdk-go"
810
"github.com/deis/controller-sdk-go/api"
@@ -39,14 +41,18 @@ func New(c *deis.Client, appID string, image string,
3941
return api.Build{}, err
4042
}
4143

42-
resBody, err := c.BasicRequest("POST", u, body)
43-
44+
res, err := c.Request("POST", u, body)
4445
if err != nil {
4546
return api.Build{}, err
4647
}
48+
// Fix json.Decoder bug in <go1.7
49+
defer func() {
50+
io.Copy(ioutil.Discard, res.Body)
51+
res.Body.Close()
52+
}()
4753

4854
build := api.Build{}
49-
if err = json.Unmarshal([]byte(resBody), &build); err != nil {
55+
if err = json.NewDecoder(res.Body).Decode(&build); err != nil {
5056
return api.Build{}, err
5157
}
5258

certs/certs.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package certs
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
7+
"io/ioutil"
68

79
deis "github.com/deis/controller-sdk-go"
810
"github.com/deis/controller-sdk-go/api"
@@ -32,13 +34,18 @@ func New(c *deis.Client, cert string, key string, name string) (api.Cert, error)
3234
return api.Cert{}, err
3335
}
3436

35-
resBody, err := c.BasicRequest("POST", "/v2/certs/", reqBody)
37+
res, err := c.Request("POST", "/v2/certs/", reqBody)
3638
if err != nil {
3739
return api.Cert{}, err
3840
}
41+
// Fix json.Decoder bug in <go1.7
42+
defer func() {
43+
io.Copy(ioutil.Discard, res.Body)
44+
res.Body.Close()
45+
}()
3946

4047
resCert := api.Cert{}
41-
if err = json.Unmarshal([]byte(resBody), &resCert); err != nil {
48+
if err = json.NewDecoder(res.Body).Decode(&resCert); err != nil {
4249
return api.Cert{}, err
4350
}
4451

@@ -48,23 +55,31 @@ func New(c *deis.Client, cert string, key string, name string) (api.Cert, error)
4855
// Get information for a certificate
4956
func Get(c *deis.Client, name string) (api.Cert, error) {
5057
url := fmt.Sprintf("/v2/certs/%s", name)
51-
body, err := c.BasicRequest("GET", url, nil)
58+
res, err := c.Request("GET", url, nil)
5259
if err != nil {
5360
return api.Cert{}, err
5461
}
62+
// Fix json.Decoder bug in <go1.7
63+
defer func() {
64+
io.Copy(ioutil.Discard, res.Body)
65+
res.Body.Close()
66+
}()
5567

56-
res := api.Cert{}
57-
if err = json.Unmarshal([]byte(body), &res); err != nil {
68+
resCert := api.Cert{}
69+
if err = json.NewDecoder(res.Body).Decode(&resCert); err != nil {
5870
return api.Cert{}, err
5971
}
6072

61-
return res, nil
73+
return resCert, nil
6274
}
6375

6476
// Delete removes a cert.
6577
func Delete(c *deis.Client, name string) error {
6678
url := fmt.Sprintf("/v2/certs/%s", name)
67-
_, err := c.BasicRequest("DELETE", url, nil)
79+
res, err := c.Request("DELETE", url, nil)
80+
if err == nil {
81+
res.Body.Close()
82+
}
6883
return err
6984
}
7085

@@ -77,13 +92,19 @@ func Attach(c *deis.Client, name string, domain string) error {
7792
}
7893

7994
url := fmt.Sprintf("/v2/certs/%s/domain/", name)
80-
_, err = c.BasicRequest("POST", url, reqBody)
95+
res, err := c.Request("POST", url, reqBody)
96+
if err == nil {
97+
res.Body.Close()
98+
}
8199
return err
82100
}
83101

84102
// Detach a certificate from a domain
85103
func Detach(c *deis.Client, name string, domain string) error {
86104
url := fmt.Sprintf("/v2/certs/%s/domain/%s", name, domain)
87-
_, err := c.BasicRequest("DELETE", url, nil)
105+
res, err := c.Request("DELETE", url, nil)
106+
if err == nil {
107+
res.Body.Close()
108+
}
88109
return err
89110
}

0 commit comments

Comments
 (0)