Skip to content

Commit 2822647

Browse files
authored
Merge pull request #1 from braze-inc/use-client-struct
Add client struct and use to call api methods
2 parents a2f1573 + 92d2222 commit 2822647

File tree

2 files changed

+147
-117
lines changed

2 files changed

+147
-117
lines changed

client.go

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

windmill.go

Lines changed: 7 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,15 @@
11
package windmill
22

33
import (
4-
"context"
5-
"errors"
6-
"fmt"
7-
"math"
8-
"math/rand"
9-
"net/http"
104
"os"
11-
"github.com/google/uuid"
12-
api "github.com/windmill-labs/windmill-go-client/api"
135
)
146

15-
type ClientWithWorkspace struct {
16-
Client *api.ClientWithResponses
17-
Workspace string
18-
}
19-
207
func GetClient() (ClientWithWorkspace, error) {
218
base_url := os.Getenv("BASE_INTERNAL_URL") + "/api"
229
workspace := os.Getenv("WM_WORKSPACE")
2310
token := os.Getenv("WM_TOKEN")
2411

25-
client, _ := api.NewClientWithResponses(base_url, func(c *api.Client) error {
26-
c.RequestEditors = append(c.RequestEditors, func(ctx context.Context, req *http.Request) error {
27-
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
28-
return nil
29-
})
30-
return nil
31-
})
32-
return ClientWithWorkspace{
33-
Client: client,
34-
Workspace: workspace,
35-
}, nil
12+
return NewClient(base_url, token, workspace)
3613
}
3714
func newBool(b bool) *bool {
3815
return &b
@@ -43,98 +20,31 @@ func GetVariable(path string) (string, error) {
4320
if err != nil {
4421
return "", err
4522
}
46-
res, err := client.Client.GetVariableValueWithResponse(context.Background(), client.Workspace, path)
47-
if err != nil {
48-
return "", err
49-
}
50-
if res.StatusCode()/100 != 2 {
51-
return "", errors.New(string(res.Body))
52-
}
53-
return *res.JSON200, nil
23+
return client.GetVariable(path)
5424
}
5525

5626
func GetResource(path string) (interface{}, error) {
5727
client, err := GetClient()
5828
if err != nil {
5929
return nil, err
6030
}
61-
params := api.GetResourceValueInterpolatedParams{}
62-
res, err := client.Client.GetResourceValueInterpolatedWithResponse(context.Background(), client.Workspace, path, &params)
63-
if err != nil {
64-
return nil, err
65-
}
66-
if res.StatusCode()/100 != 2 {
67-
return nil, errors.New(string(res.Body))
68-
}
69-
return *res.JSON200, nil
31+
return client.GetResource(path)
7032
}
7133

7234
func SetResource(path string, value interface{}, resourceTypeOpt ...string) error {
7335
client, err := GetClient()
7436
if err != nil {
7537
return err
7638
}
77-
params := api.GetResourceValueInterpolatedParams{}
78-
getRes, getErr := client.Client.GetResourceValueInterpolatedWithResponse(context.Background(), client.Workspace, path, &params)
79-
if getErr != nil {
80-
return getErr
81-
}
82-
if getRes.StatusCode() == 404 {
83-
resourceType := "any"
84-
if len(resourceTypeOpt) > 0 {
85-
resourceType = resourceTypeOpt[0]
86-
}
87-
res, err := client.Client.CreateResourceWithResponse(context.Background(), client.Workspace, &api.CreateResourceParams{
88-
UpdateIfExists: newBool(true),
89-
}, api.CreateResource{Value: &value, Path: path, ResourceType: resourceType})
90-
if err != nil {
91-
return err
92-
}
93-
if res.StatusCode()/100 != 2 {
94-
return errors.New(string(res.Body))
95-
}
96-
} else {
97-
res, err := client.Client.UpdateResourceValueWithResponse(context.Background(), client.Workspace, path, api.UpdateResourceValueJSONRequestBody{
98-
Value: &value,
99-
})
100-
if err != nil {
101-
return err
102-
}
103-
if res.StatusCode()/100 != 2 {
104-
return errors.New(string(res.Body))
105-
}
106-
}
107-
return nil
39+
return client.SetResource(path, value, resourceTypeOpt...)
10840
}
10941

11042
func SetVariable(path string, value string) error {
11143
client, err := GetClient()
11244
if err != nil {
11345
return err
11446
}
115-
f := false
116-
res, err := client.Client.UpdateVariableWithResponse(context.Background(), client.Workspace, path, &api.UpdateVariableParams{AlreadyEncrypted: &f}, api.EditVariable{Value: &value})
117-
if err != nil {
118-
f = true
119-
}
120-
if res.StatusCode()/100 != 2 {
121-
f = true
122-
}
123-
if f {
124-
res, err := client.Client.CreateVariableWithResponse(context.Background(), client.Workspace, &api.CreateVariableParams{},
125-
api.CreateVariableJSONRequestBody{
126-
Path: path,
127-
Value: value,
128-
})
129-
130-
if err != nil {
131-
return err
132-
}
133-
if res.StatusCode()/100 != 2 {
134-
return errors.New(string(res.Body))
135-
}
136-
}
137-
return nil
47+
return client.SetVariable(path, value)
13848
}
13949

14050
func GetStatePath() string {
@@ -164,29 +74,9 @@ type ResumeUrls struct {
16474
}
16575

16676
func GetResumeUrls(approver string) (ResumeUrls, error) {
167-
var urls ResumeUrls
16877
client, err := GetClient()
16978
if err != nil {
170-
return urls, err
171-
}
172-
jobId, err := uuid.Parse(os.Getenv("WM_JOB_ID"))
173-
if err != nil {
174-
return urls, err
175-
}
176-
params := api.GetResumeUrlsParams{Approver: &approver}
177-
nonce := rand.Intn(int(math.MaxUint32))
178-
res, err := client.Client.GetResumeUrlsWithResponse(context.Background(),
179-
client.Workspace,
180-
jobId,
181-
nonce,
182-
&params,
183-
)
184-
if err != nil {
185-
return urls, err
186-
}
187-
if res.StatusCode()/100 != 2 {
188-
return urls, errors.New(string(res.Body))
79+
return ResumeUrls{}, err
18980
}
190-
urls = *res.JSON200
191-
return urls, nil
81+
return client.GetResumeUrls(approver)
19282
}

0 commit comments

Comments
 (0)