1+ package codefresh
2+
3+ import (
4+ "bytes"
5+ "encoding/json"
6+ "fmt"
7+ "io/ioutil"
8+
9+ "github.com/codefresh-io/argo-platform/libs/ql/graph/model"
10+ )
11+
12+
13+ type (
14+ IArgoRuntimeAPI interface {
15+ List () ([]model.Runtime , error )
16+ }
17+ argoRuntime struct {
18+ codefresh * codefresh
19+ }
20+ graphqlRuntimesResponse struct {
21+ Data graphqlRuntimesInnerResponse `json:"data"`
22+ // Errors []byte
23+ }
24+ graphqlRuntimesInnerResponse struct {
25+ Runtimes model.RuntimePage `json:"runtimes"`
26+ }
27+ )
28+
29+ func newArgoRuntimeAPI (codefresh * codefresh ) IArgoRuntimeAPI {
30+ return & argoRuntime {codefresh : codefresh }
31+ }
32+ func (r * argoRuntime ) List () ([]model.Runtime , error ) {
33+
34+ jsonData := map [string ]interface {}{
35+ "query" :`
36+ {
37+ runtimes{
38+ edges{
39+ node {
40+ id
41+ namespace
42+ objectMeta {
43+ name
44+ description
45+ }
46+ }
47+ }
48+ }
49+ }
50+ ` ,
51+ }
52+
53+
54+ response , err := r .codefresh .requestAPI (& requestOptions {
55+ method : "POST" ,
56+ path : "/argo/api/graphql" ,
57+ body : jsonData ,
58+ })
59+ defer response .Body .Close ()
60+ if err != nil {
61+ fmt .Printf ("The HTTP request failed with error %s\n " , err )
62+ return nil , err
63+ }
64+ data , err := ioutil .ReadAll (response .Body )
65+ if err != nil {
66+ fmt .Printf ("failed to read from response body" )
67+ return nil , err
68+ }
69+ res := graphqlRuntimesResponse {}
70+ err = json .Unmarshal (data , & res )
71+ if err != nil {
72+ return nil , err
73+ }
74+ runtimes := []model.Runtime {}
75+ for _ , v := range res .Data .Runtimes .Edges {
76+ runtimes = append (runtimes , * v .Node )
77+ }
78+
79+ return runtimes , nil
80+
81+
82+ }
0 commit comments