2014-11-02 19:04:19 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"io"
|
|
|
|
|
"mime/multipart"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type HTTP interface {
|
2014-11-05 13:49:31 +00:00
|
|
|
PostForm(string, string, string, map[string]string, map[string]io.Reader) (*http.Response, error)
|
2014-11-02 19:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
2014-11-15 13:33:20 +01:00
|
|
|
type HTTPClient struct{}
|
2014-11-02 19:04:19 +01:00
|
|
|
|
2014-11-15 13:33:20 +01:00
|
|
|
func (c *HTTPClient) PostForm(url, username, password string, headers map[string]string, files map[string]io.Reader) (*http.Response, error) {
|
2014-11-05 13:49:31 +00:00
|
|
|
req, err := createPostRequest(url, username, password, headers, files)
|
2014-11-02 19:04:19 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
|
|
2014-11-05 13:49:31 +00:00
|
|
|
return client.Do(req)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func createPostRequest(url, username, password string, headers map[string]string, files map[string]io.Reader) (*http.Request, error) {
|
|
|
|
|
body, contentType, err := multiPartBody(url, files)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-02 19:04:19 +01:00
|
|
|
req, err := http.NewRequest("POST", url, body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-05 13:49:31 +00:00
|
|
|
setHeaders(req, contentType, username, password, headers)
|
|
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func setHeaders(req *http.Request, contentType, username, password string, headers map[string]string) {
|
2014-11-02 19:04:19 +01:00
|
|
|
req.SetBasicAuth(username, password)
|
|
|
|
|
|
2014-11-05 13:49:31 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
|
|
|
|
|
|
for name, value := range headers {
|
|
|
|
|
req.Header.Set(name, value)
|
|
|
|
|
}
|
2014-11-02 19:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
2014-11-05 13:49:31 +00:00
|
|
|
func multiPartBody(url string, files map[string]io.Reader) (io.Reader, string, error) {
|
2014-11-02 19:04:19 +01:00
|
|
|
body := &bytes.Buffer{}
|
|
|
|
|
writer := multipart.NewWriter(body)
|
|
|
|
|
|
|
|
|
|
if files != nil {
|
|
|
|
|
for name, reader := range files {
|
2014-11-05 13:49:31 +00:00
|
|
|
err := addFormFile(writer, name, reader)
|
2014-11-02 19:04:19 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, "", err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := writer.Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return body, writer.FormDataContentType(), nil
|
|
|
|
|
}
|
2014-11-05 13:49:31 +00:00
|
|
|
|
|
|
|
|
func addFormFile(writer *multipart.Writer, name string, reader io.Reader) error {
|
|
|
|
|
items := strings.Split(name, ":")
|
|
|
|
|
fieldname := items[0]
|
|
|
|
|
filename := items[1]
|
|
|
|
|
|
|
|
|
|
part, err := writer.CreateFormFile(fieldname, filename)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = io.Copy(part, reader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|