-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
109 lines (96 loc) · 2.31 KB
/
request.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package batch
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
)
var skipHeaders = map[string]struct{}{
"Accept-Encoding": {},
"Connection": {},
"Content-Encoding": {},
"Keep-Alive": {},
"Transfer-Encoding": {},
"Upgrade": {},
}
// Request is a batch request.
type Request struct {
Defaults *RequestPart
Requests []RequestPart
}
func (r *Request) containsRecursive() bool {
if r.Defaults != nil && r.Defaults.isRecursive() {
return true
}
for _, part := range r.Requests {
if part.isRecursive() {
return true
}
}
return false
}
// RequestPart contains sub-request information of a batch request.
type RequestPart struct {
Method string `json:"method"`
Path string `json:"path"`
Body json.RawMessage `json:"body"`
Headers map[string]string `json:"headers"`
}
// Norm merges defaults and normalizes part.
func (p *RequestPart) Norm(namespace string, defaults *RequestPart) {
if defaults == nil {
return
}
if p.Method == "" {
p.Method = defaults.Method
}
if p.Path == "" {
p.Path = defaults.Path
}
if len(p.Body) == 0 {
p.Body = append(p.Body[:0], defaults.Body...)
}
if n := len(defaults.Headers); n != 0 {
if p.Headers == nil {
p.Headers = make(map[string]string, n)
}
for key, val := range defaults.Headers {
if _, ok := p.Headers[key]; !ok {
p.Headers[key] = val
}
}
}
if strings.HasPrefix(p.Path, namespace+"/") {
p.Path = strings.TrimPrefix(p.Path, namespace)
}
}
func (p *RequestPart) isRecursive() bool {
return strings.HasPrefix(p.Path, "/batch")
}
func (p *RequestPart) httpRequest(ctx context.Context, parent http.Header) (*http.Request, error) {
switch strings.ToUpper(p.Method) {
case http.MethodGet, http.MethodHead, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete:
// OK
default:
return nil, fmt.Errorf("invalid method %q", p.Method)
}
hr, err := http.NewRequestWithContext(ctx, p.Method, p.Path, bytes.NewReader(p.Body))
if err != nil {
if _, ok := err.(*url.Error); ok {
return nil, fmt.Errorf("invalid path %q", p.Path)
}
return nil, err
}
for key := range parent {
if _, ok := skipHeaders[key]; !ok {
hr.Header.Set(key, parent.Get(key))
}
}
for key, val := range p.Headers {
hr.Header.Set(key, val)
}
return hr, nil
}