Skip to content

Commit

Permalink
bug-ish: demonstrate issue with http payload
Browse files Browse the repository at this point in the history
  • Loading branch information
frrist committed Jan 27, 2024
1 parent c5c8dae commit 31693b0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cmd/cli/job/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 39 additions & 0 deletions pkg/publicapi/client/v2/client.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
17 changes: 17 additions & 0 deletions pkg/publicapi/endpoint/orchestrator/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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,
})
Expand Down

0 comments on commit 31693b0

Please sign in to comment.