-
Notifications
You must be signed in to change notification settings - Fork 11
/
http.go
101 lines (85 loc) · 1.82 KB
/
http.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
package http
import (
gourl "net/url"
"strings"
"time"
)
var (
driver Driver
)
func SetDriver(drv Driver) {
driver = drv
}
func Do(req *Request) (*Response, error) {
return driver.Do(req)
}
type (
Header map[string][]string
Request struct {
Body []byte
Header Header
Method string
URL *gourl.URL
Timeout time.Duration
WithCredentials bool
}
Driver interface {
Do(*Request) (*Response, error)
}
Response struct {
Body []byte
Status string
StatusCode int
Header Header
}
)
// Add adds the key, value pair to the header.
// It appends to any existing values associated with key.
func (h Header) Add(key, value string) {
if _, ok := h[key]; !ok {
h[key] = make([]string, 0)
}
h[key] = append(h[key], value)
}
// Set sets the header entries associated with key to
// the single element value. It replaces any existing
// values associated with key.
func (h Header) Set(key, value string) {
if _, ok := h[key]; ok {
h[key] = nil
}
h[key] = make([]string, 0)
h[key] = append(h[key], value)
}
// Get gets the first value associated with the given key.
// If there are no values associated with the key, Get returns "".
func (h Header) Get(key string) string {
if v, ok := h[key]; ok && len(v) > 0 {
return v[0]
}
return ""
}
// Del deletes the values associated with key.
func (h Header) Del(key string) {
if _, ok := h[key]; ok {
delete(h, key)
}
}
func (h Header) String() (s string) {
for key, values := range h {
s += key + ": " + strings.Join(values, ";") + "\n"
}
return
}
func NewRequest(method string, url string, body []byte) (*Request, error) {
u, err := gourl.Parse(url)
if err != nil {
return nil, err
}
return &Request{
Method: method,
Header: make(map[string][]string),
URL: u,
Body: body,
}, nil
}