-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reword body handling to use providers
Incidentally, also add support for multipart and file uploading.
- Loading branch information
Showing
3 changed files
with
221 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package sling | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"mime/multipart" | ||
"os" | ||
|
||
"path/filepath" | ||
|
||
goquery "github.com/google/go-querystring/query" | ||
) | ||
|
||
type BodyProvider interface { | ||
ContentType() string | ||
Body() (io.ReadCloser, error) | ||
} | ||
|
||
// Public | ||
|
||
func ReaderBody(body io.Reader) BodyProvider { | ||
return readerBodyProvider{reader: body} | ||
} | ||
|
||
func JSONBody(value interface{}) BodyProvider { | ||
if value == nil { | ||
return nil | ||
} | ||
|
||
return jsonBodyProvider{payload: value} | ||
} | ||
|
||
func FormBody(value interface{}) BodyProvider { | ||
if value == nil { | ||
return nil | ||
} | ||
|
||
return formBodyProvider{payload: value} | ||
} | ||
|
||
func MultipartBody(files map[string]string, extraParams map[string]string) BodyProvider { | ||
return multipartBodyProvider{files: files, extraParams: extraParams} | ||
} | ||
|
||
// Implementations | ||
|
||
// JSON | ||
|
||
type jsonBodyProvider struct { | ||
payload interface{} | ||
} | ||
|
||
func (p jsonBodyProvider) ContentType() string { | ||
return jsonContentType | ||
} | ||
|
||
func (p jsonBodyProvider) Body() (io.ReadCloser, error) { | ||
buf := &bytes.Buffer{} | ||
err := json.NewEncoder(buf).Encode(p.payload) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ioutil.NopCloser(buf), nil | ||
} | ||
|
||
// Form | ||
type formBodyProvider struct { | ||
payload interface{} | ||
} | ||
|
||
func (p formBodyProvider) ContentType() string { | ||
return formContentType | ||
} | ||
|
||
func (p formBodyProvider) Body() (io.ReadCloser, error) { | ||
values, err := goquery.Values(p.payload) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return ioutil.NopCloser(strings.NewReader(values.Encode())), nil | ||
} | ||
|
||
// Reader | ||
|
||
type readerBodyProvider struct { | ||
reader io.Reader | ||
} | ||
|
||
func (p readerBodyProvider) ContentType() string { | ||
return "" | ||
} | ||
|
||
func (p readerBodyProvider) Body() (io.ReadCloser, error) { | ||
rc, ok := p.reader.(io.ReadCloser) | ||
if !ok { | ||
rc = ioutil.NopCloser(p.reader) | ||
} | ||
return rc, nil | ||
} | ||
|
||
// Multipart | ||
|
||
var ( | ||
multipartFromBoundary = "SlingFormBoundary0amF3aGVy" | ||
) | ||
|
||
type multipartBodyProvider struct { | ||
files map[string]string | ||
extraParams map[string]string | ||
} | ||
|
||
func (p multipartBodyProvider) ContentType() string { | ||
return "multipart/form-data; boundary=" + multipartFromBoundary | ||
} | ||
|
||
func (p multipartBodyProvider) Body() (io.ReadCloser, error) { | ||
var body bytes.Buffer | ||
w := multipart.NewWriter(&body) | ||
w.SetBoundary(multipartFromBoundary) | ||
|
||
for paramName, filePath := range p.files { | ||
f, err := os.Open(filePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
fw, err := w.CreateFormFile(paramName, filepath.Base(filePath)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if _, err = io.Copy(fw, f); err != nil { | ||
return nil, err | ||
} | ||
} | ||
// Add the other fields | ||
for paramName, paramValue := range p.extraParams { | ||
fw, err := w.CreateFormField(paramName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if _, err := fw.Write([]byte(paramValue)); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
if err := w.Close(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return ioutil.NopCloser(&body), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.