From 31693b0f7b84b5743a112be11a255223ce2a5dc2 Mon Sep 17 00:00:00 2001 From: frrist Date: Fri, 26 Jan 2024 16:00:50 -0800 Subject: [PATCH] bug-ish: demonstrate issue with http payload --- cmd/cli/job/run.go | 8 +++++ pkg/publicapi/client/v2/client.go | 39 ++++++++++++++++++++++ pkg/publicapi/endpoint/orchestrator/job.go | 17 ++++++++++ 3 files changed, 64 insertions(+) diff --git a/cmd/cli/job/run.go b/cmd/cli/job/run.go index b0f7b3f439..6368345679 100644 --- a/cmd/cli/job/run.go +++ b/cmd/cli/job/run.go @@ -154,6 +154,14 @@ func (o *RunOptions) run(cmd *cobra.Command, args []string) { client := util.GetAPIClientV2() resp, err := client.Jobs().Put(ctx, &apimodels.PutJobRequest{ Job: j, + BasePutRequest: apimodels.BasePutRequest{ + BaseRequest: apimodels.BaseRequest{ + Namespace: "TheNamespace", + Headers: map[string]string{ + "foo": "bar", + }, + }, + }, }) if err != nil { util.Fatal(cmd, fmt.Errorf("failed request: %w", err), 1) diff --git a/pkg/publicapi/client/v2/client.go b/pkg/publicapi/client/v2/client.go index 3519032d15..e4eec25b07 100644 --- a/pkg/publicapi/client/v2/client.go +++ b/pkg/publicapi/client/v2/client.go @@ -1,9 +1,13 @@ package client import ( + "bytes" "context" + "fmt" + "io/ioutil" "net/http" "net/url" + "strings" "time" "github.com/bacalhau-project/bacalhau/pkg/publicapi/apimodels" @@ -107,6 +111,11 @@ func (c *Client) doRequest(ctx context.Context, method, endpoint string, r *apim } start := time.Now() + s, err := AsCurlCommand(req) + if err != nil { + panic(err) + } + fmt.Println(s) resp, err := c.httpClient.Do(req) diff := time.Since(start) @@ -200,3 +209,33 @@ func (c *Client) url(endpoint string) (*url.URL, error) { RawPath: u.RawPath, }, nil } + +func AsCurlCommand(req *http.Request) (string, error) { + var command strings.Builder + + // Start the curl command with the method and URL + command.WriteString(fmt.Sprintf("curl -X %s '%s'", req.Method, req.URL)) + + // Add headers + for name, values := range req.Header { + for _, value := range values { + command.WriteString(fmt.Sprintf(" -H '%s: %s'", name, value)) + } + } + + // Add body if present + if req.Body != nil && req.ContentLength != 0 { + bodyBytes, err := ioutil.ReadAll(req.Body) + // Important: Restore the body so it can be used again + req.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) + + if err != nil { + return "", err + } + + // Use --data-binary to preserve the exact original body contents + command.WriteString(fmt.Sprintf(" --data-binary '%s'", string(bodyBytes))) + } + + return command.String(), nil +} diff --git a/pkg/publicapi/endpoint/orchestrator/job.go b/pkg/publicapi/endpoint/orchestrator/job.go index da01bf70ca..d3fd71ffe7 100644 --- a/pkg/publicapi/endpoint/orchestrator/job.go +++ b/pkg/publicapi/endpoint/orchestrator/job.go @@ -5,6 +5,7 @@ import ( "strconv" "github.com/labstack/echo/v4" + "github.com/rs/zerolog/log" "golang.org/x/exp/slices" "k8s.io/apimachinery/pkg/labels" @@ -37,6 +38,22 @@ func (e *Endpoint) putJob(c echo.Context) error { if err := c.Validate(&args); err != nil { return err } + if len(args.Headers) == 0 { + log.Info().Msg("HTTP Payload did not contain header") + } else { + log.Info().Msgf("HTTP Payload contains header values header: %s", args.Headers) + } + if args.Namespace != "" { + log.Info().Msgf("HTTP Payload contains namespace: %s", args.Namespace) + } else { + log.Info().Msg("HTTP Payload did not contain namespace") + } + if len(c.Request().Header) == 0 { + log.Info().Msg("HTTP Request did not contain header") + } else { + log.Info().Msgf("HTTP Request contains headers: %s", c.Request().Header) + } + resp, err := e.orchestrator.SubmitJob(ctx, &orchestrator.SubmitJobRequest{ Job: args.Job, })