Skip to content

Commit

Permalink
Core: introduce status interface and implement in all clients
Browse files Browse the repository at this point in the history
  • Loading branch information
loafoe committed Aug 31, 2022
1 parent 9cf18be commit fb81152
Show file tree
Hide file tree
Showing 144 changed files with 436 additions and 319 deletions.
11 changes: 10 additions & 1 deletion ai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ type Response struct {
*http.Response
}

func (r *Response) StatusCode() int {
if r.Response != nil {
return r.Response.StatusCode
}
return 0
}

// newResponse creates a new Response for the provided http.Response.
func newResponse(r *http.Response) *Response {
response := &Response{Response: r}
Expand All @@ -243,7 +250,9 @@ func (c *Client) Do(req *http.Request, v interface{}) (*Response, error) {
}

if v != nil {
defer resp.Body.Close() // Only close if we plan to read it
defer func() {
_ = resp.Body.Close()
}() // Only close if we plan to read it
if w, ok := v.(io.Writer); ok {
_, err = io.Copy(w, resp.Body)
} else {
Expand Down
2 changes: 1 addition & 1 deletion ai/compute_environment_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (s *ComputeEnvironmentService) GetComputeEnvironments(opt *GetOptions, opti
}
resp, err := s.Client.Do(req, &bundleResponse)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
if resp != nil && resp.StatusCode() == http.StatusNotFound {
return nil, resp, ErrEmptyResult
}
return nil, resp, err
Expand Down
2 changes: 1 addition & 1 deletion ai/compute_target_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (s *ComputeTargetService) GetComputeTargets(opt *GetOptions, options ...Opt
}
resp, err := s.client.Do(req, &bundleResponse)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
if resp != nil && resp.StatusCode() == http.StatusNotFound {
return nil, resp, ErrEmptyResult
}
return nil, resp, err
Expand Down
2 changes: 1 addition & 1 deletion ai/inference/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func TestMethodNotAllowed(t *testing.T) {
})
assert.NotNil(t, err)
assert.NotNil(t, resp)
assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode)
assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode())
}

func TestDebug(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion ai/inference/compute_environment_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ func TestComputeEnvironmentCRD(t *testing.T) {
if !assert.NotNil(t, resp) {
return
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, resp.StatusCode())
}
2 changes: 1 addition & 1 deletion ai/inference/compute_target_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,5 @@ func TestComputeTargetCRD(t *testing.T) {
if !assert.NotNil(t, resp) {
return
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, resp.StatusCode())
}
2 changes: 1 addition & 1 deletion ai/inference/job_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,5 @@ func TestJobCRD(t *testing.T) {
if !assert.NotNil(t, resp) {
return
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, resp.StatusCode())
}
2 changes: 1 addition & 1 deletion ai/inference/model_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (s *ModelService) GetModels(opt *ai.GetOptions, options ...ai.OptionFunc) (
}
resp, err := s.client.Do(req, &bundleResponse)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
if resp != nil && resp.StatusCode() == http.StatusNotFound {
return nil, resp, ai.ErrEmptyResult
}
return nil, resp, err
Expand Down
2 changes: 1 addition & 1 deletion ai/inference/model_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,5 @@ func TestModelCRD(t *testing.T) {
if !assert.NotNil(t, resp) {
return
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, resp.StatusCode())
}
2 changes: 1 addition & 1 deletion ai/job_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (s *JobService) GetJobs(opt *GetOptions, options ...OptionFunc) ([]Job, *Re
}
resp, err := s.Client.Do(req, &bundleResponse)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
if resp != nil && resp.StatusCode() == http.StatusNotFound {
return nil, resp, ErrEmptyResult
}
return nil, resp, err
Expand Down
2 changes: 1 addition & 1 deletion ai/training/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func TestMethodNotAllowed(t *testing.T) {
})
assert.NotNil(t, err)
assert.NotNil(t, resp)
assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode)
assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode())
}

func TestDebug(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion ai/workspace/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func TestMethodNotAllowed(t *testing.T) {
})
assert.NotNil(t, err)
assert.NotNil(t, resp)
assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode)
assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode())
}

func TestDebug(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions ai/workspace/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (s *Service) GetWorkspaces(opt *ai.GetOptions, options ...ai.OptionFunc) ([
}
resp, err := s.client.Do(req, &bundleResponse)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
if resp != nil && resp.StatusCode() == http.StatusNotFound {
return nil, resp, ai.ErrEmptyResult
}
return nil, resp, err
Expand Down Expand Up @@ -161,7 +161,7 @@ func (s *Service) GetWorkspaceAccessURL(ws Workspace) (*AccessURL, *ai.Response,
var accessURL AccessURL
resp, err := s.client.Do(req, &accessURL)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
if resp != nil && resp.StatusCode() == http.StatusNotFound {
return nil, resp, ai.ErrEmptyResult
}
return nil, resp, err
Expand All @@ -179,7 +179,7 @@ func (s *Service) GetWorkspaceLogs(ws Workspace) (*LogArtefact, *ai.Response, er
var artefact LogArtefact
resp, err := s.client.Do(req, &artefact)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
if resp != nil && resp.StatusCode() == http.StatusNotFound {
return nil, resp, ai.ErrEmptyResult
}
return nil, resp, err
Expand Down
7 changes: 7 additions & 0 deletions audit/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ type Response struct {
*http.Response
}

func (r *Response) StatusCode() int {
if r.Response != nil {
return r.Response.StatusCode
}
return 0
}

func newResponse(r *http.Response) *Response {
response := &Response{Response: r}
return response
Expand Down
2 changes: 1 addition & 1 deletion audit/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (c *Client) CreateAuditEvent(event *dstu2pb.AuditEvent) (*stu3pb.ContainedR
return nil, resp, doErr
}
contained := &stu3pb.ContainedResource{}
if resp.StatusCode == http.StatusCreated {
if resp.StatusCode() == http.StatusCreated {
return contained, resp, nil
}
// OperationOutcome
Expand Down
2 changes: 1 addition & 1 deletion audit/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func TestBadRequest(t *testing.T) {
if !assert.NotNil(t, resp) {
return
}
if !assert.Equal(t, http.StatusBadRequest, resp.StatusCode) {
if !assert.Equal(t, http.StatusBadRequest, resp.StatusCode()) {
return
}
assert.Nil(t, contained)
Expand Down
10 changes: 5 additions & 5 deletions blr/blobs_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (b *BlobsService) Delete(blob Blob) (bool, *Response, error) {
var deleteResponse interface{}

resp, err := b.Do(req, &deleteResponse)
if resp == nil || resp.StatusCode != http.StatusNoContent {
if resp == nil || resp.StatusCode() != http.StatusNoContent {
return false, resp, err
}
return true, resp, nil
Expand All @@ -219,7 +219,7 @@ func (b *BlobsService) SetPolicy(blob Blob, policy BlobPolicy) (bool, *Response,
var setPolicyResponse interface{}

resp, err := b.Do(req, &setPolicyResponse)
if resp == nil || resp.StatusCode != http.StatusNoContent {
if resp == nil || resp.StatusCode() != http.StatusNoContent {
return false, resp, err
}
return true, resp, nil
Expand Down Expand Up @@ -256,7 +256,7 @@ func (b *BlobsService) DeletePolicy(blob Blob) (bool, *Response, error) {
var deleteResponse interface{}

resp, err := b.Do(req, &deleteResponse)
if resp == nil || resp.StatusCode != http.StatusNoContent {
if resp == nil || resp.StatusCode() != http.StatusNoContent {
return false, resp, err
}
return true, resp, nil
Expand Down Expand Up @@ -293,7 +293,7 @@ func (b *BlobsService) CompleteUpload(blob Blob, parts BlobPartUpload) (bool, *R
var completeUploadResponse interface{}

resp, err := b.Do(req, &completeUploadResponse)
if resp == nil || resp.StatusCode != http.StatusNoContent {
if resp == nil || resp.StatusCode() != http.StatusNoContent {
return false, resp, err
}
return true, resp, nil
Expand All @@ -309,7 +309,7 @@ func (b *BlobsService) AbortUpload(blob Blob) (bool, *Response, error) {
var abortUploadResponse interface{}

resp, err := b.Do(req, &abortUploadResponse)
if resp == nil || resp.StatusCode != http.StatusNoContent {
if resp == nil || resp.StatusCode() != http.StatusNoContent {
return false, resp, err
}
return true, resp, nil
Expand Down
2 changes: 1 addition & 1 deletion blr/blobs_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ func TestBlobCRUD(t *testing.T) {
return
}
assert.True(t, res)
assert.Equal(t, http.StatusNoContent, resp.StatusCode)
assert.Equal(t, http.StatusNoContent, resp.StatusCode())
}
7 changes: 7 additions & 0 deletions blr/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ type Response struct {
*http.Response
}

func (r *Response) StatusCode() int {
if r.Response != nil {
return r.Response.StatusCode
}
return 0
}

// newResponse creates a new Response for the provided http.Response.
func newResponse(r *http.Response) *Response {
response := &Response{Response: r}
Expand Down
2 changes: 1 addition & 1 deletion cartel/add_security_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ func TestAddSecurityGroups(t *testing.T) {
if !assert.NotNil(t, aur) {
return
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, resp.StatusCode())
assert.Equal(t, true, aur.Success())
}
2 changes: 1 addition & 1 deletion cartel/add_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ func TestAddTags(t *testing.T) {
if !assert.NotNil(t, atr) {
return
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, resp.StatusCode())
assert.Equal(t, true, atr.Success())
}
2 changes: 1 addition & 1 deletion cartel/add_user_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ func TestAddUserGroups(t *testing.T) {
if !assert.NotNil(t, aur) {
return
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, resp.StatusCode())
assert.Equal(t, true, aur.Success())
}
2 changes: 1 addition & 1 deletion cartel/all_instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestAllInstances(t *testing.T) {
if !assert.NotNil(t, instances) {
return
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, resp.StatusCode())
if !assert.Equal(t, 1, len(*instances)) {
return
}
Expand Down
11 changes: 10 additions & 1 deletion cartel/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ type Response struct {
Message string
}

func (r *Response) StatusCode() int {
if r.Response != nil {
return r.Response.StatusCode
}
return 0
}

// OptionFunc is the function signature function for options
type OptionFunc func(*http.Request) error

Expand Down Expand Up @@ -150,7 +157,9 @@ func (c *Client) do(req *http.Request, v interface{}) (*Response, error) {
if resp == nil || (err != nil && err != io.EOF) {
return nil, fmt.Errorf("client.do: %w", err)
}
defer resp.Body.Close()
defer func() {
_ = resp.Body.Close()
}()
response := newResponse(resp)
if v != nil {
if w, ok := v.(io.Writer); ok {
Expand Down
4 changes: 2 additions & 2 deletions cartel/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestCreate(t *testing.T) {
return
}
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, resp.StatusCode())
assert.Equal(t, true, sr.Success())
assert.Equal(t, "i-xxfbdf005781fa900", sr.InstanceID())
assert.Equal(t, "192.168.2.106", sr.IPAddress())
Expand Down Expand Up @@ -85,6 +85,6 @@ func TestAlreadyExist(t *testing.T) {
if !assert.NotNil(t, resp) {
return
}
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
assert.Equal(t, http.StatusBadRequest, resp.StatusCode())
assert.Equal(t, ErrHostnameAlreadyExists, err)
}
2 changes: 1 addition & 1 deletion cartel/deployment_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ func TestDeploymentState(t *testing.T) {
if !assert.NotNil(t, resp) {
return
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, resp.StatusCode())
assert.Equal(t, "succeeded", aur)
}
2 changes: 1 addition & 1 deletion cartel/destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ func TestDestroy(t *testing.T) {
return
}
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, resp.StatusCode())
assert.Equal(t, true, sr.Success())
}
2 changes: 1 addition & 1 deletion cartel/details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestDetails(t *testing.T) {
if !assert.NotNil(t, details) {
return
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, resp.StatusCode())
assert.Equal(t, "container-host", details.Role)
assert.Equal(t, "2019-11-29T18:17:44.000Z", details.LaunchTime)
}
Expand Down
2 changes: 1 addition & 1 deletion cartel/remove_security_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ func TestRemoveSecurityGroups(t *testing.T) {
if !assert.NotNil(t, aur) {
return
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, resp.StatusCode())
assert.Equal(t, true, aur.Success())
}
2 changes: 1 addition & 1 deletion cartel/remove_user_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ func TestRemoveUserGroups(t *testing.T) {
if !assert.NotNil(t, aur) {
return
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, resp.StatusCode())
assert.Equal(t, true, aur.Success())
}
2 changes: 1 addition & 1 deletion cartel/roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestRoles(t *testing.T) {
if !assert.NotNil(t, roles) {
return
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, resp.StatusCode())
if !assert.Equal(t, 3, len(*roles)) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion cartel/security_group_details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestSecurityGroupDetails(t *testing.T) {
return
}
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, resp.StatusCode())
if !assert.Equal(t, 1, len(*details)) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion cartel/security_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ func TestSecurityGroups(t *testing.T) {
return
}
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, resp.StatusCode())
assert.Equal(t, 3, len(*groups))
}
2 changes: 1 addition & 1 deletion cartel/set_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ func TestSetProtect(t *testing.T) {
return
}
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, resp.StatusCode())
assert.Equal(t, true, pr.Success())
}
2 changes: 1 addition & 1 deletion cartel/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (c *Client) Start(nameTag string) (*StartResponse, *Response, error) {
var responseBody StartResponse
resp, err := c.do(req, &responseBody)
if resp != nil {
responseBody.Code = resp.StatusCode
responseBody.Code = resp.StatusCode()
}
return &responseBody, resp, err
}
Loading

0 comments on commit fb81152

Please sign in to comment.