Skip to content
This repository has been archived by the owner on Jan 24, 2019. It is now read-only.

refactor http request #656

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions providers/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 6 additions & 4 deletions providers/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions providers/internal_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions providers/provider_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down