/
restful.go
executable file
·221 lines (188 loc) · 4.69 KB
/
restful.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package rest
import (
"bytes"
"github.com/go-chassis/go-chassis/core/client"
"io"
"io/ioutil"
"net/http"
"sync"
)
//Client is a struct
type Client struct {
c *http.Client
opts client.Options
mu sync.Mutex // protects following
}
//Do is a method
func (c *Client) Do(req *Request, resp *Response) (err error) {
resp.Resp, err = c.c.Do(req.Req)
return
}
//Request is struct
type Request struct {
Req *http.Request
}
//NewRequest is a function which creates new request
func NewRequest(method, urlStr string, body ...[]byte) (*Request, error) {
if method == "" {
method = "GET"
}
var r io.Reader
if body != nil {
r = bytes.NewReader(body[0])
}
req, err := http.NewRequest(method, urlStr, r)
if err != nil {
return nil, err
}
return &Request{Req: req}, nil
}
//SetURI sets host for the request.
func (req *Request) SetURI(url string) {
if tempURL, err := req.Req.URL.Parse(url); err == nil {
req.Req.URL = tempURL
}
}
//Copy is method
func (req *Request) Copy() *Request {
newReq, err := http.NewRequest(req.Req.Method, req.Req.URL.String(), req.Req.Body)
if err != nil {
return nil
}
//Copy headers
for key := range req.Req.Header {
newReq.Header.Add(key, req.Req.Header.Get(key))
}
//Copy cookies
for _, c := range req.Req.Cookies() {
newReq.AddCookie(c)
}
return &Request{Req: newReq}
}
//GetRequest is a method
func (req *Request) GetRequest() *http.Request {
return req.Req
}
//SetBody is a method used for setting body for a request
func (req *Request) SetBody(body []byte) {
req.Req.Body = ioutil.NopCloser(bytes.NewReader(body))
}
//SetCookie set key value in request cookie
func (req *Request) SetCookie(k, v string) {
c := &http.Cookie{
Name: k,
Value: v,
}
req.Req.AddCookie(c)
}
//GetURI is a method
func (req *Request) GetURI() string {
return req.Req.URL.String()
}
// SetContentType is a method used for setting content-type in a request
func (req *Request) SetContentType(ct string) {
req.Req.Header.Set("Content-Type", ct)
}
// GetContentType is a method used for getting content-type in a request
func (req *Request) GetContentType() string {
return req.Req.Header.Get("Content-Type")
}
//SetHeader is a method used for setting header in a request
func (req *Request) SetHeader(key, value string) {
req.Req.Header.Set(key, value)
}
//SetHeaderCookie is a method used to setting header cookie
func (req *Request) SetHeaderCookie(key, value string) {
req.Req.Header.Set(key, value)
}
//GetHeader is a method which gets head from a request
func (req *Request) GetHeader(key string) string {
return string(req.Req.Header.Get(key))
}
//GetCookie is a method which gets cookie from a request
func (req *Request) GetCookie(key string) string {
cookie, err := req.Req.Cookie(key)
if err == http.ErrNoCookie {
return ""
}
return cookie.Value
}
//SetMethod is a method
func (req *Request) SetMethod(method string) {
req.Req.Method = method
}
//GetMethod is a method
func (req *Request) GetMethod() string {
return req.Req.Method
}
//Close is used for closing a request
//TODO Confirm it's necessary or not
func (req *Request) Close() {
//req.Req.Body.Close()
}
//Response is a struct used for handling response
type Response struct {
Resp *http.Response
}
// NewResponse is creating the object of response
func NewResponse() *Response {
resp := http.Response{
Header: http.Header{},
}
return &Response{
Resp: &resp,
}
}
// GetResponse is a method used to get response
func (resp *Response) GetResponse() *http.Response {
return resp.Resp
}
// GetStatusCode returns response status code.
func (resp *Response) GetStatusCode() int {
if resp.Resp != nil {
return resp.Resp.StatusCode
}
return 0
}
// SetStatusCode sets the status code
func (resp *Response) SetStatusCode(s int) {
resp.Resp.StatusCode = s
}
// ReadBody read body from the from the response
func (resp *Response) ReadBody() []byte {
if resp.Resp != nil && resp.Resp.Body != nil {
body, err := ioutil.ReadAll(resp.Resp.Body)
if err != nil {
return nil
}
return body
}
return nil
}
// GetHeader get header from the response
//TODO Confirm it's necessary or not
func (resp *Response) GetHeader() []byte {
bf := new(bytes.Buffer)
resp.Resp.Header.Write(bf)
return bf.Bytes()
}
// Close closes the file descriptor
//TODO Confirm it's necessary or not
func (resp *Response) Close() {
if resp.Resp != nil && resp.Resp.Body != nil {
resp.Resp.Body.Close()
}
}
// GetCookie returns response Cookie.
func (resp *Response) GetCookie(key string) []byte {
for _, c := range resp.Resp.Cookies() {
if c.Name == key {
return []byte(c.Value)
}
}
return nil
}
// SetCookie sets the cookie.
func (resp *Response) SetCookie(cookie *http.Cookie) {
resp.Resp.Header.Add("Set-Cookie", cookie.String())
}