Skip to content

Commit 096ebfb

Browse files
committed
Merge pull request Kong#21 from montanaflynn/master
Go: Add timeout, fix error checking bug, refactor
2 parents 9762f4c + 876db60 commit 096ebfb

8 files changed

Lines changed: 33 additions & 23 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ snippets/
128128
└── endpoint-3.js
129129
```
130130

131-
132131
### Module
133132

134133
```js
@@ -192,6 +191,7 @@ module.exports.info = {
192191
| --------------- | ------- | ------------------------------------------------------------------------ |
193192
| `errorChecking` | `false` | add error checking for request, response and body |
194193
| `printBody` | `true` | include code to print the body as a string |
194+
| `timeout` | `-1` | sets a request timeout in seconds (requires go 1.3+) |
195195

196196
### HTTPie
197197

‎src/targets/go/native.js‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
var util = require('util');
44

55
module.exports = function (source, options) {
6+
67
// Let's Go!
78
var code = [];
89

910
// Define Options
1011
var opts = util._extend({
1112
checkErrors: false,
12-
printBody: true
13+
printBody: true,
14+
timeout: -1
1315
}, options);
1416

1517
var errorPlaceholder = opts.checkErrors ? 'err' : '_';
@@ -27,6 +29,10 @@ module.exports = function (source, options) {
2729
code.push('import (');
2830
code.push('\t"fmt"');
2931

32+
if (opts.timeout > 0) {
33+
code.push('\t"time"');
34+
}
35+
3036
if (source.postData.text) {
3137
code.push('\t"strings"');
3238
}
@@ -36,22 +42,30 @@ module.exports = function (source, options) {
3642
if (opts.printBody) {
3743
code.push('\t"io/ioutil"');
3844
}
45+
3946
code.push(')\n');
4047

4148
code.push('func main() {');
4249

4350
// Create client
44-
code.push('\tclient := &http.Client{}');
51+
if (opts.timeout > 0) {
52+
code.push('\tclient := http.Client{');
53+
code.push(util.format('\t\tTimeout: time.Duration(%s * time.Second),', opts.timeout));
54+
code.push('\t}');
55+
} else {
56+
code.push('\tclient := &http.Client{}');
57+
}
58+
4559
code.push(util.format('\turl := "%s"', source.fullUrl));
4660

4761
// If we have body content or not create the var and reader or nil
4862
if (source.postData.text) {
49-
code.push('\tpayload := ' + JSON.stringify(source.postData.text));
50-
code.push(util.format('\treq, ' + errorPlaceholder + ' := http.NewRequest("%s", url, strings.NewReader(payload))', source.method));
63+
code.push(util.format('\tpayload := strings.NewReader(%s)', JSON.stringify(source.postData.text)));
64+
code.push(util.format('\treq, %s := http.NewRequest("%s", url, payload)', errorPlaceholder, source.method));
5165
} else {
5266
code.push(util.format('\treq, %s := http.NewRequest("%s", url, nil)', errorPlaceholder, source.method));
5367
}
54-
68+
5569
errorCheck();
5670

5771
// Add headers
@@ -66,14 +80,10 @@ module.exports = function (source, options) {
6680
// Get Body
6781
if (opts.printBody) {
6882
code.push('\tdefer res.Body.Close()');
69-
}
70-
71-
if (opts.printBody) {
7283
code.push(util.format('\tbody, %s := ioutil.ReadAll(res.Body)', errorPlaceholder));
84+
errorCheck();
7385
}
7486

75-
errorCheck();
76-
7787
// Print it
7888
code.push('\tfmt.Println(res)');
7989

‎test/fixtures/output/go/native/application-form-encoded.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
func main() {
1111
client := &http.Client{}
1212
url := "http://mockbin.com/har"
13-
payload := "foo=bar&hello=world"
14-
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
13+
payload := strings.NewReader("foo=bar&hello=world")
14+
req, _ := http.NewRequest("POST", url, payload)
1515
req.Header.Add("content-type", "application/x-www-form-urlencoded")
1616
res, _ := client.Do(req)
1717
defer res.Body.Close()

‎test/fixtures/output/go/native/application-json.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
func main() {
1111
client := &http.Client{}
1212
url := "http://mockbin.com/har"
13-
payload := "{\"number\": 1, \"string\": \"f\\\"oo\", \"arr\": [1, 2, 3], \"nested\": {\"a\": \"b\"}, \"arr_mix\": [1, \"a\", {\"arr_mix_nested\": {}}] }"
14-
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
13+
payload := strings.NewReader("{\"number\": 1, \"string\": \"f\\\"oo\", \"arr\": [1, 2, 3], \"nested\": {\"a\": \"b\"}, \"arr_mix\": [1, \"a\", {\"arr_mix_nested\": {}}] }")
14+
req, _ := http.NewRequest("POST", url, payload)
1515
req.Header.Add("content-type", "application/json")
1616
res, _ := client.Do(req)
1717
defer res.Body.Close()

‎test/fixtures/output/go/native/full.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
func main() {
1111
client := &http.Client{}
1212
url := "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value"
13-
payload := "foo=bar"
14-
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
13+
payload := strings.NewReader("foo=bar")
14+
req, _ := http.NewRequest("POST", url, payload)
1515
req.Header.Add("cookie", "foo=bar; bar=baz")
1616
req.Header.Add("accept", "application/json")
1717
req.Header.Add("content-type", "application/x-www-form-urlencoded")

‎test/fixtures/output/go/native/multipart-data.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
func main() {
1111
client := &http.Client{}
1212
url := "http://mockbin.com/har"
13-
payload := "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--"
14-
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
13+
payload := strings.NewReader("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--")
14+
req, _ := http.NewRequest("POST", url, payload)
1515
req.Header.Add("content-type", "multipart/form-data; boundary=---011000010111000001101001")
1616
res, _ := client.Do(req)
1717
defer res.Body.Close()

‎test/fixtures/output/go/native/multipart-file.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
func main() {
1111
client := &http.Client{}
1212
url := "http://mockbin.com/har"
13-
payload := "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--"
14-
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
13+
payload := strings.NewReader("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--")
14+
req, _ := http.NewRequest("POST", url, payload)
1515
req.Header.Add("content-type", "multipart/form-data; boundary=---011000010111000001101001")
1616
res, _ := client.Do(req)
1717
defer res.Body.Close()

‎test/fixtures/output/go/native/multipart-form-data.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
func main() {
1111
client := &http.Client{}
1212
url := "http://mockbin.com/har"
13-
payload := "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--"
14-
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
13+
payload := strings.NewReader("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--")
14+
req, _ := http.NewRequest("POST", url, payload)
1515
req.Header.Add("content-type", "multipart/form-data; boundary=---011000010111000001101001")
1616
res, _ := client.Do(req)
1717
defer res.Body.Close()

0 commit comments

Comments
 (0)