From 524dccef13f5596f13c761c8da0034e019742964 Mon Sep 17 00:00:00 2001 From: yyoshiki41 Date: Fri, 5 Oct 2018 23:11:06 +0900 Subject: [PATCH] refactor http response --- api/api.go | 10 ++++++---- providers/github.go | 18 +++++++++--------- providers/google.go | 10 ++++++---- providers/internal_util.go | 4 ++-- providers/provider_default.go | 5 +++-- 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/api/api.go b/api/api.go index e8378ff9d..fb413ecbf 100644 --- a/api/api.go +++ b/api/api.go @@ -16,13 +16,14 @@ func Request(req *http.Request) (*simplejson.Json, error) { log.Printf("%s %s %s", req.Method, req.URL, err) return nil, err } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) - resp.Body.Close() log.Printf("%d %s %s %s", resp.StatusCode, req.Method, req.URL, body) if err != nil { return nil, err } - if resp.StatusCode != 200 { + if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("got %d %s", resp.StatusCode, body) } data, err := simplejson.NewJson(body) @@ -38,13 +39,14 @@ func RequestJson(req *http.Request, v interface{}) error { log.Printf("%s %s %s", req.Method, req.URL, err) return err } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) - resp.Body.Close() log.Printf("%d %s %s %s", resp.StatusCode, req.Method, req.URL, body) if err != nil { return err } - if resp.StatusCode != 200 { + if resp.StatusCode != http.StatusOK { return fmt.Errorf("got %d %s", resp.StatusCode, body) } return json.Unmarshal(body, v) diff --git a/providers/github.go b/providers/github.go index 26526ce76..59623fe43 100644 --- a/providers/github.go +++ b/providers/github.go @@ -86,13 +86,13 @@ func (p *GitHubProvider) hasOrg(accessToken string) (bool, error) { if err != nil { return false, err } + defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) - resp.Body.Close() if err != nil { return false, err } - if resp.StatusCode != 200 { + if resp.StatusCode != http.StatusOK { return false, fmt.Errorf( "got %d from %q %s", resp.StatusCode, endpoint.String(), body) } @@ -150,13 +150,13 @@ func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) { if err != nil { return false, err } + defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) - resp.Body.Close() if err != nil { return false, err } - if resp.StatusCode != 200 { + if resp.StatusCode != http.StatusOK { return false, fmt.Errorf( "got %d from %q %s", resp.StatusCode, endpoint.String(), body) } @@ -225,13 +225,13 @@ func (p *GitHubProvider) GetEmailAddress(s *SessionState) (string, error) { if err != nil { return "", err } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) - resp.Body.Close() if err != nil { return "", err } - - if resp.StatusCode != 200 { + if resp.StatusCode != http.StatusOK { return "", fmt.Errorf("got %d from %q %s", resp.StatusCode, endpoint.String(), body) } @@ -273,14 +273,14 @@ func (p *GitHubProvider) GetUserName(s *SessionState) (string, error) { if err != nil { return "", err } + defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) - defer resp.Body.Close() if err != nil { return "", err } - if resp.StatusCode != 200 { + if resp.StatusCode != http.StatusOK { return "", fmt.Errorf("got %d from %q %s", resp.StatusCode, endpoint.String(), body) } diff --git a/providers/google.go b/providers/google.go index 66406bd2b..dd2532e61 100644 --- a/providers/google.go +++ b/providers/google.go @@ -113,14 +113,15 @@ func (p *GoogleProvider) Redeem(redirectURL, code string) (s *SessionState, err if err != nil { return } + defer resp.Body.Close() + var body []byte body, err = ioutil.ReadAll(resp.Body) - resp.Body.Close() if err != nil { return } - if resp.StatusCode != 200 { + if resp.StatusCode != http.StatusOK { err = fmt.Errorf("got %d from %q %s", resp.StatusCode, p.RedeemURL.String(), body) return } @@ -289,14 +290,15 @@ func (p *GoogleProvider) redeemRefreshToken(refreshToken string) (token string, if err != nil { return } + defer resp.Body.Close() + var body []byte body, err = ioutil.ReadAll(resp.Body) - resp.Body.Close() if err != nil { return } - if resp.StatusCode != 200 { + if resp.StatusCode != http.StatusOK { err = fmt.Errorf("got %d from %q %s", resp.StatusCode, p.RedeemURL.String(), body) return } diff --git a/providers/internal_util.go b/providers/internal_util.go index 924d41197..88586ef20 100644 --- a/providers/internal_util.go +++ b/providers/internal_util.go @@ -61,12 +61,12 @@ func validateToken(p Provider, access_token string, header http.Header) bool { log.Printf("token validation request failed: %s", err) return false } + defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) - resp.Body.Close() log.Printf("%d GET %s %s", resp.StatusCode, stripToken(endpoint), body) - if resp.StatusCode == 200 { + if resp.StatusCode == http.StatusOK { return true } log.Printf("token validation request failed: status %d - %s", resp.StatusCode, body) diff --git a/providers/provider_default.go b/providers/provider_default.go index 355e6c37e..711de62ee 100644 --- a/providers/provider_default.go +++ b/providers/provider_default.go @@ -40,14 +40,15 @@ func (p *ProviderData) Redeem(redirectURL, code string) (s *SessionState, err er if err != nil { return nil, err } + defer resp.Body.Close() + var body []byte body, err = ioutil.ReadAll(resp.Body) - resp.Body.Close() if err != nil { return } - if resp.StatusCode != 200 { + if resp.StatusCode != http.StatusOK { err = fmt.Errorf("got %d from %q %s", resp.StatusCode, p.RedeemURL.String(), body) return }