Skip to content

Commit

Permalink
feat(internal): fallback to json serialization if no serialization me…
Browse files Browse the repository at this point in the history
…thods are defined
  • Loading branch information
stainless-bot committed Dec 14, 2023
1 parent 9525bc2 commit 0cb56cd
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions internal/requestconfig/requestconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,40 @@ func getPlatformProperties() map[string]string {

func NewRequestConfig(ctx context.Context, method string, u string, body interface{}, dst interface{}, opts ...func(*RequestConfig) error) (*RequestConfig, error) {
var b []byte

contentType := "application/json"
hasSerializationFunc := false
if body, ok := body.(json.Marshaler); ok {
var err error
b, err = body.MarshalJSON()
if err != nil {
return nil, err
}
hasSerializationFunc = true
}
if body, ok := body.(apiform.Marshaler); ok {
var err error
b, contentType, err = body.MarshalMultipart()
if err != nil {
return nil, err
}
hasSerializationFunc = true
}
if body, ok := body.(apiquery.Queryer); ok {
u = u + "?" + body.URLQuery().Encode()
hasSerializationFunc = true
}

// Fallback to json serialization if none of the serialization functions that we expect
// to see is present.
if body != nil && !hasSerializationFunc {
var err error
b, err = json.Marshal(body)
if err != nil {
return nil, err
}
}

req, err := http.NewRequestWithContext(ctx, method, u, nil)
if err != nil {
return nil, err
Expand Down

0 comments on commit 0cb56cd

Please sign in to comment.