Skip to content

Commit 69008ff

Browse files
committed
Merge branch 'python-style-tweaks'
2 parents e3ef549 + 929be62 commit 69008ff

30 files changed

+93
-55
lines changed

src/targets/python/helpers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ function concatValues (concatType, values, pretty, indentation, indentLevel) {
2525

2626
if (pretty) {
2727
return openingBrace + '\n' + currentIndent + values.join(join) + '\n' + closingBraceIndent + closingBrace
28+
} else if (concatType === 'object' && values.length > 0) {
29+
return openingBrace + ' ' + values.join(join) + ' ' + closingBrace
2830
} else {
2931
return openingBrace + values.join(join) + closingBrace
3032
}

src/targets/python/python3.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ module.exports = function (source, options) {
5252
const headerCount = Object.keys(headers).length
5353
if (headerCount === 1) {
5454
for (const header in headers) {
55-
code.push('headers = { \'%s\': "%s" }', header, headers[header])
55+
code.push('headers = { "%s": "%s" }', header, headers[header])
5656
.blank()
5757
}
5858
} else if (headerCount > 1) {
@@ -62,9 +62,9 @@ module.exports = function (source, options) {
6262

6363
for (const header in headers) {
6464
if (count++ !== headerCount) {
65-
code.push(' \'%s\': "%s",', header, headers[header])
65+
code.push(' "%s": "%s",', header, headers[header])
6666
} else {
67-
code.push(' \'%s\': "%s"', header, headers[header])
67+
code.push(' "%s": "%s"', header, headers[header])
6868
}
6969
}
7070

src/targets/python/requests.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ const util = require('util')
1414
const CodeBuilder = require('../../helpers/code-builder')
1515
const helpers = require('./helpers')
1616

17+
const builtInMethods = [
18+
'HEAD',
19+
'GET',
20+
'POST',
21+
'PUT',
22+
'PATCH',
23+
'DELETE',
24+
'OPTIONS'
25+
]
26+
1727
module.exports = function (source, options) {
1828
const opts = Object.assign({
1929
indent: ' ',
@@ -34,7 +44,7 @@ module.exports = function (source, options) {
3444
// Construct query string
3545
let qs
3646
if (Object.keys(source.queryObj).length) {
37-
qs = 'querystring = ' + JSON.stringify(source.queryObj)
47+
qs = 'querystring = ' + helpers.literalRepresentation(source.queryObj, opts)
3848

3949
code.push(qs)
4050
.blank()
@@ -52,6 +62,14 @@ module.exports = function (source, options) {
5262
}
5363
break
5464

65+
case 'application/x-www-form-urlencoded':
66+
if (source.postData.paramsObj) {
67+
code.push('payload = %s', helpers.literalRepresentation(source.postData.paramsObj, opts))
68+
hasPayload = true
69+
break
70+
}
71+
// Otherwise, fall through to treat as plain text:
72+
5573
default: {
5674
const payload = JSON.stringify(source.postData.text)
5775
if (payload) {
@@ -67,7 +85,7 @@ module.exports = function (source, options) {
6785

6886
if (headerCount === 1) {
6987
for (const header in headers) {
70-
code.push('headers = {"%s": "%s"}', header, headers[header])
88+
code.push('headers = { "%s": "%s" }', header, headers[header])
7189
.blank()
7290
}
7391
} else if (headerCount > 1) {
@@ -89,7 +107,10 @@ module.exports = function (source, options) {
89107

90108
// Construct request
91109
const method = source.method
92-
let request = util.format('response = requests.request("%s", url', method)
110+
111+
let request = builtInMethods.includes(method)
112+
? util.format('response = requests.%s(url', method.toLowerCase())
113+
: util.format('response = requests.request("%s", url', method)
93114

94115
if (hasPayload) {
95116
if (jsonPayload) {

test/fixtures/output/python/python3/application-form-encoded.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
payload = "foo=bar&hello=world"
66

7-
headers = { 'content-type': "application/x-www-form-urlencoded" }
7+
headers = { "content-type": "application/x-www-form-urlencoded" }
88

99
conn.request("POST", "/har", payload, headers)
1010

test/fixtures/output/python/python3/application-json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
payload = "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}"
66

7-
headers = { 'content-type': "application/json" }
7+
headers = { "content-type": "application/json" }
88

99
conn.request("POST", "/har", payload, headers)
1010

test/fixtures/output/python/python3/cookies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
conn = http.client.HTTPConnection("mockbin.com")
44

5-
headers = { 'cookie': "foo=bar; bar=baz" }
5+
headers = { "cookie": "foo=bar; bar=baz" }
66

77
conn.request("POST", "/har", headers=headers)
88

test/fixtures/output/python/python3/full.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
payload = "foo=bar"
66

77
headers = {
8-
'cookie': "foo=bar; bar=baz",
9-
'accept': "application/json",
10-
'content-type': "application/x-www-form-urlencoded"
8+
"cookie": "foo=bar; bar=baz",
9+
"accept": "application/json",
10+
"content-type": "application/x-www-form-urlencoded"
1111
}
1212

1313
conn.request("POST", "/har?foo=bar&foo=baz&baz=abc&key=value", payload, headers)

test/fixtures/output/python/python3/headers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
conn = http.client.HTTPConnection("mockbin.com")
44

55
headers = {
6-
'accept': "application/json",
7-
'x-foo': "Bar"
6+
"accept": "application/json",
7+
"x-foo": "Bar"
88
}
99

1010
conn.request("GET", "/har", headers=headers)

test/fixtures/output/python/python3/jsonObj-multiline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
payload = "{\n \"foo\": \"bar\"\n}"
66

7-
headers = { 'content-type': "application/json" }
7+
headers = { "content-type": "application/json" }
88

99
conn.request("POST", "/har", payload, headers)
1010

test/fixtures/output/python/python3/jsonObj-null-value.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
payload = "{\"foo\":null}"
66

7-
headers = { 'content-type': "application/json" }
7+
headers = { "content-type": "application/json" }
88

99
conn.request("POST", "/har", payload, headers)
1010

0 commit comments

Comments
 (0)