Skip to content

Commit 7016d69

Browse files
committed
简化http请求
1 parent 3a7bed0 commit 7016d69

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

http/client/client.go

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package client
22

33
import (
44
"bytes"
5+
"fmt"
56
"io/ioutil"
67
"net"
78
"net/http"
@@ -12,13 +13,22 @@ import (
1213
)
1314

1415
type httpClient struct {
15-
hc http.Client
16+
client http.Client
1617
}
1718

18-
//NewClient 创建一个带超时控制的http client.
19-
func New(timeout time.Duration) httpClient {
19+
type StatusError struct {
20+
Code int
21+
}
22+
23+
func (se *StatusError) Error() string {
24+
return fmt.Sprintf("HTTP Status %v", se.Code)
25+
}
26+
27+
//NewClient 创建一个带超时控制的http client, 单位秒.
28+
func New(ts int) httpClient {
29+
timeout := time.Duration(ts) * time.Second
2030
return httpClient{
21-
hc: http.Client{
31+
client: http.Client{
2232
Transport: &http.Transport{
2333
Dial: func(netw, addr string) (net.Conn, error) {
2434
c, err := net.DialTimeout(netw, addr, timeout)
@@ -38,51 +48,46 @@ func New(timeout time.Duration) httpClient {
3848
}
3949
}
4050

41-
func (c httpClient) do(method, url string, headers map[string]string, body *bytes.Buffer) ([]byte, int, error) {
42-
var req *http.Request
43-
var err error
44-
45-
//参数body就个指向结构体的指针(*bytes.Buffer),NewRequest的body参数是一个接口(io.Reader)
46-
if body == nil {
47-
req, err = http.NewRequest(method, url, nil)
48-
} else {
49-
req, err = http.NewRequest(method, url, body)
50-
}
51-
51+
func (c httpClient) do(method, url string, headers map[string]string, body []byte) ([]byte, error) {
52+
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
5253
if err != nil {
53-
return nil, http.StatusInternalServerError, err
54+
return nil, errors.Trace(err)
5455
}
5556

5657
for k, v := range headers {
5758
req.Header.Set(k, v)
5859
}
5960

60-
resp, err := c.hc.Do(req)
61+
resp, err := c.client.Do(req)
6162
if err != nil {
62-
return nil, 0, errors.Trace(err)
63+
return nil, errors.Trace(err)
6364
}
6465
defer resp.Body.Close()
6566

6667
data, err := ioutil.ReadAll(resp.Body)
6768
if err != nil {
68-
return nil, 0, errors.Trace(err)
69+
return nil, errors.Trace(err)
70+
}
71+
72+
if resp.StatusCode != http.StatusOK {
73+
return nil, errors.Trace(&StatusError{resp.StatusCode})
6974
}
7075

71-
return data, resp.StatusCode, nil
76+
return data, nil
7277
}
7378

74-
func (c httpClient) Get(url string, headers map[string]string, body *bytes.Buffer) ([]byte, int, error) {
79+
func (c httpClient) Get(url string, headers map[string]string, body []byte) ([]byte, error) {
7580
return c.do("GET", url, headers, body)
7681
}
7782

78-
func (c httpClient) POST(url string, headers map[string]string, body *bytes.Buffer) ([]byte, int, error) {
83+
func (c httpClient) POST(url string, headers map[string]string, body []byte) ([]byte, error) {
7984
return c.do("POST", url, headers, body)
8085
}
8186

82-
func (c httpClient) PUT(url string, headers map[string]string, body *bytes.Buffer) ([]byte, int, error) {
87+
func (c httpClient) PUT(url string, headers map[string]string, body []byte) ([]byte, error) {
8388
return c.do("PUT", url, headers, body)
8489
}
8590

86-
func (c httpClient) DELETE(url string, headers map[string]string, body *bytes.Buffer) ([]byte, int, error) {
91+
func (c httpClient) DELETE(url string, headers map[string]string, body []byte) ([]byte, error) {
8792
return c.do("DELETE", url, headers, body)
8893
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (i *index) GET(w http.ResponseWriter, req *http.Request) {
2222

2323
func testHTTPClient() {
2424
url := "http://127.0.0.1:9000/main/index/"
25-
buf, _, err := client.New(time.Second).Get(url, nil, nil)
25+
buf, err := client.New(1).Get(url, nil, nil)
2626
if err != nil {
2727
panic(err.Error())
2828
}

0 commit comments

Comments
 (0)