Skip to content

Commit

Permalink
Renaming MultipartBody into MultipartMessage and introducing the File…
Browse files Browse the repository at this point in the history
…Map type.
  • Loading branch information
xiam committed Aug 13, 2014
1 parent 2bb4965 commit b33f49f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
32 changes: 19 additions & 13 deletions main.go
Expand Up @@ -71,9 +71,11 @@ type File struct {
io.Reader
}

// MultipartBody struct for multipart requests, you can't generate a
// MultipartBody directly, use rest.NewMultipartBody() instead.
type MultipartBody struct {
type FileMap map[string][]File

// MultipartMessage struct for multipart requests, you can't generate a
// MultipartMessage directly, use rest.NewMultipartMessage() instead.
type MultipartMessage struct {
contentType string
buf io.Reader
}
Expand All @@ -82,8 +84,12 @@ type MultipartBody struct {
// to use the same prefix for all of your requests or in scenarios where it
// would be handy to keep a session cookie.
type Client struct {
Header http.Header
Prefix string
// These headers will be added in every request.
Header http.Header
// String to be added at the begining of every URL in Get(), Post(), Put()
// and Delete() methods.
Prefix string
// Jar to store cookies.
CookieJar *cookiejar.Jar
}

Expand Down Expand Up @@ -130,7 +136,7 @@ func (self *Client) SetBasicAuth(username string, password string) {
self.Header.Set("Authorization", "Basic "+basicAuth(username, password))
}

func (self *Client) newMultipartRequest(dst interface{}, method string, addr *url.URL, body *MultipartBody) error {
func (self *Client) newMultipartRequest(dst interface{}, method string, addr *url.URL, body *MultipartMessage) error {
var res *http.Response
var req *http.Request

Expand Down Expand Up @@ -232,7 +238,7 @@ func (self *Client) Delete(dst interface{}, path string, data url.Values) error
// PutMultipart performs a HTTP PUT multipart request and, when complete,
// attempts to convert the response body into the datatype given by dst (a
// pointer to a struct, map or []byte array).
func (self *Client) PutMultipart(dst interface{}, uri string, data *MultipartBody) error {
func (self *Client) PutMultipart(dst interface{}, uri string, data *MultipartMessage) error {
var addr *url.URL
var err error

Expand All @@ -246,7 +252,7 @@ func (self *Client) PutMultipart(dst interface{}, uri string, data *MultipartBod
// PostMultipart performs a HTTP POST multipart request and, when complete,
// attempts to convert the response body into the datatype given by dst (a
// pointer to a struct, map or []byte array).
func (self *Client) PostMultipart(dst interface{}, uri string, data *MultipartBody) error {
func (self *Client) PostMultipart(dst interface{}, uri string, data *MultipartMessage) error {
var addr *url.URL
var err error

Expand Down Expand Up @@ -317,9 +323,9 @@ func (self *Client) Get(dst interface{}, path string, data url.Values) error {
return self.newRequest(dst, "GET", addr, nil)
}

// NewMultipartBody creates a *MultipartBody based on the given parameters.
// NewMultipartMessage creates a *MultipartMessage based on the given parameters.
// This is useful for PostMultipart() and PutMultipart().
func NewMultipartBody(params url.Values, filemap map[string][]File) (*MultipartBody, error) {
func NewMultipartMessage(params url.Values, filemap FileMap) (*MultipartMessage, error) {

dst := bytes.NewBuffer(nil)

Expand Down Expand Up @@ -353,7 +359,7 @@ func NewMultipartBody(params url.Values, filemap map[string][]File) (*MultipartB

body.Close()

return &MultipartBody{body.FormDataContentType(), dst}, nil
return &MultipartMessage{body.FormDataContentType(), dst}, nil
}

// Returns the body of the request as a io.ReadCloser
Expand Down Expand Up @@ -581,13 +587,13 @@ func Delete(dest interface{}, uri string, data url.Values) error {
// PostMultipart performs a HTTP POST multipart request using the default
// client and, when complete, attempts to convert the response body into the
// datatype given by dst (a pointer to a struct, map or []byte array).
func PostMultipart(dest interface{}, uri string, data *MultipartBody) error {
func PostMultipart(dest interface{}, uri string, data *MultipartMessage) error {
return DefaultClient.PostMultipart(dest, uri, data)
}

// PutMultipart performs a HTTP PUT multipart request using the default client
// and, when complete, attempts to convert the response body into the datatype
// given by dst (a pointer to a struct, map or []byte array).
func PutMultipart(dest interface{}, uri string, data *MultipartBody) error {
func PutMultipart(dest interface{}, uri string, data *MultipartMessage) error {
return DefaultClient.PutMultipart(dest, uri, data)
}
24 changes: 12 additions & 12 deletions main_test.go
Expand Up @@ -244,14 +244,14 @@ func TestPostMultipart(t *testing.T) {

files := map[string][]File{
"file": []File{
File{
path.Base(fileMain.Name()),
fileMain,
{
Name: path.Base(fileMain.Name()),
Reader: fileMain,
},
},
}

body, err := NewMultipartBody(url.Values{"foo": {"bar"}}, files)
body, err := NewMultipartMessage(url.Values{"foo": {"bar"}}, files)

var buf map[string]interface{}

Expand All @@ -265,7 +265,7 @@ func TestPostMultipart(t *testing.T) {
t.Fatalf("Test failed.")
}

body, err = NewMultipartBody(nil, files)
body, err = NewMultipartMessage(nil, files)

err = client.PostMultipart(&buf, "/post", body)

Expand All @@ -277,7 +277,7 @@ func TestPostMultipart(t *testing.T) {
t.Fatalf("Test failed.")
}

body, err = NewMultipartBody(url.Values{"foo": {"bar"}}, nil)
body, err = NewMultipartMessage(url.Values{"foo": {"bar"}}, nil)

err = client.PostMultipart(&buf, "/post", body)

Expand Down Expand Up @@ -308,7 +308,7 @@ func TestPutMultipart(t *testing.T) {
},
}

body, err := NewMultipartBody(url.Values{"foo": {"bar"}}, files)
body, err := NewMultipartMessage(url.Values{"foo": {"bar"}}, files)

var buf map[string]interface{}

Expand All @@ -322,7 +322,7 @@ func TestPutMultipart(t *testing.T) {
t.Fatalf("Test failed.")
}

body, err = NewMultipartBody(nil, files)
body, err = NewMultipartMessage(nil, files)

err = client.PutMultipart(&buf, "/put", body)

Expand All @@ -334,7 +334,7 @@ func TestPutMultipart(t *testing.T) {
t.Fatalf("Test failed.")
}

body, err = NewMultipartBody(url.Values{"foo": {"bar"}}, nil)
body, err = NewMultipartMessage(url.Values{"foo": {"bar"}}, nil)

err = client.PutMultipart(&buf, "/put", body)

Expand All @@ -356,16 +356,16 @@ func TestSugar(t *testing.T) {

defer fileMain.Close()

files := map[string][]File{
files := FileMap{
"file": []File{
File{
{
path.Base(fileMain.Name()),
fileMain,
},
},
}

body, err := NewMultipartBody(url.Values{"foo": {"bar"}}, files)
body, err := NewMultipartMessage(url.Values{"foo": {"bar"}}, files)

var buf map[string]interface{}

Expand Down

0 comments on commit b33f49f

Please sign in to comment.