-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
127 lines (103 loc) · 2.86 KB
/
Copy pathclient.go
File metadata and controls
127 lines (103 loc) · 2.86 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// HTTP Client that simplifies HTTP interactions in Go by
// wrapping the built-in net/http package.
package http
import (
"bytes"
"io/ioutil"
"net/http"
"time"
)
// Client represents a HTTP client that sends Requests and receives Responses.
type Client struct {
cookies []*http.Cookie // maintained for the entire client session
}
// Request represents an HTTP request sent by a client.
type Request struct {
URL string
Timeout int // in seconds
Payload []byte
ContentType string
}
// Response represents the response from an HTTP request.
type Response struct {
StatusCode int // e.g. 200
Payload []byte
Header http.Header
}
// NewRequest returns a new Request given a URL, payload and contentType.
func NewRequest(url, payload, contentType string) *Request {
return &Request{
URL: url,
Timeout: DefaultTimeout, // default in seconds
Payload: []byte(payload),
ContentType: contentType,
}
}
// NewGetRequest returns a new Request given just a URL.
func NewGetRequest(url string) *Request {
return &Request{
URL: url,
Timeout: DefaultTimeout, // default in seconds
Payload: nil,
ContentType: ContentTypeNone,
}
}
// Get sends a HTTP GET request to a server
func (c *Client) Get(req *Request) (*Response, error) {
return c.Send(req, MethodGet)
}
// Post sends a HTTP Post request to a server
func (c *Client) Post(req *Request) (*Response, error) {
return c.Send(req, MethodPost)
}
// Send a HTTP request of any method type to a server
func (c *Client) Send(request *Request, method string) (*Response, error) {
timeout := time.Duration(time.Duration(request.Timeout) * time.Second)
client := http.Client{
Timeout: timeout,
}
req, err := http.NewRequest(method, request.URL, bytes.NewReader(request.Payload))
// Not all requests require a ContentType (e.g. GETs)
if request.ContentType != ContentTypeNone {
req.Header.Set("Content-Type", request.ContentType)
}
// Add any cookies that have been obtained from
// any previous calls on this client.
for _, cookie := range c.cookies {
req.AddCookie(cookie)
}
// make the actual HTTP request
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
payload, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// Keep the Cookies for the next request
for _, cookie := range resp.Cookies() {
if index := getCookieIndex(cookie.Name, c.cookies); index > -1 {
// update existing cookie
c.cookies[index] = cookie
} else {
// add new cookies
c.cookies = append(c.cookies, cookie)
}
}
response := &Response{
StatusCode: resp.StatusCode,
Payload: payload,
Header: resp.Header,
}
return response, nil
}
func getCookieIndex(name string, cookies []*http.Cookie) int {
for index, cookie := range cookies {
if cookie.Name == name {
return index
}
}
return -1
}