-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
66 lines (56 loc) · 1.11 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
package patch
import (
"context"
"fmt"
"io"
"net/http"
)
// Request holds the information needed to make an HTTP request
type Request struct {
Ctx context.Context
Method string
URL string
Headers http.Header
Body interface{}
Encoder Encoder
}
func (r *Request) validate() error {
switch {
case !validMethod(r.Method):
return InvalidMethodError(r.Method)
}
return nil
}
func (r *Request) prepareBody(defaultEncoder Encoder) (io.Reader, string, error) {
if r.Body == nil {
return nil, "", nil
}
enc := r.Encoder
if enc == nil {
enc = defaultEncoder
}
if enc == nil {
return nil, "", fmt.Errorf("request has body but no encoder set on client or request")
}
reader, err := enc.Encode(r.Body)
if err != nil {
return nil, "", err
}
return reader, enc.ContentType(), nil
}
func validMethod(method string) bool {
switch method {
case http.MethodGet:
case http.MethodHead:
case http.MethodPost:
case http.MethodPut:
case http.MethodPatch:
case http.MethodDelete:
case http.MethodConnect:
case http.MethodOptions:
case http.MethodTrace:
default:
return false
}
return true
}