Skip to content

Commit b6ca9d9

Browse files
authored
feat: CR-22880 update model packages (#462)
* bumped to `v1.1.0` * updated models, added StringMap * return better error information
1 parent 15e356d commit b6ca9d9

File tree

9 files changed

+17443
-3673
lines changed

9 files changed

+17443
-3673
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=v1.0.3
1+
VERSION=v1.1.0
22

33
ifndef GOBIN
44
ifndef GOPATH

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ module github.com/codefresh-io/go-sdk
33
go 1.22
44

55
require (
6-
github.com/stretchr/testify v1.8.4
6+
github.com/stretchr/testify v1.9.0
77
sigs.k8s.io/yaml v1.4.0
88
)
99

1010
require (
1111
github.com/davecgh/go-spew v1.1.1 // indirect
1212
github.com/pmezard/go-difflib v1.0.0 // indirect
13-
github.com/stretchr/objx v0.5.0 // indirect
13+
github.com/stretchr/objx v0.5.2 // indirect
1414
gopkg.in/yaml.v3 v3.0.1 // indirect
1515
)

go.sum

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
21
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
32
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
43
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
54
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
65
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
76
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
8-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
9-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
10-
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
11-
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
12-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
13-
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
14-
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
15-
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
7+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
8+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
9+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
10+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
1611
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1712
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
18-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1913
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
2014
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2115
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=

pkg/client/client.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ type (
3939
}
4040

4141
ApiError struct {
42-
response *http.Response
42+
status string
43+
statusCode int
44+
body string
4345
}
4446

4547
GraphqlError struct {
@@ -60,7 +62,7 @@ type (
6062
)
6163

6264
func (e *ApiError) Error() string {
63-
return e.response.Status
65+
return fmt.Sprintf("API error: %s: %s", e.status, e.body)
6466
}
6567

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

176178
if res.StatusCode >= http.StatusBadRequest {
177-
return nil, &ApiError{response: res}
179+
defer res.Body.Close()
180+
bytes, err := io.ReadAll(res.Body)
181+
body := string(bytes)
182+
if err != nil {
183+
body = fmt.Sprintf("failed to read response Body: %s", err.Error())
184+
}
185+
186+
return nil, &ApiError{
187+
status: res.Status,
188+
statusCode: res.StatusCode,
189+
body: body,
190+
}
178191
}
179192

180193
return res, nil

pkg/model/StringMap.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package model
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
)
8+
9+
type StringMap map[string]string
10+
11+
func (m *StringMap) UnmarshalGQL(v interface{}) error {
12+
anyMap, ok := v.(map[string]any)
13+
if !ok {
14+
return fmt.Errorf("StringMap must be a map")
15+
}
16+
17+
*m = make(map[string]string, len(anyMap))
18+
for k, v := range anyMap {
19+
(*m)[k], ok = v.(string)
20+
if !ok {
21+
return fmt.Errorf("StringMap value %q must be strings", k)
22+
}
23+
}
24+
25+
return nil
26+
}
27+
28+
func (m StringMap) MarshalGQL(w io.Writer) {
29+
_ = json.NewEncoder(w).Encode(m)
30+
}

0 commit comments

Comments
 (0)