Merge pull request #418 from fuxiaohei/master
add comments for httplib package.
Showing
1 changed file
with
27 additions
and
0 deletions
| ... | @@ -18,6 +18,7 @@ import ( | ... | @@ -18,6 +18,7 @@ import ( |
| 18 | 18 | ||
| 19 | var defaultUserAgent = "beegoServer" | 19 | var defaultUserAgent = "beegoServer" |
| 20 | 20 | ||
| 21 | // Get returns *BeegoHttpRequest with GET method. | ||
| 21 | func Get(url string) *BeegoHttpRequest { | 22 | func Get(url string) *BeegoHttpRequest { |
| 22 | var req http.Request | 23 | var req http.Request |
| 23 | req.Method = "GET" | 24 | req.Method = "GET" |
| ... | @@ -26,6 +27,7 @@ func Get(url string) *BeegoHttpRequest { | ... | @@ -26,6 +27,7 @@ func Get(url string) *BeegoHttpRequest { |
| 26 | return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil} | 27 | return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil} |
| 27 | } | 28 | } |
| 28 | 29 | ||
| 30 | // Post returns *BeegoHttpRequest with POST method. | ||
| 29 | func Post(url string) *BeegoHttpRequest { | 31 | func Post(url string) *BeegoHttpRequest { |
| 30 | var req http.Request | 32 | var req http.Request |
| 31 | req.Method = "POST" | 33 | req.Method = "POST" |
| ... | @@ -34,6 +36,7 @@ func Post(url string) *BeegoHttpRequest { | ... | @@ -34,6 +36,7 @@ func Post(url string) *BeegoHttpRequest { |
| 34 | return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil} | 36 | return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil} |
| 35 | } | 37 | } |
| 36 | 38 | ||
| 39 | // Put returns *BeegoHttpRequest with PUT method. | ||
| 37 | func Put(url string) *BeegoHttpRequest { | 40 | func Put(url string) *BeegoHttpRequest { |
| 38 | var req http.Request | 41 | var req http.Request |
| 39 | req.Method = "PUT" | 42 | req.Method = "PUT" |
| ... | @@ -42,6 +45,7 @@ func Put(url string) *BeegoHttpRequest { | ... | @@ -42,6 +45,7 @@ func Put(url string) *BeegoHttpRequest { |
| 42 | return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil} | 45 | return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil} |
| 43 | } | 46 | } |
| 44 | 47 | ||
| 48 | // Delete returns *BeegoHttpRequest DELETE GET method. | ||
| 45 | func Delete(url string) *BeegoHttpRequest { | 49 | func Delete(url string) *BeegoHttpRequest { |
| 46 | var req http.Request | 50 | var req http.Request |
| 47 | req.Method = "DELETE" | 51 | req.Method = "DELETE" |
| ... | @@ -50,6 +54,7 @@ func Delete(url string) *BeegoHttpRequest { | ... | @@ -50,6 +54,7 @@ func Delete(url string) *BeegoHttpRequest { |
| 50 | return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil} | 54 | return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil} |
| 51 | } | 55 | } |
| 52 | 56 | ||
| 57 | // Head returns *BeegoHttpRequest with HEAD method. | ||
| 53 | func Head(url string) *BeegoHttpRequest { | 58 | func Head(url string) *BeegoHttpRequest { |
| 54 | var req http.Request | 59 | var req http.Request |
| 55 | req.Method = "HEAD" | 60 | req.Method = "HEAD" |
| ... | @@ -58,6 +63,7 @@ func Head(url string) *BeegoHttpRequest { | ... | @@ -58,6 +63,7 @@ func Head(url string) *BeegoHttpRequest { |
| 58 | return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil} | 63 | return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil} |
| 59 | } | 64 | } |
| 60 | 65 | ||
| 66 | // BeegoHttpRequest provides more useful methods for requesting one url than http.Request. | ||
| 61 | type BeegoHttpRequest struct { | 67 | type BeegoHttpRequest struct { |
| 62 | url string | 68 | url string |
| 63 | req *http.Request | 69 | req *http.Request |
| ... | @@ -68,37 +74,46 @@ type BeegoHttpRequest struct { | ... | @@ -68,37 +74,46 @@ type BeegoHttpRequest struct { |
| 68 | tlsClientConfig *tls.Config | 74 | tlsClientConfig *tls.Config |
| 69 | } | 75 | } |
| 70 | 76 | ||
| 77 | // Debug sets show debug or not when executing request. | ||
| 71 | func (b *BeegoHttpRequest) Debug(isdebug bool) *BeegoHttpRequest { | 78 | func (b *BeegoHttpRequest) Debug(isdebug bool) *BeegoHttpRequest { |
| 72 | b.showdebug = isdebug | 79 | b.showdebug = isdebug |
| 73 | return b | 80 | return b |
| 74 | } | 81 | } |
| 75 | 82 | ||
| 83 | // SetTimeout sets connect time out and read-write time out for BeegoRequest. | ||
| 76 | func (b *BeegoHttpRequest) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *BeegoHttpRequest { | 84 | func (b *BeegoHttpRequest) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *BeegoHttpRequest { |
| 77 | b.connectTimeout = connectTimeout | 85 | b.connectTimeout = connectTimeout |
| 78 | b.readWriteTimeout = readWriteTimeout | 86 | b.readWriteTimeout = readWriteTimeout |
| 79 | return b | 87 | return b |
| 80 | } | 88 | } |
| 81 | 89 | ||
| 90 | // SetTLSClientConfig sets tls connection configurations if visiting https url. | ||
| 82 | func (b *BeegoHttpRequest) SetTLSClientConfig(config *tls.Config) *BeegoHttpRequest { | 91 | func (b *BeegoHttpRequest) SetTLSClientConfig(config *tls.Config) *BeegoHttpRequest { |
| 83 | b.tlsClientConfig = config | 92 | b.tlsClientConfig = config |
| 84 | return b | 93 | return b |
| 85 | } | 94 | } |
| 86 | 95 | ||
| 96 | // Header add header item string in request. | ||
| 87 | func (b *BeegoHttpRequest) Header(key, value string) *BeegoHttpRequest { | 97 | func (b *BeegoHttpRequest) Header(key, value string) *BeegoHttpRequest { |
| 88 | b.req.Header.Set(key, value) | 98 | b.req.Header.Set(key, value) |
| 89 | return b | 99 | return b |
| 90 | } | 100 | } |
| 91 | 101 | ||
| 102 | // SetCookie add cookie into request. | ||
| 92 | func (b *BeegoHttpRequest) SetCookie(cookie *http.Cookie) *BeegoHttpRequest { | 103 | func (b *BeegoHttpRequest) SetCookie(cookie *http.Cookie) *BeegoHttpRequest { |
| 93 | b.req.Header.Add("Cookie", cookie.String()) | 104 | b.req.Header.Add("Cookie", cookie.String()) |
| 94 | return b | 105 | return b |
| 95 | } | 106 | } |
| 96 | 107 | ||
| 108 | // Param adds query param in to request. | ||
| 109 | // params build query string as ?key1=value1&key2=value2... | ||
| 97 | func (b *BeegoHttpRequest) Param(key, value string) *BeegoHttpRequest { | 110 | func (b *BeegoHttpRequest) Param(key, value string) *BeegoHttpRequest { |
| 98 | b.params[key] = value | 111 | b.params[key] = value |
| 99 | return b | 112 | return b |
| 100 | } | 113 | } |
| 101 | 114 | ||
| 115 | // Body adds request raw body. | ||
| 116 | // it supports string and []byte. | ||
| 102 | func (b *BeegoHttpRequest) Body(data interface{}) *BeegoHttpRequest { | 117 | func (b *BeegoHttpRequest) Body(data interface{}) *BeegoHttpRequest { |
| 103 | switch t := data.(type) { | 118 | switch t := data.(type) { |
| 104 | case string: | 119 | case string: |
| ... | @@ -169,6 +184,8 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { | ... | @@ -169,6 +184,8 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { |
| 169 | return resp, nil | 184 | return resp, nil |
| 170 | } | 185 | } |
| 171 | 186 | ||
| 187 | // String returns the body string in response. | ||
| 188 | // it calls Response inner. | ||
| 172 | func (b *BeegoHttpRequest) String() (string, error) { | 189 | func (b *BeegoHttpRequest) String() (string, error) { |
| 173 | data, err := b.Bytes() | 190 | data, err := b.Bytes() |
| 174 | if err != nil { | 191 | if err != nil { |
| ... | @@ -178,6 +195,8 @@ func (b *BeegoHttpRequest) String() (string, error) { | ... | @@ -178,6 +195,8 @@ func (b *BeegoHttpRequest) String() (string, error) { |
| 178 | return string(data), nil | 195 | return string(data), nil |
| 179 | } | 196 | } |
| 180 | 197 | ||
| 198 | // Bytes returns the body []byte in response. | ||
| 199 | // it calls Response inner. | ||
| 181 | func (b *BeegoHttpRequest) Bytes() ([]byte, error) { | 200 | func (b *BeegoHttpRequest) Bytes() ([]byte, error) { |
| 182 | resp, err := b.getResponse() | 201 | resp, err := b.getResponse() |
| 183 | if err != nil { | 202 | if err != nil { |
| ... | @@ -194,6 +213,8 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) { | ... | @@ -194,6 +213,8 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) { |
| 194 | return data, nil | 213 | return data, nil |
| 195 | } | 214 | } |
| 196 | 215 | ||
| 216 | // ToFile saves the body data in response to one file. | ||
| 217 | // it calls Response inner. | ||
| 197 | func (b *BeegoHttpRequest) ToFile(filename string) error { | 218 | func (b *BeegoHttpRequest) ToFile(filename string) error { |
| 198 | f, err := os.Create(filename) | 219 | f, err := os.Create(filename) |
| 199 | if err != nil { | 220 | if err != nil { |
| ... | @@ -216,6 +237,8 @@ func (b *BeegoHttpRequest) ToFile(filename string) error { | ... | @@ -216,6 +237,8 @@ func (b *BeegoHttpRequest) ToFile(filename string) error { |
| 216 | return nil | 237 | return nil |
| 217 | } | 238 | } |
| 218 | 239 | ||
| 240 | // ToJson returns the map that marshals from the body bytes as json in response . | ||
| 241 | // it calls Response inner. | ||
| 219 | func (b *BeegoHttpRequest) ToJson(v interface{}) error { | 242 | func (b *BeegoHttpRequest) ToJson(v interface{}) error { |
| 220 | data, err := b.Bytes() | 243 | data, err := b.Bytes() |
| 221 | if err != nil { | 244 | if err != nil { |
| ... | @@ -228,6 +251,8 @@ func (b *BeegoHttpRequest) ToJson(v interface{}) error { | ... | @@ -228,6 +251,8 @@ func (b *BeegoHttpRequest) ToJson(v interface{}) error { |
| 228 | return nil | 251 | return nil |
| 229 | } | 252 | } |
| 230 | 253 | ||
| 254 | // ToXml returns the map that marshals from the body bytes as xml in response . | ||
| 255 | // it calls Response inner. | ||
| 231 | func (b *BeegoHttpRequest) ToXML(v interface{}) error { | 256 | func (b *BeegoHttpRequest) ToXML(v interface{}) error { |
| 232 | data, err := b.Bytes() | 257 | data, err := b.Bytes() |
| 233 | if err != nil { | 258 | if err != nil { |
| ... | @@ -240,10 +265,12 @@ func (b *BeegoHttpRequest) ToXML(v interface{}) error { | ... | @@ -240,10 +265,12 @@ func (b *BeegoHttpRequest) ToXML(v interface{}) error { |
| 240 | return nil | 265 | return nil |
| 241 | } | 266 | } |
| 242 | 267 | ||
| 268 | // Response executes request client gets response mannually. | ||
| 243 | func (b *BeegoHttpRequest) Response() (*http.Response, error) { | 269 | func (b *BeegoHttpRequest) Response() (*http.Response, error) { |
| 244 | return b.getResponse() | 270 | return b.getResponse() |
| 245 | } | 271 | } |
| 246 | 272 | ||
| 273 | // TimeoutDialer returns functions of connection dialer with timeout settings for http.Transport Dial field. | ||
| 247 | func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error) { | 274 | func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error) { |
| 248 | return func(netw, addr string) (net.Conn, error) { | 275 | return func(netw, addr string) (net.Conn, error) { |
| 249 | conn, err := net.DialTimeout(netw, addr, cTimeout) | 276 | conn, err := net.DialTimeout(netw, addr, cTimeout) | ... | ... |
-
Please register or sign in to post a comment