Skip to content

Commit

Permalink
Add batch SetMultipartFields in order to set multiple fields (go-res…
Browse files Browse the repository at this point in the history
…ty#211)

* Add batch SetMultipartFields in order to set multiple fields
* Improve test and update example with comment
* change SetMultipartFields to variadic
* fix usage example comment for SetMultipartFields
* Remove empty new lines for code consistency
  • Loading branch information
e11ni authored and jeevatkm committed Jan 10, 2019
1 parent e6f89f7 commit c7aef45
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 12 deletions.
6 changes: 3 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func (c *Client) R() *Request {

client: c,
multipartFiles: []*File{},
multipartFields: []*multipartField{},
multipartFields: []*MultipartField{},
pathParams: map[string]string{},
jsonEscapeHTML: true,
}
Expand Down Expand Up @@ -917,8 +917,8 @@ func (f *File) String() string {
return fmt.Sprintf("ParamName: %v; FileName: %v", f.ParamName, f.Name)
}

// multipartField represent custom data part for multipart request
type multipartField struct {
// MultipartField represent custom data part for multipart request
type MultipartField struct {
Param string
FileName string
ContentType string
Expand Down
30 changes: 24 additions & 6 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ func (r *Request) SetError(err interface{}) *Request {
func (r *Request) SetFile(param, filePath string) *Request {
r.isMultiPart = true
r.FormData.Set("@"+param, filePath)

return r
}

Expand Down Expand Up @@ -273,27 +272,47 @@ func (r *Request) SetFiles(files map[string]string) *Request {
//
func (r *Request) SetFileReader(param, fileName string, reader io.Reader) *Request {
r.isMultiPart = true

r.multipartFiles = append(r.multipartFiles, &File{
Name: fileName,
ParamName: param,
Reader: reader,
})

return r
}

// SetMultipartField method is to set custom data using io.Reader for multipart upload.
func (r *Request) SetMultipartField(param, fileName, contentType string, reader io.Reader) *Request {
r.isMultiPart = true

r.multipartFields = append(r.multipartFields, &multipartField{
r.multipartFields = append(r.multipartFields, &MultipartField{
Param: param,
FileName: fileName,
ContentType: contentType,
Reader: reader,
})
return r
}

// SetMultipartFields method is to set multiple data fields using io.Reader for multipart upload.
// Example:
// resty.R().SetMultipartFields(
// &resty.MultipartField{
// Param: "uploadManifest1",
// FileName: "upload-file-1.json",
// ContentType: "application/json",
// Reader: strings.NewReader(`{"input": {"name": "Uploaded document 1", "_filename" : ["file1.txt"]}}`),
// },
// &resty.MultipartField{
// Param: "uploadManifest2",
// FileName: "upload-file-2.json",
// ContentType: "application/json",
// Reader: strings.NewReader(`{"input": {"name": "Uploaded document 2", "_filename" : ["file2.txt"]}}`),
// })
//
// If you have slice already, then simply call-
// resty.R().SetMultipartFields(fields...)
func (r *Request) SetMultipartFields(fields ...*MultipartField) *Request {
r.isMultiPart = true
r.multipartFields = append(r.multipartFields, fields...)
return r
}

Expand All @@ -304,7 +323,6 @@ func (r *Request) SetMultipartField(param, fileName, contentType string, reader
//
func (r *Request) SetContentLength(l bool) *Request {
r.setContentLength = true

return r
}

Expand Down
2 changes: 1 addition & 1 deletion request16.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Request struct {
client *Client
bodyBuf *bytes.Buffer
multipartFiles []*File
multipartFields []*multipartField
multipartFields []*MultipartField
}

func (r *Request) addContextIfAvailable() {
Expand Down
2 changes: 1 addition & 1 deletion request17.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type Request struct {
client *Client
bodyBuf *bytes.Buffer
multipartFiles []*File
multipartFields []*multipartField
multipartFields []*MultipartField
}

// Context method returns the Context if its already set in request
Expand Down
36 changes: 36 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,42 @@ func TestMultiPartMultipartField(t *testing.T) {
assertEqual(t, true, strings.Contains(responseStr, "upload-file.json"))
}

func TestMultiPartMultipartFields(t *testing.T) {
ts := createFormPostServer(t)
defer ts.Close()
defer cleanupFiles(".testdata/upload")

jsonStr1 := `{"input": {"name": "Uploaded document 1", "_filename" : ["file1.txt"]}}`
jsonStr2 := `{"input": {"name": "Uploaded document 2", "_filename" : ["file2.txt"]}}`

fields := []*MultipartField{
&MultipartField{
Param: "uploadManifest1",
FileName: "upload-file-1.json",
ContentType: "application/json",
Reader: strings.NewReader(jsonStr1),
},
&MultipartField{
Param: "uploadManifest2",
FileName: "upload-file-2.json",
ContentType: "application/json",
Reader: strings.NewReader(jsonStr2),
},
}

resp, err := dclr().
SetFormData(map[string]string{"first_name": "Jeevanandam", "last_name": "M"}).
SetMultipartFields(fields...).
Post(ts.URL + "/upload")

responseStr := resp.String()

assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())
assertEqual(t, true, strings.Contains(responseStr, "upload-file-1.json"))
assertEqual(t, true, strings.Contains(responseStr, "upload-file-2.json"))
}

func TestGetWithCookie(t *testing.T) {
ts := createGetServer(t)
defer ts.Close()
Expand Down
2 changes: 1 addition & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func createMultipartHeader(param, fileName, contentType string) textproto.MIMEHe
return hdr
}

func addMultipartFormField(w *multipart.Writer, mf *multipartField) error {
func addMultipartFormField(w *multipart.Writer, mf *MultipartField) error {
partWriter, err := w.CreatePart(createMultipartHeader(mf.Param, mf.FileName, mf.ContentType))
if err != nil {
return err
Expand Down

0 comments on commit c7aef45

Please sign in to comment.