From 92d2222c37f419c5b64d53e3a81f2a7a9c3f92d2 Mon Sep 17 00:00:00 2001 From: Ada Brooker Date: Fri, 27 Jun 2025 10:19:45 -0400 Subject: [PATCH 1/2] Add client struct and use to call api methods --- client.go | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++ windmill.go | 124 +++------------------------------------------- 2 files changed, 147 insertions(+), 117 deletions(-) create mode 100644 client.go diff --git a/client.go b/client.go new file mode 100644 index 0000000..1cd0dc6 --- /dev/null +++ b/client.go @@ -0,0 +1,140 @@ +package windmill + +import ( + "context" + "errors" + "fmt" + "math" + "math/rand" + "net/http" + "os" + + "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) { + 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 +} diff --git a/windmill.go b/windmill.go index f8d46bf..726103a 100644 --- a/windmill.go +++ b/windmill.go @@ -1,38 +1,15 @@ package windmill import ( - "context" - "errors" - "fmt" - "math" - "math/rand" - "net/http" "os" - "github.com/google/uuid" - api "github.com/windmill-labs/windmill-go-client/api" ) -type ClientWithWorkspace struct { - Client *api.ClientWithResponses - Workspace string -} - func GetClient() (ClientWithWorkspace, error) { base_url := os.Getenv("BASE_INTERNAL_URL") + "/api" workspace := os.Getenv("WM_WORKSPACE") token := os.Getenv("WM_TOKEN") - client, _ := api.NewClientWithResponses(base_url, 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, - }, nil + return NewClient(base_url, token, workspace) } func newBool(b bool) *bool { return &b @@ -43,14 +20,7 @@ func GetVariable(path string) (string, error) { if err != nil { return "", err } - res, err := client.Client.GetVariableValueWithResponse(context.Background(), client.Workspace, path) - if err != nil { - return "", err - } - if res.StatusCode()/100 != 2 { - return "", errors.New(string(res.Body)) - } - return *res.JSON200, nil + return client.GetVariable(path) } func GetResource(path string) (interface{}, error) { @@ -58,15 +28,7 @@ func GetResource(path string) (interface{}, error) { if err != nil { return nil, err } - params := api.GetResourceValueInterpolatedParams{} - res, err := client.Client.GetResourceValueInterpolatedWithResponse(context.Background(), client.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 + return client.GetResource(path) } func SetResource(path string, value interface{}, resourceTypeOpt ...string) error { @@ -74,37 +36,7 @@ func SetResource(path string, value interface{}, resourceTypeOpt ...string) erro if err != nil { return err } - params := api.GetResourceValueInterpolatedParams{} - getRes, getErr := client.Client.GetResourceValueInterpolatedWithResponse(context.Background(), client.Workspace, path, ¶ms) - if getErr != nil { - return getErr - } - if getRes.StatusCode() == 404 { - resourceType := "any" - if len(resourceTypeOpt) > 0 { - resourceType = resourceTypeOpt[0] - } - res, err := client.Client.CreateResourceWithResponse(context.Background(), client.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 := client.Client.UpdateResourceValueWithResponse(context.Background(), client.Workspace, path, api.UpdateResourceValueJSONRequestBody{ - Value: &value, - }) - if err != nil { - return err - } - if res.StatusCode()/100 != 2 { - return errors.New(string(res.Body)) - } - } - return nil + return client.SetResource(path, value, resourceTypeOpt...) } func SetVariable(path string, value string) error { @@ -112,29 +44,7 @@ func SetVariable(path string, value string) error { if err != nil { return err } - f := false - res, err := client.Client.UpdateVariableWithResponse(context.Background(), client.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 := client.Client.CreateVariableWithResponse(context.Background(), client.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 + return client.SetVariable(path, value) } func GetStatePath() string { @@ -164,29 +74,9 @@ type ResumeUrls struct { } func GetResumeUrls(approver string) (ResumeUrls, error) { - var urls ResumeUrls client, err := GetClient() if err != nil { - return urls, err - } - 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 := client.Client.GetResumeUrlsWithResponse(context.Background(), - client.Workspace, - jobId, - nonce, - ¶ms, - ) - if err != nil { - return urls, err - } - if res.StatusCode()/100 != 2 { - return urls, errors.New(string(res.Body)) + return ResumeUrls{}, err } - urls = *res.JSON200 - return urls, nil + return client.GetResumeUrls(approver) } From 79fac180570fc362c2a69779d03d6cdf2436fe6d Mon Sep 17 00:00:00 2001 From: Ada Brooker Date: Mon, 30 Jun 2025 09:44:05 -0400 Subject: [PATCH 2/2] Add api to baseurl --- client.go | 6 ++++++ windmill.go | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 1cd0dc6..004d63d 100644 --- a/client.go +++ b/client.go @@ -8,6 +8,7 @@ import ( "math/rand" "net/http" "os" + "strings" "github.com/google/uuid" api "github.com/windmill-labs/windmill-go-client/api" @@ -19,6 +20,11 @@ type ClientWithWorkspace struct { } 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)) diff --git a/windmill.go b/windmill.go index 726103a..de8b247 100644 --- a/windmill.go +++ b/windmill.go @@ -5,7 +5,7 @@ import ( ) func GetClient() (ClientWithWorkspace, error) { - base_url := os.Getenv("BASE_INTERNAL_URL") + "/api" + base_url := os.Getenv("BASE_INTERNAL_URL") workspace := os.Getenv("WM_WORKSPACE") token := os.Getenv("WM_TOKEN")