Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions extensions/cel/libs/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,56 @@ func (r *contextImpl) Post(url string, data any, headers map[string]string) (any
return r.executeRequest(req)
}

func (r *contextImpl) Put(url string, data any, headers map[string]string) (any, error) {
if err := r.validateURL(url); err != nil {
return nil, err
}
body, err := buildRequestData(data)
if err != nil {
return nil, fmt.Errorf("failed to encode request data: %w", err)
}
Comment on lines +322 to +325

Copilot AI Apr 25, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put/Patch now call buildRequestData(), but buildRequestData currently hard-codes "HTTP POST" in its encode error message. This will produce confusing errors for PUT/PATCH requests; consider making the error message method-agnostic (or pass the HTTP verb into buildRequestData and include it in the message).

Copilot uses AI. Check for mistakes.
req, err := http.NewRequestWithContext(context.TODO(), "PUT", url, body)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
for h, v := range headers {
req.Header.Add(h, v)
}
return r.executeRequest(req)
}

func (r *contextImpl) Patch(url string, data any, headers map[string]string) (any, error) {
if err := r.validateURL(url); err != nil {
return nil, err
}
body, err := buildRequestData(data)
if err != nil {
return nil, fmt.Errorf("failed to encode request data: %w", err)
}
req, err := http.NewRequestWithContext(context.TODO(), "PATCH", url, body)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
for h, v := range headers {
req.Header.Add(h, v)
}
return r.executeRequest(req)
}

func (r *contextImpl) Delete(url string, headers map[string]string) (any, error) {
if err := r.validateURL(url); err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(context.TODO(), "DELETE", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
for h, v := range headers {
req.Header.Add(h, v)
}
return r.executeRequest(req)
}

func (r *contextImpl) executeRequest(req *http.Request) (any, error) {
resp, err := r.client.Do(req)
if err != nil {
Expand Down
85 changes: 85 additions & 0 deletions extensions/cel/libs/http/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,91 @@ func (c *impl) post_request_with_headers_string(args ...ref.Val) ref.Val {
return c.post_request_string_with_client(args...)
}

func (c *impl) put_request_string_with_client(args ...ref.Val) ref.Val {
if len(args) != 4 {
return types.NewErr("expected 4 arguments, got %d", len(args))
}
if request, err := utils.GetArg[Context](args, 0); err != nil {
return err
} else if url, err := utils.GetArg[string](args, 1); err != nil {
return err
} else if data, err := utils.GetArg[*structpb.Value](args, 2); err != nil {
return err
} else if header, err := utils.GetArg[map[string]string](args, 3); err != nil {
return err
} else {
data, err := request.Put(url, data, header)
if err != nil {
return types.NewErr("request failed: %v", err)
}
return c.NativeToValue(data)
}
}

func (c *impl) put_request_string(args ...ref.Val) ref.Val {
return c.put_request_string_with_client(args[0], args[1], args[2], c.NativeToValue(make(map[string]string, 0)))
}

func (c *impl) put_request_with_headers_string(args ...ref.Val) ref.Val {
return c.put_request_string_with_client(args...)
}

func (c *impl) patch_request_string_with_client(args ...ref.Val) ref.Val {
if len(args) != 4 {
return types.NewErr("expected 4 arguments, got %d", len(args))
}
if request, err := utils.GetArg[Context](args, 0); err != nil {
return err
} else if url, err := utils.GetArg[string](args, 1); err != nil {
return err
} else if data, err := utils.GetArg[*structpb.Value](args, 2); err != nil {
return err
} else if header, err := utils.GetArg[map[string]string](args, 3); err != nil {
return err
} else {
data, err := request.Patch(url, data, header)
if err != nil {
return types.NewErr("request failed: %v", err)
}
return c.NativeToValue(data)
}
}

func (c *impl) patch_request_string(args ...ref.Val) ref.Val {
return c.patch_request_string_with_client(args[0], args[1], args[2], c.NativeToValue(make(map[string]string, 0)))
}

func (c *impl) patch_request_with_headers_string(args ...ref.Val) ref.Val {
return c.patch_request_string_with_client(args...)
}

func (c *impl) delete_request_with_client_string(args ...ref.Val) ref.Val {
if len(args) != 3 {
return types.NewErr("expected 3 arguments, got %d", len(args))
}
if request, err := utils.GetArg[Context](args, 0); err != nil {
return err
} else if url, err := utils.GetArg[string](args, 1); err != nil {
return err
} else if header, err := utils.GetArg[map[string]string](args, 2); err != nil {
return err
} else {
data, err := request.Delete(url, header)
if err != nil {
return types.NewErr("request failed: %v", err)
}
return c.NativeToValue(data)
}
}

func (c *impl) delete_request_string(request, url ref.Val) ref.Val {
return c.delete_request_with_client_string(request, url, c.NativeToValue(make(map[string]string, 0)))
}

func (c *impl) delete_request_with_headers_string(args ...ref.Val) ref.Val {
return c.delete_request_with_client_string(args...)
}

func (c *impl) http_client_string(request, caBundle ref.Val) ref.Val {
if request, err := utils.ConvertToNative[Context](request); err != nil {
return types.NewErr("invalid arg %d: %v", 0, err)
Expand Down
Loading
Loading