Skip to content

Commit 722a963

Browse files
committed
Added support for PUT, PATCH and DELETE as well as reworked some internal APIs
Signed-off-by: Levi Gross <[email protected]>
1 parent 69c1724 commit 722a963

9 files changed

+106
-65
lines changed

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
language: go
22

33
go:
4-
# - 1.0 These won't work with coveralls
5-
# - 1.1
4+
# - 1.0 This won't work with coveralls
5+
# - 1.1 This won't work with coveralls
66
# - 1.2 This won't work with our XML wonderfulness
77
- 1.3
88
- 1.4

base.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,21 @@ func Get(url string, ro *RequestOptions) chan *Response {
99
return doRequest("GET", url, ro)
1010
}
1111

12-
func Put(url string, ro *RequestOptions) chan *Response { return nil }
12+
func Put(url string, ro *RequestOptions) chan *Response {
13+
return doRequest("PUT", url, ro)
14+
}
15+
16+
func Patch(url string, ro *RequestOptions) chan *Response {
17+
return doRequest("PATCH", url, ro)
18+
}
19+
20+
func Delete(url string, ro *RequestOptions) chan *Response {
21+
return doRequest("DELETE", url, ro)
22+
}
1323

1424
func Post(url string, ro *RequestOptions) chan *Response {
1525
return doRequest("POST", url, ro)
1626
}
17-
func Delete(url string, ro *RequestOptions) chan *Response { return nil }
1827

1928
func Head(url string, ro *RequestOptions) chan *Response {
2029
return doRequest("HEAD", url, ro)

base_delete_test.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ import (
55
)
66

77
func TestBasicDeleteRequest(t *testing.T) {
8-
resp := Delete("http://httpbin.org/get", nil)
9-
if resp != nil {
10-
t.Error("It is time to start writing the DELETE tests")
8+
resp := <-Delete("http://httpbin.org/delete", nil)
9+
10+
if resp.Error != nil {
11+
t.Error("Unable to make request", resp.Error)
12+
}
13+
14+
if resp.Ok != true {
15+
t.Error("Request did not return OK")
1116
}
1217
}

base_patch_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package grequests
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestBasicPatchRequest(t *testing.T) {
8+
resp := <-Patch("http://httpbin.org/patch", nil)
9+
10+
if resp.Error != nil {
11+
t.Error("Unable to make request", resp.Error)
12+
}
13+
14+
if resp.Ok != true {
15+
t.Error("Request did not return OK")
16+
}
17+
}

base_post_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func verifyOkPostResponse(resp *Response, t *testing.T) *BasicPostResponse {
245245
}
246246

247247
if myJsonStruct.Form.One != "Two" {
248-
t.Error("Invalid post response: ", myJsonStruct.Form.One)
248+
t.Errorf("Invalid post response: %#v", myJsonStruct.Form)
249249
}
250250

251251
if resp.Bytes() != nil {

base_put_test.go

+31-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,37 @@ import (
55
)
66

77
func TestBasicPutRequest(t *testing.T) {
8-
resp := Put("http://httpbin.org/get", nil)
9-
if resp != nil {
10-
t.Error("It is time to start writing the PUT tests")
8+
resp := <-Put("http://httpbin.org/put", nil)
9+
10+
if resp.Error != nil {
11+
t.Error("Unable to make request", resp.Error)
12+
}
13+
14+
if resp.Ok != true {
15+
t.Error("Request did not return OK")
16+
}
17+
18+
}
19+
20+
func TestBasicPutUploadRequest(t *testing.T) {
21+
fd, err := FileUploadFromDisk("test_files/mypassword")
22+
23+
if err != nil {
24+
t.Error("Unable to open file: ", err)
25+
}
26+
27+
resp := <-Put("http://httpbin.org/put",
28+
&RequestOptions{
29+
File: fd,
30+
Data: map[string]string{"One": "Two"},
31+
})
32+
33+
if resp.Error != nil {
34+
t.Error("Unable to make request", resp.Error)
35+
}
36+
37+
if resp.Ok != true {
38+
t.Error("Request did not return OK")
1139
}
1240

1341
}

request.go

+34-26
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"encoding/xml"
88
"io"
9+
"mime"
910
"mime/multipart"
1011
"net/http"
1112
"net/url"
@@ -65,22 +66,14 @@ func buildRequest(httpMethod, url string, ro *RequestOptions) (*http.Response, e
6566
}
6667
// Create our own HTTP client
6768
httpClient := buildHTTPClient(ro)
69+
6870
// Build our URL
6971
if ro.Params != nil {
7072
url = buildUrlParams(url, ro.Params)
7173
}
7274

7375
// Build the request
74-
var (
75-
req *http.Request
76-
err error
77-
)
78-
79-
if IsIdempotentMethod(httpMethod) {
80-
req, err = http.NewRequest(httpMethod, url, nil)
81-
} else {
82-
req, err = buildNonIdempotentRequest(httpMethod, url, ro)
83-
}
76+
req, err := buildHTTPRequest(httpMethod, url, ro)
8477

8578
if err != nil {
8679
return nil, err
@@ -92,16 +85,7 @@ func buildRequest(httpMethod, url string, ro *RequestOptions) (*http.Response, e
9285
return httpClient.Do(req)
9386
}
9487

95-
func buildNonIdempotentRequest(httpMethod, userUrl string, ro *RequestOptions) (*http.Request, error) {
96-
if httpMethod == "POST" {
97-
return buildPostRequest(httpMethod, userUrl, ro)
98-
}
99-
100-
return nil, nil // Placeholder
101-
102-
}
103-
104-
func buildPostRequest(httpMethod, userUrl string, ro *RequestOptions) (*http.Request, error) {
88+
func buildHTTPRequest(httpMethod, userUrl string, ro *RequestOptions) (*http.Request, error) {
10589
if ro.Json != nil {
10690
return createBasicJsonRequest(httpMethod, userUrl, ro)
10791
}
@@ -110,12 +94,34 @@ func buildPostRequest(httpMethod, userUrl string, ro *RequestOptions) (*http.Req
11094
return createBasicXmlRequest(httpMethod, userUrl, ro)
11195
}
11296

113-
if ro.File == nil {
114-
return createBasicPostRequest(httpMethod, userUrl, ro)
97+
if ro.File != nil {
98+
return createFileUploadRequest(httpMethod, userUrl, ro)
11599
}
116100

117-
// Our only other option is a multipart POST
118-
return createMultiPartPostRequest(httpMethod, userUrl, ro)
101+
if ro.Data != nil {
102+
return createBasicRequest(httpMethod, userUrl, ro)
103+
}
104+
105+
return http.NewRequest(httpMethod, userUrl, nil)
106+
}
107+
108+
func createFileUploadRequest(httpMethod, userUrl string, ro *RequestOptions) (*http.Request, error) {
109+
if httpMethod == "POST" {
110+
return createMultiPartPostRequest(httpMethod, userUrl, ro)
111+
}
112+
113+
// This may be a PUT or PATCH request so we will just put the raw io.ReadCloser in the request body
114+
115+
req, err := http.NewRequest(httpMethod, userUrl, ro.File.FileContents)
116+
117+
if err != nil {
118+
return nil, err
119+
}
120+
121+
req.Header.Set("Content-Type", mime.TypeByExtension(ro.File.FileName))
122+
123+
return req, nil
124+
119125
}
120126

121127
func createBasicXmlRequest(httpMethod, userUrl string, ro *RequestOptions) (*http.Request, error) {
@@ -187,7 +193,7 @@ func createBasicJsonRequest(httpMethod, userUrl string, ro *RequestOptions) (*ht
187193
return req, nil
188194

189195
}
190-
func createBasicPostRequest(httpMethod, userUrl string, ro *RequestOptions) (*http.Request, error) {
196+
func createBasicRequest(httpMethod, userUrl string, ro *RequestOptions) (*http.Request, error) {
191197

192198
req, err := http.NewRequest(httpMethod, userUrl, strings.NewReader(encodePostValues(ro.Data)))
193199
if err != nil {
@@ -206,7 +212,7 @@ func encodePostValues(postValues map[string]string) string {
206212
urlValues.Set(key, value)
207213
}
208214

209-
return urlValues.Encode() // This will sort and encode all of the string values
215+
return urlValues.Encode() // This will sort all of the string values
210216
}
211217

212218
func buildHTTPClient(ro *RequestOptions) *http.Client {
@@ -256,6 +262,8 @@ func addHTTPHeaders(ro *RequestOptions, req *http.Request) {
256262

257263
if ro.UserAgent != "" {
258264
req.Header.Set("User-Agent", ro.UserAgent)
265+
} else {
266+
req.Header.Set("User-Agent", localUserAgent)
259267
}
260268

261269
if ro.Auth != nil {

utils.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@ package grequests
22

33
import "io"
44

5-
var (
6-
idempotentHTTPMethods = map[string]bool{"GET": true, "HEAD": true, "OPTIONS": true}
5+
const (
6+
localUserAgent = "GRequests 0.1"
77
)
88

99
type XMLCharDecoder func(charset string, input io.Reader) (io.Reader, error)
10-
11-
func IsIdempotentMethod(httpMethod string) bool {
12-
_, ok := idempotentHTTPMethods[httpMethod]
13-
return ok
14-
}

utils_test.go

-21
Original file line numberDiff line numberDiff line change
@@ -1,22 +1 @@
11
package grequests
2-
3-
import "testing"
4-
5-
func TestIsIdempotentMethod(t *testing.T) {
6-
if IsIdempotentMethod("POST") != false {
7-
t.Error("POST is not idempotent")
8-
}
9-
10-
if IsIdempotentMethod("GET") != true {
11-
t.Error("GET is idempotent")
12-
}
13-
14-
if IsIdempotentMethod("OPTIONS") != true {
15-
t.Error("OPTIONS is idempotent")
16-
}
17-
18-
if IsIdempotentMethod("HEAD") != true {
19-
t.Error("HEAD is idempotent")
20-
}
21-
22-
}

0 commit comments

Comments
 (0)