Skip to content

Commit

Permalink
improve request body handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Apr 3, 2024
1 parent 479fe65 commit 500893f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
toolchain go1.22.0

require (
github.com/hasura/ndc-rest-schema v0.0.0-20240403025915-a07cf4007901
github.com/hasura/ndc-rest-schema v0.0.0-20240403043119-75fbf0984119
github.com/hasura/ndc-sdk-go v1.0.1-0.20240331071727-9c3db01b3219
github.com/lmittmann/tint v1.0.4
github.com/stretchr/testify v1.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ github.com/hasura/ndc-rest-schema v0.0.0-20240402180009-226aaac6dc39 h1:XViMqRUe
github.com/hasura/ndc-rest-schema v0.0.0-20240402180009-226aaac6dc39/go.mod h1:jcUMvsv1DjSONUk713W+jDNRTE0UQDVqHg6bRHk+Dcc=
github.com/hasura/ndc-rest-schema v0.0.0-20240403025915-a07cf4007901 h1:wr48uZbZTVCdiZUkR7hb9FswzO1XPV+HgkVI7bvNwM0=
github.com/hasura/ndc-rest-schema v0.0.0-20240403025915-a07cf4007901/go.mod h1:jcUMvsv1DjSONUk713W+jDNRTE0UQDVqHg6bRHk+Dcc=
github.com/hasura/ndc-rest-schema v0.0.0-20240403043119-75fbf0984119 h1:Cl66wyJt257VnIc/86qm1ZOWlDFiG1Q1ztqCxuVKoIc=
github.com/hasura/ndc-rest-schema v0.0.0-20240403043119-75fbf0984119/go.mod h1:jcUMvsv1DjSONUk713W+jDNRTE0UQDVqHg6bRHk+Dcc=
github.com/hasura/ndc-sdk-go v1.0.0 h1:vLbv1k1UIkIXUUrNzi3B4QvAqT1/q2xxlrzff8+Z6+E=
github.com/hasura/ndc-sdk-go v1.0.0/go.mod h1:EeM3hKbhCfBjDDva8mP4D2KeptTqAaxNqNw8rFQAnMs=
github.com/hasura/ndc-sdk-go v1.0.1-0.20240331071727-9c3db01b3219 h1:1HniD4n41sKrKaQIBGO71tivLM3m435FCIJNUKJTX2k=
Expand Down
36 changes: 23 additions & 13 deletions rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
Expand Down Expand Up @@ -76,22 +77,30 @@ func (client *httpClient) Send(ctx context.Context, rawRequest *rest.Request, he
}

func createRequest(ctx context.Context, rawRequest *rest.Request, headers http.Header, data any) (*http.Request, error) {
contentType := headers.Get(contentTypeHeader)
if contentType == "" {
contentType = contentTypeJSON
}

var body io.Reader
if data != nil {
switch contentType {
case contentTypeJSON:
bodyBytes, err := json.Marshal(data)
if err != nil {
return nil, err
contentType := contentTypeJSON
if rawRequest.RequestBody != nil {
contentType = rawRequest.RequestBody.ContentType
// TODO: validate data with request body
if data != nil {
if strings.HasPrefix(contentType, "text/") {
body = bytes.NewBuffer([]byte(fmt.Sprintln(data)))
} else {
switch contentType {
case rest.ContentTypeFormURLEncoded:
case rest.ContentTypeJSON:
bodyBytes, err := json.Marshal(data)
if err != nil {
return nil, err
}
body = bytes.NewBuffer(bodyBytes)
default:
return nil, fmt.Errorf("unsupported content type %s", contentType)
}
}
body = bytes.NewBuffer(bodyBytes)
default:
return nil, fmt.Errorf("unsupported content type %s", contentType)
} else if rawRequest.RequestBody.Required {
return nil, errors.New("request body is required")
}
}

Expand All @@ -102,6 +111,7 @@ func createRequest(ctx context.Context, rawRequest *rest.Request, headers http.H
for key, header := range headers {
request.Header[key] = header
}
request.Header.Set(rest.ContentTypeHeader, contentType)

return request, nil
}
Expand Down
1 change: 0 additions & 1 deletion rest/parameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,4 @@ func TestEncodeParameterValues(t *testing.T) {
}
})
}

}

0 comments on commit 500893f

Please sign in to comment.