Skip to content

Commit

Permalink
ref(*): pass io.ReadCloser instead of string body
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Anderson committed Jun 18, 2016
1 parent aba8426 commit 7dca4be
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 207 deletions.
55 changes: 39 additions & 16 deletions apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"strconv"
"strings"

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

resBody, err := c.BasicRequest("POST", "/v2/apps/", body)

res, err := c.Request("POST", "/v2/apps/", body)
if err != nil {
return api.App{}, err
}
// Fix json.Decoder bug in <go1.7
defer func() {
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
}()

app := api.App{}
if err = json.Unmarshal([]byte(resBody), &app); err != nil {
if err = json.NewDecoder(res.Body).Decode(&app); err != nil {
return api.App{}, err
}

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

body, err := c.BasicRequest("GET", u, nil)

res, err := c.Request("GET", u, nil)
if err != nil {
return api.App{}, err
}
// Fix json.Decoder bug in <go1.7
defer func() {
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
}()

app := api.App{}

if err = json.Unmarshal([]byte(body), &app); err != nil {
if err = json.NewDecoder(res.Body).Decode(&app); err != nil {
return api.App{}, err
}

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

body, err := c.BasicRequest("GET", u, nil)
res, err := c.Request("GET", u, nil)
if err != nil {
return "", ErrNoLogs
}
// Fix json.Decoder bug in <go1.7
defer func() {
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
}()

body, err := ioutil.ReadAll(res.Body)
if err != nil || len(body) < 1 {

return "", ErrNoLogs
}

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

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

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

resBody, err := c.BasicRequest("POST", u, body)

res, err := c.Request("POST", u, body)
if err != nil {
return api.AppRunResponse{}, err
}

res := api.AppRunResponse{}
arr := api.AppRunResponse{}

if err = json.Unmarshal([]byte(resBody), &res); err != nil {
if err = json.NewDecoder(res.Body).Decode(&arr); err != nil {
return api.AppRunResponse{}, err
}

return res, nil
return arr, nil
}

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

_, err := c.BasicRequest("DELETE", u, nil)
res, err := c.Request("DELETE", u, nil)
if err == nil {
res.Body.Close()
}
return err
}

Expand All @@ -159,6 +179,9 @@ func Transfer(c *deis.Client, appID string, username string) error {
return err
}

_, err = c.BasicRequest("POST", u, body)
res, err := c.Request("POST", u, body)
if err == nil {
res.Body.Close()
}
return err
}
37 changes: 28 additions & 9 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package auth

import (
"encoding/json"
"io"
"io/ioutil"

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

_, err = c.BasicRequest("POST", "/v2/auth/register/", body)
res, err := c.Request("POST", "/v2/auth/register/", body)
if err == nil {
res.Body.Close()
}
return err
}

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

body, err := c.BasicRequest("POST", "/v2/auth/login/", reqBody)

res, err := c.Request("POST", "/v2/auth/login/", reqBody)
if err != nil {
return "", err
}
// Fix json.Decoder bug in <go1.7
defer func() {
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
}()

token := api.AuthLoginResponse{}
if err = json.Unmarshal([]byte(body), &token); err != nil {
if err = json.NewDecoder(res.Body).Decode(&token); err != nil {
return "", err
}

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

_, err = c.BasicRequest("DELETE", "/v2/auth/cancel/", body)
res, err := c.Request("DELETE", "/v2/auth/cancel/", body)
if err == nil {
res.Body.Close()
}
return err
}

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

body, err := c.BasicRequest("POST", "/v2/auth/tokens/", reqBody)

res, err := c.Request("POST", "/v2/auth/tokens/", reqBody)
if err != nil {
return "", err
}
// Fix json.Decoder bug in <go1.7
defer func() {
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
}()

if all == true {
return "", nil
}

token := api.AuthRegenerateResponse{}
if err = json.Unmarshal([]byte(body), &token); err != nil {
if err = json.NewDecoder(res.Body).Decode(&token); err != nil {
return "", err
}

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

_, err = c.BasicRequest("POST", "/v2/auth/passwd/", body)
res, err := c.Request("POST", "/v2/auth/passwd/", body)
if err == nil {
res.Body.Close()
}
return err
}
3 changes: 2 additions & 1 deletion auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package auth

import (
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -254,7 +255,7 @@ func TestDeleteUserApp(t *testing.T) {
err = Delete(deis, "admin")
// should be a 409 Conflict

expected := fmt.Errorf("\n%s %s\n\n", "409", "Conflict")
expected := errors.New("409 Conflict")
if reflect.DeepEqual(err, expected) == false {
t.Errorf("got '%s' but expected '%s'", err, expected)
}
Expand Down
12 changes: 9 additions & 3 deletions builds/builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package builds
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"

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

resBody, err := c.BasicRequest("POST", u, body)

res, err := c.Request("POST", u, body)
if err != nil {
return api.Build{}, err
}
// Fix json.Decoder bug in <go1.7
defer func() {
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
}()

build := api.Build{}
if err = json.Unmarshal([]byte(resBody), &build); err != nil {
if err = json.NewDecoder(res.Body).Decode(&build); err != nil {
return api.Build{}, err
}

Expand Down
39 changes: 30 additions & 9 deletions certs/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package certs
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"

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

resBody, err := c.BasicRequest("POST", "/v2/certs/", reqBody)
res, err := c.Request("POST", "/v2/certs/", reqBody)
if err != nil {
return api.Cert{}, err
}
// Fix json.Decoder bug in <go1.7
defer func() {
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
}()

resCert := api.Cert{}
if err = json.Unmarshal([]byte(resBody), &resCert); err != nil {
if err = json.NewDecoder(res.Body).Decode(&resCert); err != nil {
return api.Cert{}, err
}

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

res := api.Cert{}
if err = json.Unmarshal([]byte(body), &res); err != nil {
resCert := api.Cert{}
if err = json.NewDecoder(res.Body).Decode(&resCert); err != nil {
return api.Cert{}, err
}

return res, nil
return resCert, nil
}

// Delete removes a cert.
func Delete(c *deis.Client, name string) error {
url := fmt.Sprintf("/v2/certs/%s", name)
_, err := c.BasicRequest("DELETE", url, nil)
res, err := c.Request("DELETE", url, nil)
if err == nil {
res.Body.Close()
}
return err
}

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

url := fmt.Sprintf("/v2/certs/%s/domain/", name)
_, err = c.BasicRequest("POST", url, reqBody)
res, err := c.Request("POST", url, reqBody)
if err == nil {
res.Body.Close()
}
return err
}

// Detach a certificate from a domain
func Detach(c *deis.Client, name string, domain string) error {
url := fmt.Sprintf("/v2/certs/%s/domain/%s", name, domain)
_, err := c.BasicRequest("DELETE", url, nil)
res, err := c.Request("DELETE", url, nil)
if err == nil {
res.Body.Close()
}
return err
}
Loading

0 comments on commit 7dca4be

Please sign in to comment.