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) {
7783func 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.
144157func 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}
0 commit comments