Skip to content

Commit

Permalink
Add support for query parameters in multi-part form upload (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
amckinney authored Jan 10, 2024
1 parent 4c0d13d commit 80944cd
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ install:
.PHONY: test
test: install
go test ./...
npm install -g @fern-api/seed-cli@0.15.11-5-gc8d919536
npm install -g @fern-api/seed-cli@0.16.25-2-g32eebe2b
seed test --workspace sdk --fixture enum-query-params
seed test --workspace sdk --fixture response-property
seed test --workspace sdk --fixture file-upload
Expand Down
5 changes: 1 addition & 4 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,10 +893,7 @@ func shouldSkipRequestType(irEndpoint *fernir.HttpEndpoint) bool {
// This endpoint doesn't have any in-lined request types that need to be generated.
return true
}
if irEndpoint.RequestBody != nil && irEndpoint.RequestBody.FileUpload != nil {
return !fileUploadHasBodyProperties(irEndpoint.RequestBody.FileUpload)
}
return false
return !needsRequestParameter(irEndpoint)
}

// fileUploadHasBodyProperties returns true if the file upload request has at least
Expand Down
3 changes: 3 additions & 0 deletions internal/generator/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -1670,6 +1670,9 @@ func needsRequestParameter(endpoint *ir.HttpEndpoint) bool {
if endpoint.SdkRequest == nil {
return false
}
if len(endpoint.QueryParameters) > 0 {
return true
}
if endpoint.RequestBody != nil {
return endpoint.RequestBody.FileUpload == nil || fileUploadHasBodyProperties(endpoint.RequestBody.FileUpload)
}
Expand Down
8 changes: 8 additions & 0 deletions seed/sdk/file-upload/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import (
core "github.com/file-upload/fern/core"
)

type JustFileWithQueryParamsRequet struct {
MaybeString *string `json:"-"`
Integer int `json:"-"`
MaybeInteger *int `json:"-"`
ListOfStrings []string `json:"-"`
OptionalListOfStrings []*string `json:"-"`
}

type MyRequest struct {
MaybeString *string `json:"maybeString,omitempty"`
Integer int `json:"integer"`
Expand Down
58 changes: 58 additions & 0 deletions seed/sdk/file-upload/service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
io "io"
multipart "mime/multipart"
http "net/http"
url "net/url"
)

type Client struct {
Expand Down Expand Up @@ -169,3 +170,60 @@ func (c *Client) JustFile(ctx context.Context, file io.Reader) error {
}
return nil
}

func (c *Client) JustFileWithQueryParams(ctx context.Context, file io.Reader, request *fern.JustFileWithQueryParamsRequet) error {
baseURL := ""
if c.baseURL != "" {
baseURL = c.baseURL
}
endpointURL := baseURL + "/" + "just-file-with-query-params"

queryParams := make(url.Values)
if request.MaybeString != nil {
queryParams.Add("maybeString", fmt.Sprintf("%v", *request.MaybeString))
}
queryParams.Add("integer", fmt.Sprintf("%v", request.Integer))
if request.MaybeInteger != nil {
queryParams.Add("maybeInteger", fmt.Sprintf("%v", *request.MaybeInteger))
}
for _, value := range request.ListOfStrings {
queryParams.Add("listOfStrings", fmt.Sprintf("%v", value))
}
for _, value := range request.OptionalListOfStrings {
queryParams.Add("optionalListOfStrings", fmt.Sprintf("%v", *value))
}
if len(queryParams) > 0 {
endpointURL += "?" + queryParams.Encode()
}

requestBuffer := bytes.NewBuffer(nil)
writer := multipart.NewWriter(requestBuffer)
fileFilename := "file_filename"
if named, ok := file.(interface{ Name() string }); ok {
fileFilename = named.Name()
}
filePart, err := writer.CreateFormFile("file", fileFilename)
if err != nil {
return err
}
if _, err := io.Copy(filePart, file); err != nil {
return err
}
if err := writer.Close(); err != nil {
return err
}
c.header.Set("Content-Type", writer.FormDataContentType())

if err := c.caller.Call(
ctx,
&core.CallParams{
URL: endpointURL,
Method: http.MethodPost,
Headers: c.header,
Request: requestBuffer,
},
); err != nil {
return err
}
return nil
}

0 comments on commit 80944cd

Please sign in to comment.