Skip to content

Commit 487371b

Browse files
committed
use the graphqlAPI in all relevant calls
handle non-error responses which represent server errors.
1 parent 67ee829 commit 487371b

File tree

4 files changed

+81
-134
lines changed

4 files changed

+81
-134
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.31.2
1+
0.32.0

pkg/codefresh/argo_runtime.go

Lines changed: 50 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package codefresh
22

33
import (
4-
"encoding/json"
4+
"context"
55
"fmt"
6-
"io/ioutil"
76

87
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
98
)
109

1110
type (
1211
IArgoRuntimeAPI interface {
13-
List() ([]model.Runtime, error)
14-
Create(runtimeName, cluster, runtimeVersion string) (*model.RuntimeCreationResponse, error)
12+
List(ctx context.Context) ([]model.Runtime, error)
13+
Create(ctx context.Context, runtimeName, cluster, runtimeVersion string) (*model.RuntimeCreationResponse, error)
1514
}
1615
argoRuntime struct {
1716
codefresh *codefresh
@@ -35,107 +34,75 @@ func newArgoRuntimeAPI(codefresh *codefresh) IArgoRuntimeAPI {
3534
return &argoRuntime{codefresh: codefresh}
3635
}
3736

38-
func (r *argoRuntime) Create(runtimeName, cluster, runtimeVersion string) (*model.RuntimeCreationResponse, error) {
37+
func (r *argoRuntime) List(ctx context.Context) ([]model.Runtime, error) {
3938
jsonData := map[string]interface{}{
40-
"query": `mutation CreateRuntime($name: String!, $cluster: String!, $runtimeVersion: String!) {
41-
runtime(name: $name, cluster: $cluster, runtimeVersion: $runtimeVersion) {
42-
name
43-
newAccessToken
44-
}
45-
}`,
46-
"variables": map[string]interface{}{
47-
"name": runtimeName,
48-
"cluster": cluster,
49-
"runtimeVersion": runtimeVersion,
50-
},
51-
}
52-
53-
response, err := r.codefresh.requestAPI(&requestOptions{
54-
method: "POST",
55-
path: qlEndPoint,
56-
body: jsonData,
57-
})
58-
59-
if err != nil {
60-
fmt.Printf("The HTTP request failed with error %s\n", err)
61-
return nil, err
62-
}
63-
64-
defer response.Body.Close()
65-
66-
data, err := ioutil.ReadAll(response.Body)
67-
if err != nil {
68-
fmt.Printf("failed to read from response body")
69-
return nil, err
39+
"query": `{
40+
runtimes {
41+
edges {
42+
node {
43+
metadata {
44+
name
45+
namespace
46+
}
47+
self {
48+
healthStatus
49+
version
50+
}
51+
cluster
52+
}
53+
}
54+
}
55+
}`,
7056
}
7157

72-
res := graphQlRuntimeCreationResponse{}
73-
err = json.Unmarshal(data, &res)
58+
res := &graphqlRuntimesResponse{}
59+
err := r.codefresh.graphqlAPI(ctx, jsonData, res)
7460
if err != nil {
75-
return nil, err
61+
return nil, fmt.Errorf("failed getting runtime list: %w", err)
7662
}
7763

7864
if len(res.Errors) > 0 {
7965
return nil, graphqlErrorResponse{errors: res.Errors}
8066
}
8167

82-
return &res.Data.Runtime, nil
68+
runtimes := make([]model.Runtime, len(res.Data.Runtimes.Edges))
69+
for i := range res.Data.Runtimes.Edges {
70+
runtimes[i] = *res.Data.Runtimes.Edges[i].Node
71+
}
72+
73+
return runtimes, nil
8374
}
8475

85-
func (r *argoRuntime) List() ([]model.Runtime, error) {
76+
func (r *argoRuntime) Create(ctx context.Context, runtimeName, cluster, runtimeVersion string) (*model.RuntimeCreationResponse, error) {
8677
jsonData := map[string]interface{}{
87-
"query": `{
88-
runtimes {
89-
edges {
90-
node {
91-
metadata {
92-
name
93-
namespace
94-
}
95-
self {
96-
healthStatus
97-
version
78+
"query": `
79+
mutation CreateRuntime(
80+
$name: String!
81+
$cluster: String!
82+
$runtimeVersion: String!
83+
) {
84+
runtime(name: $name, cluster: $cluster, runtimeVersion: $runtimeVersion) {
85+
name
86+
newAccessToken
9887
}
99-
cluster
100-
}
10188
}
102-
}
103-
}
104-
`,
105-
}
106-
107-
response, err := r.codefresh.requestAPI(&requestOptions{
108-
method: "POST",
109-
path: qlEndPoint,
110-
body: jsonData,
111-
})
112-
if err != nil {
113-
fmt.Printf("The HTTP request failed with error %s\n", err)
114-
return nil, err
115-
}
116-
defer response.Body.Close()
117-
118-
data, err := ioutil.ReadAll(response.Body)
119-
if err != nil {
120-
fmt.Printf("failed to read from response body")
121-
return nil, err
89+
`,
90+
"variables": map[string]interface{}{
91+
"name": runtimeName,
92+
"cluster": cluster,
93+
"runtimeVersion": runtimeVersion,
94+
},
12295
}
12396

124-
res := graphqlRuntimesResponse{}
125-
err = json.Unmarshal(data, &res)
126-
97+
res := &graphQlRuntimeCreationResponse{}
98+
err := r.codefresh.graphqlAPI(ctx, jsonData, res)
12799
if err != nil {
128-
return nil, err
100+
return nil, fmt.Errorf("failed getting runtime list: %w", err)
129101
}
130102

131103
if len(res.Errors) > 0 {
132104
return nil, graphqlErrorResponse{errors: res.Errors}
133105
}
134106

135-
runtimes := make([]model.Runtime, len(res.Data.Runtimes.Edges))
136-
for i := range res.Data.Runtimes.Edges {
137-
runtimes[i] = *res.Data.Runtimes.Edges[i].Node
138-
}
139-
140-
return runtimes, nil
107+
return &res.Data.Runtime, nil
141108
}

pkg/codefresh/codefresh.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"errors"
78
"fmt"
89
"io/ioutil"
910
"net/http"
1011
"strings"
1112

12-
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
1313
"github.com/google/go-querystring/query"
1414
)
1515

@@ -33,13 +33,6 @@ type (
3333
GitSource() IGitSourceAPI
3434
Component() IComponentAPI
3535
}
36-
37-
graphqlResponse struct {
38-
Data struct {
39-
Runtimes model.RuntimePage
40-
}
41-
Errors []graphqlError
42-
}
4336
)
4437

4538
var qlEndPoint = "/2.0/api/graphql"
@@ -148,6 +141,11 @@ func (c *codefresh) graphqlAPI(ctx context.Context, body map[string]interface{},
148141
}
149142
defer response.Body.Close()
150143

144+
statusOK := response.StatusCode >= 200 && response.StatusCode < 300
145+
if !statusOK {
146+
return errors.New(response.Status)
147+
}
148+
151149
data, err := ioutil.ReadAll(response.Body)
152150
if err != nil {
153151
return fmt.Errorf("failed to read from response body: %w", err)

pkg/codefresh/git-source.go

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package codefresh
22

33
import (
4-
"encoding/json"
4+
"context"
55
"fmt"
6-
"io/ioutil"
76

87
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
98
)
109

1110
type (
1211
IGitSourceAPI interface {
13-
List(runtimeName string) ([]model.GitSource, error)
12+
List(ctc context.Context, runtimeName string) ([]model.GitSource, error)
1413
}
1514

1615
gitSource struct {
@@ -29,54 +28,37 @@ func newGitSourceAPI(codefresh *codefresh) IGitSourceAPI {
2928
return &gitSource{codefresh: codefresh}
3029
}
3130

32-
func (g *gitSource) List(runtimeName string) ([]model.GitSource, error) {
31+
func (g *gitSource) List(ctx context.Context, runtimeName string) ([]model.GitSource, error) {
3332
jsonData := map[string]interface{}{
34-
"query": `query GitSources($runtime: String) {
35-
gitSources(runtime: $runtime) {
36-
edges {
37-
node {
38-
metadata {
39-
name
40-
}
41-
self {
42-
path
43-
repoURL
44-
status {
45-
syncStatus
46-
healthStatus
33+
"query": `
34+
query GitSources($runtime: String) {
35+
gitSources(runtime: $runtime) {
36+
edges {
37+
node {
38+
metadata {
39+
name
40+
}
41+
self {
42+
path
43+
repoURL
44+
status {
45+
syncStatus
46+
healthStatus
47+
}
48+
}
49+
}
4750
}
48-
}
4951
}
50-
}
51-
}
52-
}`,
52+
}`,
5353
"variables": map[string]interface{}{
5454
"runtime": runtimeName,
5555
},
5656
}
5757

58-
response, err := g.codefresh.requestAPI(&requestOptions{
59-
method: "POST",
60-
path: "/2.0/api/graphql",
61-
body: jsonData,
62-
})
58+
res := &graphQlGitSourcesListResponse{}
59+
err := g.codefresh.graphqlAPI(ctx, jsonData, res)
6360
if err != nil {
64-
fmt.Printf("The HTTP request failed with error %s\n", err)
65-
return nil, err
66-
}
67-
defer response.Body.Close()
68-
69-
data, err := ioutil.ReadAll(response.Body)
70-
if err != nil {
71-
fmt.Printf("failed to read from response body")
72-
return nil, err
73-
}
74-
75-
res := graphQlGitSourcesListResponse{}
76-
err = json.Unmarshal(data, &res)
77-
78-
if err != nil {
79-
return nil, err
61+
return nil, fmt.Errorf("failed getting git-source list: %w", err)
8062
}
8163

8264
gitSources := make([]model.GitSource, len(res.Data.GitSources.Edges))

0 commit comments

Comments
 (0)