Skip to content

Commit 9187334

Browse files
committed
Merge pull request #24 from montanaflynn/master
Add Python target
2 parents 810fa33 + b0fc974 commit 9187334

28 files changed

+356
-13
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ currently the following output [targets](/src/targets) are supported:
3434
- [CoHTTP](https://github.com/mirage/ocaml-cohttp)
3535
- PHP
3636
- [ext-curl](http://php.net/manual/en/book.curl.php)
37+
- Python
38+
- [Python 3](https://docs.python.org/3/library/http.client.html)
3739
- [Wget](https://www.gnu.org/software/wget/)
3840

3941
## Installation

src/targets/go/native.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,54 +44,57 @@ module.exports = function (source, options) {
4444

4545
code.push(')\n')
4646

47-
code.push('func main() {')
47+
code.push('func main() {\n')
4848

4949
// Create client
5050
if (opts.timeout > 0) {
5151
code.push('\tclient := http.Client{')
5252
code.push(util.format('\t\tTimeout: time.Duration(%s * time.Second),', opts.timeout))
53-
code.push('\t}')
53+
code.push('\t}\n')
5454
} else {
55-
code.push('\tclient := &http.Client{}')
55+
code.push('\tclient := &http.Client{}\n')
5656
}
5757

58-
code.push(util.format('\turl := "%s"', source.fullUrl))
58+
code.push(util.format('\turl := "%s"\n', source.fullUrl))
5959

6060
// If we have body content or not create the var and reader or nil
6161
if (source.postData.text) {
62-
code.push(util.format('\tpayload := strings.NewReader(%s)', JSON.stringify(source.postData.text)))
63-
code.push(util.format('\treq, %s := http.NewRequest("%s", url, payload)', errorPlaceholder, source.method))
62+
code.push(util.format('\tpayload := strings.NewReader(%s)\n', JSON.stringify(source.postData.text)))
63+
code.push(util.format('\treq, %s := http.NewRequest("%s", url, payload)\n', errorPlaceholder, source.method))
6464
} else {
65-
code.push(util.format('\treq, %s := http.NewRequest("%s", url, nil)', errorPlaceholder, source.method))
65+
code.push(util.format('\treq, %s := http.NewRequest("%s", url, nil)\n', errorPlaceholder, source.method))
6666
}
6767

6868
errorCheck()
6969

7070
// Add headers
71-
Object.keys(source.allHeaders).map(function (key) {
72-
code.push(util.format('\treq.Header.Add("%s", "%s")', key, source.allHeaders[key]))
73-
})
71+
if (Object.keys(source.allHeaders).length) {
72+
Object.keys(source.allHeaders).map(function (key) {
73+
code.push(util.format('\treq.Header.Add("%s", "%s")', key, source.allHeaders[key]))
74+
})
75+
code.push(null)
76+
}
7477

7578
// Make request
7679
code.push(util.format('\tres, %s := client.Do(req)', errorPlaceholder))
7780
errorCheck()
7881

7982
// Get Body
8083
if (opts.printBody) {
81-
code.push('\tdefer res.Body.Close()')
84+
code.push('\n\tdefer res.Body.Close()')
8285
code.push(util.format('\tbody, %s := ioutil.ReadAll(res.Body)', errorPlaceholder))
8386
errorCheck()
8487
}
8588

8689
// Print it
87-
code.push('\tfmt.Println(res)')
90+
code.push('\n\tfmt.Println(res)')
8891

8992
if (opts.printBody) {
9093
code.push('\tfmt.Println(string(body))')
9194
}
9295

9396
// End main block
94-
code.push('}')
97+
code.push('\n}')
9598

9699
return code.join('\n')
97100
}

src/targets/python/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
module.exports = require('require-directory')(module)

src/targets/python/info.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict'
2+
3+
module.exports = {
4+
key: 'python',
5+
title: 'Python',
6+
extname: '.py',
7+
default: 'python3'
8+
}

src/targets/python/python3.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'use strict'
2+
3+
var util = require('util')
4+
5+
module.exports = function (source, options) {
6+
// Start Request
7+
var code = []
8+
code.push('import http.client\n')
9+
10+
// Check which protocol to be used for the client connection
11+
var protocol = source.uriObj.protocol
12+
if (protocol === 'https:') {
13+
code.push(util.format('conn = http.client.HTTPSConnection("%s")\n', source.uriObj.host))
14+
} else {
15+
code.push(util.format('conn = http.client.HTTPConnection("%s")\n', source.uriObj.host))
16+
}
17+
18+
// Create payload string if it exists
19+
var payload = JSON.stringify(source.postData.text)
20+
if (payload) {
21+
code.push(util.format('payload = %s\n', payload))
22+
}
23+
24+
// Create Headers
25+
var header
26+
var headers = source.allHeaders
27+
var headerCount = Object.keys(headers).length
28+
if (headerCount === 1) {
29+
for (header in headers) {
30+
code.push(util.format('headers = { \'%s\': "%s" }\n', header, headers[header]))
31+
}
32+
} else if (headerCount > 1) {
33+
var headerLine
34+
var count = 1
35+
code.push('headers = {')
36+
for (header in headers) {
37+
if (count++ !== headerCount) {
38+
headerLine = util.format(' \'%s\': "%s",', header, headers[header])
39+
} else {
40+
headerLine = util.format(' \'%s\': "%s"', header, headers[header])
41+
}
42+
code.push(headerLine)
43+
}
44+
code.push(' }\n')
45+
}
46+
47+
// Make Request
48+
var method = source.method
49+
var path = source.uriObj.path
50+
if (payload && headerCount) {
51+
code.push(util.format('conn.request("%s", "%s", payload, headers)', method, path))
52+
} else if (payload && !headerCount) {
53+
code.push(util.format('conn.request("%s", "%s", payload)', method, path))
54+
} else if (!payload && headerCount) {
55+
code.push(util.format('conn.request("%s", "%s", headers = headers)', method, path))
56+
} else {
57+
code.push(util.format('conn.request("%s", "%s")', method, path))
58+
}
59+
60+
// Get Response
61+
code.push('\nres = conn.getresponse()')
62+
code.push('data = res.read()')
63+
code.push('\nprint(res.status)')
64+
code.push('print(data)')
65+
66+
// console.log(code)
67+
return code.join('\n')
68+
}
69+
70+
module.exports.info = {
71+
key: 'python3',
72+
title: 'http.client',
73+
link: 'https://docs.python.org/3/library/http.client.html',
74+
description: 'Python3 HTTP Client'
75+
}

test/fixtures/available-targets.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@
7474
}
7575
]
7676
},
77+
{
78+
"key": "python",
79+
"title": "Python",
80+
"extname": ".py",
81+
"default": "python3",
82+
"clients": [
83+
{
84+
"key": "python3",
85+
"title": "http.client",
86+
"link": "https://docs.python.org/3/library/http.client.html",
87+
"description": "Python3 HTTP Client"
88+
}
89+
]
90+
},
7791
{
7892
"key": "objc",
7993
"title": "Objective-C",

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,23 @@ import (
88
)
99

1010
func main() {
11+
1112
client := &http.Client{}
13+
1214
url := "http://mockbin.com/har"
15+
1316
payload := strings.NewReader("foo=bar&hello=world")
17+
1418
req, _ := http.NewRequest("POST", url, payload)
19+
1520
req.Header.Add("content-type", "application/x-www-form-urlencoded")
21+
1622
res, _ := client.Do(req)
23+
1724
defer res.Body.Close()
1825
body, _ := ioutil.ReadAll(res.Body)
26+
1927
fmt.Println(res)
2028
fmt.Println(string(body))
29+
2130
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,23 @@ import (
88
)
99

1010
func main() {
11+
1112
client := &http.Client{}
13+
1214
url := "http://mockbin.com/har"
15+
1316
payload := strings.NewReader("{\"number\": 1, \"string\": \"f\\\"oo\", \"arr\": [1, 2, 3], \"nested\": {\"a\": \"b\"}, \"arr_mix\": [1, \"a\", {\"arr_mix_nested\": {}}] }")
17+
1418
req, _ := http.NewRequest("POST", url, payload)
19+
1520
req.Header.Add("content-type", "application/json")
21+
1622
res, _ := client.Do(req)
23+
1724
defer res.Body.Close()
1825
body, _ := ioutil.ReadAll(res.Body)
26+
1927
fmt.Println(res)
2028
fmt.Println(string(body))
29+
2130
}

test/fixtures/output/go/native/cookies.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@ import (
77
)
88

99
func main() {
10+
1011
client := &http.Client{}
12+
1113
url := "http://mockbin.com/har"
14+
1215
req, _ := http.NewRequest("POST", url, nil)
16+
1317
req.Header.Add("cookie", "foo=bar; bar=baz")
18+
1419
res, _ := client.Do(req)
20+
1521
defer res.Body.Close()
1622
body, _ := ioutil.ReadAll(res.Body)
23+
1724
fmt.Println(res)
1825
fmt.Println(string(body))
26+
1927
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,25 @@ import (
88
)
99

1010
func main() {
11+
1112
client := &http.Client{}
13+
1214
url := "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value"
15+
1316
payload := strings.NewReader("foo=bar")
17+
1418
req, _ := http.NewRequest("POST", url, payload)
19+
1520
req.Header.Add("cookie", "foo=bar; bar=baz")
1621
req.Header.Add("accept", "application/json")
1722
req.Header.Add("content-type", "application/x-www-form-urlencoded")
23+
1824
res, _ := client.Do(req)
25+
1926
defer res.Body.Close()
2027
body, _ := ioutil.ReadAll(res.Body)
28+
2129
fmt.Println(res)
2230
fmt.Println(string(body))
31+
2332
}

0 commit comments

Comments
 (0)