Skip to content

Commit

Permalink
feat: CR-22880 update model packages (#462)
Browse files Browse the repository at this point in the history
* bumped to `v1.1.0`
* updated models, added StringMap
* return better error information
  • Loading branch information
ATGardner authored Mar 24, 2024
1 parent 15e356d commit b6ca9d9
Show file tree
Hide file tree
Showing 9 changed files with 17,443 additions and 3,673 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=v1.0.3
VERSION=v1.1.0

ifndef GOBIN
ifndef GOPATH
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ module github.com/codefresh-io/go-sdk
go 1.22

require (
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
sigs.k8s.io/yaml v1.4.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
14 changes: 4 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
Expand Down
19 changes: 16 additions & 3 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ type (
}

ApiError struct {
response *http.Response
status string
statusCode int
body string
}

GraphqlError struct {
Expand All @@ -60,7 +62,7 @@ type (
)

func (e *ApiError) Error() string {
return e.response.Status
return fmt.Sprintf("API error: %s: %s", e.status, e.body)
}

func NewCfClient(host, token, graphqlPath string, httpClient *http.Client) *CfClient {
Expand Down Expand Up @@ -174,7 +176,18 @@ func (c *CfClient) apiCall(ctx context.Context, baseUrl *url.URL, opt *RequestOp
}

if res.StatusCode >= http.StatusBadRequest {
return nil, &ApiError{response: res}
defer res.Body.Close()
bytes, err := io.ReadAll(res.Body)
body := string(bytes)
if err != nil {
body = fmt.Sprintf("failed to read response Body: %s", err.Error())
}

return nil, &ApiError{
status: res.Status,
statusCode: res.StatusCode,
body: body,
}
}

return res, nil
Expand Down
30 changes: 30 additions & 0 deletions pkg/model/StringMap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package model

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

type StringMap map[string]string

func (m *StringMap) UnmarshalGQL(v interface{}) error {
anyMap, ok := v.(map[string]any)
if !ok {
return fmt.Errorf("StringMap must be a map")
}

*m = make(map[string]string, len(anyMap))
for k, v := range anyMap {
(*m)[k], ok = v.(string)
if !ok {
return fmt.Errorf("StringMap value %q must be strings", k)
}
}

return nil
}

func (m StringMap) MarshalGQL(w io.Writer) {
_ = json.NewEncoder(w).Encode(m)
}
Loading

0 comments on commit b6ca9d9

Please sign in to comment.