Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Gorequest may return nil values for responses #33

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
8 changes: 8 additions & 0 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ func (c *Client) CreateAccountUserJSON(accountID string, params UserParams) (str
Send(params).
End()

if response == nil {
return internalError(errs)
}

if response.StatusCode != 201 { // Expect Created on success
errs = ProcessErrors(response.StatusCode, body)
}
Expand All @@ -120,6 +124,10 @@ func (c *Client) DeleteAccountUserJSON(accountID, userID string) (string, []erro
apibase+"accounts/"+accountID+"/users/"+userID).
End()

if response == nil {
return internalError(errs)
}

if response.StatusCode != 200 { // Expect OK on success
errs = ProcessErrors(response.StatusCode, body)
}
Expand Down
8 changes: 8 additions & 0 deletions audit_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func (c *Client) GetAuditEventsJSON(params AuditEventsParams) (string, []error)
Query(params).
End()

if response == nil {
return internalError(errs)
}

if response.StatusCode != 200 { // Expect Accepted on success - assume error on anything else
myerrors := Errors{}
err := json.Unmarshal([]byte(body), &myerrors)
Expand Down Expand Up @@ -86,6 +90,10 @@ func (c *Client) GetAuditEventJSON(id string) (string, []error) {
response, body, errs := c.newRequest("GET", apibase+"/audit_events/"+id).
End()

if response == nil {
return internalError(errs)
}

if response.StatusCode != 200 { // Expect Accepted on success - assume error on anything else
errs = ProcessErrors(response.StatusCode, body)
}
Expand Down
8 changes: 8 additions & 0 deletions backups.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func (c *Client) StartBackupForDeploymentJSON(deploymentid string) (string, []er
response, body, errs := c.newRequest("POST", apibase+"deployments/"+deploymentid+"/backups").
End()

if response == nil {
return internalError(errs)
}

if response.StatusCode != 202 { // Expect Accepted on success - assume error on anything else
errs = ProcessErrors(response.StatusCode, body)
}
Expand Down Expand Up @@ -145,6 +149,10 @@ func (c *Client) RestoreBackupJSON(params RestoreBackupParams) (string, []error)
Send(backupparams).
End()

if response == nil {
return internalError(errs)
}

if response.StatusCode != 202 { // Expect Accepted on success - assume error on anything else
errs = ProcessErrors(response.StatusCode, body)
}
Expand Down
14 changes: 13 additions & 1 deletion composeapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package composeapi

import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -30,7 +31,8 @@ import (
)

const (
apibase = "https://api.compose.io/2016-07/"
apibase = "https://api.compose.io/2016-07/"
gorequestInternalFailure = "An internal error occurred. Could not complete request"
)

// Client is a structure that holds session information for the API
Expand Down Expand Up @@ -122,6 +124,9 @@ func (c *Client) SetAPIToken(newtoken string) {
//GetJSON Gets JSON string of content at an endpoint
func (c *Client) getJSON(endpoint string) (string, []error) {
response, body, errs := c.newRequest("GET", apibase+endpoint).End()
if response == nil {
return internalError(errs)
}

if response.StatusCode != 200 {
errs = ProcessErrors(response.StatusCode, body)
Expand Down Expand Up @@ -151,3 +156,10 @@ func ProcessErrors(statuscode int, body string) []error {

return errs
}

func internalError(errs []error) (string, []error) {
if len(errs) > 0 {
return "", errs
}
return "", []error{errors.New(gorequestInternalFailure)}
}
4 changes: 4 additions & 0 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func (c *Client) UpdateVersionJSON(deploymentID string, version string) (string,
Send(patchParams).
End()

if response == nil {
return internalError(errs)
}

if response.StatusCode != 200 { // Expect OK on success - assume error on anything else
errs = ProcessErrors(response.StatusCode, body)
}
Expand Down
12 changes: 12 additions & 0 deletions deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ func (c *Client) CreateDeploymentJSON(params DeploymentParams) (string, []error)
Send(deploymentparams).
End()

if response == nil {
return internalError(errs)
}

if response.StatusCode != 202 { // Expect Accepted on success - assume error on anything else
myerrors := Errors{}
err := json.Unmarshal([]byte(body), &myerrors)
Expand Down Expand Up @@ -223,6 +227,10 @@ func (c *Client) DeprovisionDeploymentJSON(deploymentID string) (string, []error
response, body, errs := c.newRequest("DELETE", apibase+"deployments/"+deploymentID).
End()

if response == nil {
return internalError(errs)
}

if response.StatusCode != 202 { // Expect Accepted on success - assume error on anything else
errs = ProcessErrors(response.StatusCode, body)
}
Expand Down Expand Up @@ -260,6 +268,10 @@ func (c *Client) PatchDeploymentJSON(params PatchDeploymentParams) (string, []er
Send(patchParams).
End()

if response == nil {
return internalError(errs)
}

if response.StatusCode != 200 { // Expect Accepted on success - assume error on anything else
errs = ProcessErrors(response.StatusCode, body)
}
Expand Down
4 changes: 4 additions & 0 deletions scalings.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func (c *Client) SetScalingsJSON(params ScalingsParams) (string, []error) {
Send(scalingsparams).
End()

if response == nil {
return internalError(errs)
}

if response.StatusCode != 200 { // Expect Accepted on success - assume error on anything else
errs = ProcessErrors(response.StatusCode, body)
}
Expand Down
4 changes: 4 additions & 0 deletions tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func (c *Client) updateClusterTagsJSON(clusterID, method string, tags []string)
Send(clusterTags{ClusterTags: clusterTagList{Tags: tags}}).
End()

if response == nil {
return internalError(errs)
}

if response.StatusCode != 200 { // Expect OK on success - assume error on anything else
errs = ProcessErrors(response.StatusCode, body)
}
Expand Down
12 changes: 12 additions & 0 deletions team_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package composeapi

import (
"encoding/json"
"errors"
"fmt"
)

Expand Down Expand Up @@ -48,6 +49,10 @@ func (c *Client) CreateTeamRoleJSON(deploymentID string, params TeamRoleParams)
Send(updateTeamRole{TeamRole: params}).
End()

if response == nil {
return internalError(errs)
}

if response.StatusCode != 201 {
myErrors := Errors{}
err := json.Unmarshal([]byte(body), &myErrors)
Expand Down Expand Up @@ -99,6 +104,13 @@ func (c *Client) DeleteTeamRoleJSON(deploymentID string, params TeamRoleParams)
Send(updateTeamRole{TeamRole: params}).
End()

if response == nil {
if len(errs) > 0 {
return errs
}
return []error{errors.New(gorequestInternalFailure)}
}

if response.StatusCode != 204 { // No response body is returned on success
errs = ProcessErrors(response.StatusCode, body)
}
Expand Down
16 changes: 16 additions & 0 deletions teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func (c *Client) CreateTeamJSON(params TeamParams) (string, []error) {
Send(teamParams).
End()

if response == nil {
return internalError(errs)
}

if response.StatusCode != 201 { // Expect Created on success - assume error on anything else
errs = ProcessErrors(response.StatusCode, body)
}
Expand Down Expand Up @@ -137,6 +141,10 @@ func (c *Client) DeleteTeamJSON(teamID string) (string, []error) {
response, body, errs := c.newRequest("DELETE", apibase+"teams/"+teamID).
End()

if response == nil {
return internalError(errs)
}

if response.StatusCode != 200 { // Expect OK on success - assume error on anything else
errs = ProcessErrors(response.StatusCode, body)
}
Expand Down Expand Up @@ -165,6 +173,10 @@ func (c *Client) PatchTeamJSON(teamID, teamName string) (string, []error) {
Send(patchParams).
End()

if response == nil {
return internalError(errs)
}

if response.StatusCode != 200 { // Expect OK on success - assume error on anything else
errs = ProcessErrors(response.StatusCode, body)
}
Expand Down Expand Up @@ -193,6 +205,10 @@ func (c *Client) PutTeamUsersJSON(teamID string, userIDs []string) (string, []er
Send(putUsers).
End()

if response == nil {
return internalError(errs)
}

if response.StatusCode != 200 { // Expect OK on success - assume error on anything else
errs = ProcessErrors(response.StatusCode, body)
}
Expand Down
4 changes: 4 additions & 0 deletions whitelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (c *Client) createDeploymentWhitelistJSON(deploymentID string, params Deplo
Send(whitelistParams).
End()

if response == nil {
return internalError(errs)
}

if response.StatusCode != 202 {
myerrors := Errors{}
err := json.Unmarshal([]byte(body), &myerrors)
Expand Down