@@ -32,47 +32,38 @@ type Response struct {
32
32
}
33
33
34
34
// Do will excute the request with the default http client
35
- func Do (req * http.Request ) (Response , error ) {
35
+ func Do (req * http.Request ) (* Response , error ) {
36
36
return ClientDo (DefaultHTTPClient , req )
37
37
}
38
38
39
39
// ClientDo will excute the request with a specific http client
40
- func ClientDo (client * http.Client , req * http.Request , streamResp ... bool ) (Response , error ) {
41
- // TODO: process can be canceled by context
42
- //ctx := req.Context()
43
- //select {
44
- //}
40
+ func ClientDo (client * http.Client , req * http.Request , streamResp ... bool ) (* Response , error ) {
45
41
isStream := false
46
42
if len (streamResp ) > 0 && streamResp [0 ] == true {
47
43
isStream = true
48
44
}
49
45
resp , err := client .Do (req )
50
- if ! isStream && resp != nil {
51
- defer resp .Body .Close ()
52
- }
53
46
if err != nil {
54
- if isStream && resp != nil {
55
- resp .Body .Close ()
56
- }
57
- return Response {}, fmt .Errorf (
58
- "do request(%s) failed: %s" , req .URL , err )
47
+ return nil , err
59
48
}
60
49
61
50
// return a stream
62
51
if isStream {
63
- return Response {
52
+ return & Response {
64
53
Status : resp .StatusCode ,
65
54
Header : resp .Header ,
66
55
BodyStream : resp .Body ,
67
56
}, nil
68
57
}
58
+
59
+ defer resp .Body .Close ()
69
60
// return the data
70
61
body , err := ioutil .ReadAll (resp .Body )
71
62
if err != nil {
72
- return Response {} , fmt .Errorf (
63
+ return nil , fmt .Errorf (
73
64
"read response body failed:%v" , err )
74
65
}
75
- return Response {
66
+ return & Response {
76
67
Status : resp .StatusCode ,
77
68
Header : resp .Header ,
78
69
Body : body ,
@@ -105,27 +96,27 @@ func NewRequest(ctx context.Context, method string, url string, headers map[stri
105
96
return req , nil
106
97
}
107
98
108
- func doRequest (ctx context.Context , method string , url string , headers map [string ]string , query url.Values , body io.Reader ) (Response , error ) {
99
+ func doRequest (ctx context.Context , method string , url string , headers map [string ]string , query url.Values , body io.Reader ) (* Response , error ) {
109
100
req , err := NewRequest (ctx , method , url , headers , query , body )
110
101
if err != nil {
111
- return Response {} , err
102
+ return nil , err
112
103
}
113
104
114
105
return Do (req )
115
106
}
116
107
117
108
// Get will get remote data with custom headers
118
- func Get (ctx context.Context , url string , headers map [string ]string , query url.Values ) (Response , error ) {
109
+ func Get (ctx context.Context , url string , headers map [string ]string , query url.Values ) (* Response , error ) {
119
110
return doRequest (ctx , "GET" , url , headers , query , nil )
120
111
}
121
112
122
113
// Post will create remote resource
123
- func Post (ctx context.Context , url string , headers map [string ]string , query url.Values , body io.Reader ) (Response , error ) {
114
+ func Post (ctx context.Context , url string , headers map [string ]string , query url.Values , body io.Reader ) (* Response , error ) {
124
115
return doRequest (ctx , "POST" , url , headers , query , body )
125
116
}
126
117
127
118
// PostForm will create remote resource with x-www-form-urlencoded format data
128
- func PostForm (ctx context.Context , url string , form url.Values ) (Response , error ) {
119
+ func PostForm (ctx context.Context , url string , form url.Values ) (* Response , error ) {
129
120
return Post (ctx ,
130
121
url ,
131
122
map [string ]string {"Content-Type" : "application/x-www-form-urlencoded" },
@@ -134,16 +125,16 @@ func PostForm(ctx context.Context, url string, form url.Values) (Response, error
134
125
}
135
126
136
127
// Put will update a remote resource
137
- func Put (ctx context.Context , url string , headers map [string ]string , query url.Values , body io.Reader ) (Response , error ) {
128
+ func Put (ctx context.Context , url string , headers map [string ]string , query url.Values , body io.Reader ) (* Response , error ) {
138
129
return doRequest (ctx , "PUT" , url , headers , query , body )
139
130
}
140
131
141
132
// Patch will partially update a remote resource
142
- func Patch (ctx context.Context , url string , headers map [string ]string , query url.Values , body io.Reader ) (Response , error ) {
133
+ func Patch (ctx context.Context , url string , headers map [string ]string , query url.Values , body io.Reader ) (* Response , error ) {
143
134
return doRequest (ctx , "PATCH" , url , headers , query , body )
144
135
}
145
136
146
137
// Delete will delete remote resource
147
- func Delete (ctx context.Context , url string , headers map [string ]string , query url.Values ) (Response , error ) {
138
+ func Delete (ctx context.Context , url string , headers map [string ]string , query url.Values ) (* Response , error ) {
148
139
return doRequest (ctx , "DELETE" , url , headers , query , nil )
149
140
}
0 commit comments