forked from windmill-labs/windmill-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
146 lines (135 loc) · 3.73 KB
/
client.go
File metadata and controls
146 lines (135 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package windmill
import (
"context"
"errors"
"fmt"
"math"
"math/rand"
"net/http"
"os"
"strings"
"github.com/google/uuid"
api "github.com/windmill-labs/windmill-go-client/api"
)
type ClientWithWorkspace struct {
Client *api.ClientWithResponses
Workspace string
}
func NewClient(baseUrl, token, workspace string) (ClientWithWorkspace, error) {
if !strings.HasSuffix(baseUrl, "/") {
baseUrl += "/"
}
baseUrl += "api/"
client, err := api.NewClientWithResponses(baseUrl, func(c *api.Client) error {
c.RequestEditors = append(c.RequestEditors, func(ctx context.Context, req *http.Request) error {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
return nil
})
return nil
})
return ClientWithWorkspace{
Client: client,
Workspace: workspace,
}, err
}
func (c ClientWithWorkspace) GetVariable(path string) (string, error) {
res, err := c.Client.GetVariableValueWithResponse(context.Background(), c.Workspace, path)
if err != nil {
return "", err
}
if res.StatusCode()/100 != 2 {
return "", errors.New(string(res.Body))
}
return *res.JSON200, nil
}
func (c ClientWithWorkspace) GetResource(path string) (interface{}, error) {
params := api.GetResourceValueInterpolatedParams{}
res, err := c.Client.GetResourceValueInterpolatedWithResponse(context.Background(), c.Workspace, path, ¶ms)
if err != nil {
return nil, err
}
if res.StatusCode()/100 != 2 {
return nil, errors.New(string(res.Body))
}
return *res.JSON200, nil
}
func (c ClientWithWorkspace) SetResource(path string, value interface{}, resourceTypeOpt ...string) error {
params := api.GetResourceValueInterpolatedParams{}
getRes, getErr := c.Client.GetResourceValueInterpolatedWithResponse(context.Background(), c.Workspace, path, ¶ms)
if getErr != nil {
return getErr
}
if getRes.StatusCode() == 404 {
resourceType := "any"
if len(resourceTypeOpt) > 0 {
resourceType = resourceTypeOpt[0]
}
res, err := c.Client.CreateResourceWithResponse(context.Background(), c.Workspace, &api.CreateResourceParams{
UpdateIfExists: newBool(true),
}, api.CreateResource{Value: &value, Path: path, ResourceType: resourceType})
if err != nil {
return err
}
if res.StatusCode()/100 != 2 {
return errors.New(string(res.Body))
}
} else {
res, err := c.Client.UpdateResourceValueWithResponse(context.Background(), c.Workspace, path, api.UpdateResourceValueJSONRequestBody{
Value: &value,
})
if err != nil {
return err
}
if res.StatusCode()/100 != 2 {
return errors.New(string(res.Body))
}
}
return nil
}
func (c ClientWithWorkspace) SetVariable(path string, value string) error {
f := false
res, err := c.Client.UpdateVariableWithResponse(context.Background(), c.Workspace, path, &api.UpdateVariableParams{AlreadyEncrypted: &f}, api.EditVariable{Value: &value})
if err != nil {
f = true
}
if res.StatusCode()/100 != 2 {
f = true
}
if f {
res, err := c.Client.CreateVariableWithResponse(context.Background(), c.Workspace, &api.CreateVariableParams{},
api.CreateVariableJSONRequestBody{
Path: path,
Value: value,
})
if err != nil {
return err
}
if res.StatusCode()/100 != 2 {
return errors.New(string(res.Body))
}
}
return nil
}
func (c ClientWithWorkspace) GetResumeUrls(approver string) (ResumeUrls, error) {
var urls ResumeUrls
jobId, err := uuid.Parse(os.Getenv("WM_JOB_ID"))
if err != nil {
return urls, err
}
params := api.GetResumeUrlsParams{Approver: &approver}
nonce := rand.Intn(int(math.MaxUint32))
res, err := c.Client.GetResumeUrlsWithResponse(context.Background(),
c.Workspace,
jobId,
nonce,
¶ms,
)
if err != nil {
return urls, err
}
if res.StatusCode()/100 != 2 {
return urls, errors.New(string(res.Body))
}
urls = *res.JSON200
return urls, nil
}