-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
88 lines (72 loc) · 2.4 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package httpbatcher
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
"net/textproto"
)
// ErrInvalidMediaType is returned when the media type is not multipart/mixed
var ErrInvalidMediaType = fmt.Errorf("Expected media type multipart/mixed")
// ErrMultipartBoundaryNotDefined is returned when the media type is not multipart/mixed
var ErrMultipartBoundaryNotDefined = fmt.Errorf("Media type multipart/mixed require boundary parameter")
// BuildRequest encode a slice of requests into a single, multipart one
func BuildRequest(targetURL string, requests ...*http.Request) (*http.Request, error) {
var body bytes.Buffer
req, err := http.NewRequest(http.MethodPost, targetURL, &body)
if err != nil {
return nil, fmt.Errorf("Impossible to create the wrapper request: %v", err)
}
multiWriter := multipart.NewWriter(&body)
partHeaders := textproto.MIMEHeader{}
partHeaders.Set("Content-Type", "application/http")
for _, r := range requests {
partBody, errFor := multiWriter.CreatePart(partHeaders)
if errFor != nil {
return nil, fmt.Errorf("Error while creating request part: %v", errFor)
}
r.WriteProxy(partBody)
}
multiWriter.Close()
mediaType := mime.FormatMediaType("multipart/mixed", map[string]string{"boundary": multiWriter.Boundary()})
req.Header.Set("Content-Type", mediaType)
return req, err
}
// UnpackResponse decode a multipart response into a slice containing its parts
func UnpackResponse(fatResponse *http.Response) ([]*http.Response, error) {
if fatResponse.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(fatResponse.Body)
return nil, fmt.Errorf("Response status is %v: %v", fatResponse.StatusCode, string(body))
}
mediaType, params, err := mime.ParseMediaType(fatResponse.Header.Get("Content-Type"))
if err != nil {
return nil, fmt.Errorf("Can't parse the content type: %v", err)
}
if mediaType != "multipart/mixed" {
return nil, ErrInvalidMediaType
}
if params["boundary"] == "" {
return nil, ErrMultipartBoundaryNotDefined
}
mr := multipart.NewReader(fatResponse.Body, params["boundary"])
var responses []*http.Response
for {
np, err := mr.NextPart()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
resp, err := http.ReadResponse(bufio.NewReader(np), nil)
if err != nil {
return nil, fmt.Errorf("Unexpected error: %v", err)
}
responses = append(responses, resp)
}
return responses, nil
}