-
Notifications
You must be signed in to change notification settings - Fork 712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom data for multipart request #129
Custom data for multipart request #129
Conversation
Codecov Report
@@ Coverage Diff @@
## master #129 +/- ##
==========================================
- Coverage 96.32% 96.02% -0.31%
==========================================
Files 10 10
Lines 1034 1056 +22
==========================================
+ Hits 996 1014 +18
- Misses 21 23 +2
- Partials 17 19 +2
Continue to review full report at Codecov.
|
@Asker80 Thanks for the PR, I will have a look and get back. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Asker80 Thank you for your PR and idea to improve resty.
- Please have a look on comments and make updates.
- Also please add test cases into
request_test.go
for this implementation. - Bring PR into Green tick status
client.go
Outdated
@@ -865,3 +866,10 @@ type File struct { | |||
func (f *File) String() string { | |||
return fmt.Sprintf("ParamName: %v; FileName: %v", f.ParamName, f.Name) | |||
} | |||
|
|||
// MultipartCustomData represent custom data part for multipart request | |||
type MultipartCustomData struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Asker80 Change struct
name to multipartField
. Basically making as unexported struct.
client.go
Outdated
|
||
// MultipartCustomData represent custom data part for multipart request | ||
type MultipartCustomData struct { | ||
Params map[string]string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Asker80 Remove map and add two fields-
Param string
FileName string
request.go
Outdated
@@ -279,6 +279,19 @@ func (r *Request) SetFileReader(param, fileName string, reader io.Reader) *Reque | |||
return r | |||
} | |||
|
|||
// SetMultipartData method is to set custom data using io.Reader for multipart upload. | |||
func (r *Request) SetMultipartData(params map[string]string, contentType string, reader io.Reader) *Request { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Asker80 Change method name to SetMultipartField
. Your idea, it supports multiple fields.
Also update your method signature to
func (r *Request) SetMultipartField(param, fileName, contentType string, reader io.Reader) *Request {
request16.go
Outdated
@@ -43,6 +43,7 @@ type Request struct { | |||
isSaveResponse bool | |||
outputFile string | |||
multipartFiles []*File | |||
multipartCustomData []*MultipartCustomData |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Asker80 Change attribute name to multipartFields
request17.go
Outdated
@@ -44,6 +44,7 @@ type Request struct { | |||
isSaveResponse bool | |||
outputFile string | |||
multipartFiles []*File | |||
multipartCustomData []*MultipartCustomData |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Asker80 Change attribute name to multipartFields
util.go
Outdated
@@ -107,6 +107,24 @@ func escapeQuotes(s string) string { | |||
return quoteEscaper.Replace(s) | |||
} | |||
|
|||
func writeMultipartFormCustomData(w *multipart.Writer, params map[string]string, contentType string, r io.Reader) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Asker80 We are gonna improve it further.
- Create a new unexported method called
createMultipartHeader
func createMultipartHeader(param, fileName, contentType string) textproto.MIMEHeader {
hdr := make(textproto.MIMEHeader)
hdr.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
escapeQuotes(param), escapeQuotes(fileName)))
hdr.Set("Content-Type", contentType)
return hdr
}
- Change method signature to
func addMultipartFormField(w *multipart.Writer, mf *multipartField) error {
- Call method
createMultipartHeader
from methodaddMultipartFormField
to get part header
End result may look like-
func addMultipartFormField(w *multipart.Writer, mf *multipartField) error {
partWriter, err := w.CreatePart(createMultipartHeader(mf.Param, mf.FileName, mf.ContentType))
if err != nil {
return err
}
_, err = io.Copy(partWriter, r)
return err
}
Optional:
Call createMultipartHeader
from method writeMultipartFormFile
. Otherwise I will take care after your PR merge.
util.go
Outdated
@@ -148,6 +166,10 @@ func addFileReader(w *multipart.Writer, f *File) error { | |||
return writeMultipartFormFile(w, f.ParamName, f.Name, f.Reader) | |||
} | |||
|
|||
func addCustomDataReader(w *multipart.Writer, cd *MultipartCustomData) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Asker80 Please remove it, this method not needed after you take care of comments.
middleware.go
Outdated
@@ -300,6 +300,16 @@ func handleMultipart(c *Client, r *Request) (err error) { | |||
} | |||
} | |||
|
|||
// adding custom data support | |||
if len(r.multipartCustomData) > 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Asker80 Based review comments/recommendation. This section will change to
// GitHub #130 adding multipart field support with content type
if len(r.multipartFields) > 0 {
for _, mf := range r.multipartFields {
if err = addMultipartFormField(w, mf); err != nil {
return
}
}
}
client.go
Outdated
client: c, | ||
bodyBuf: nil, | ||
multipartFiles: []*File{}, | ||
multipartCustomData: []*MultipartCustomData{}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Asker80 Based on review comments/recommendation, please make appropriate updates.
@Asker80 Thanks for the updates. I'm merging it. |
Proposal for
MultipartCustomData
type, in case there's a need to inject custom data into multipart request. SinceSetFile()
andSetFileReader()
functions are specifically file-oriented, I created a separate type to holdContent-Disposition
header fields,Content-Type
and the actual data.