Skip to content

Commit 2f7525d

Browse files
feat: make Python snippets simpler, clearer & more consistent (Kong#288)
Co-authored-by: Tim Perry <[email protected]>
1 parent 19f1b2f commit 2f7525d

19 files changed

Lines changed: 48 additions & 40 deletions

src/targets/python/helpers.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
/**
2-
* Create an string of given length filled with blank spaces
3-
*
4-
* @param length Length of the array to return
5-
* @param str String to pad out with
6-
*/
7-
function buildString(length: number, str: string) {
8-
return str.repeat(length);
9-
}
10-
111
/**
122
* Create a string corresponding to a Dictionary or Array literal representation with pretty option
133
* and indentation.
@@ -19,8 +9,8 @@ function concatValues(
199
indentation: string,
2010
indentLevel: number,
2111
) {
22-
const currentIndent = buildString(indentLevel, indentation);
23-
const closingBraceIndent = buildString(indentLevel - 1, indentation);
12+
const currentIndent = indentation.repeat(indentLevel);
13+
const closingBraceIndent = indentation.repeat(indentLevel - 1);
2414
const join = pretty ? `,\n${currentIndent}` : ', ';
2515
const openingBrace = concatType === 'object' ? '{' : '[';
2616
const closingBrace = concatType === 'object' ? '}' : ']';
@@ -30,7 +20,12 @@ function concatValues(
3020
join,
3121
)}\n${closingBraceIndent}${closingBrace}`;
3222
}
33-
return openingBrace + values.join(join) + closingBrace;
23+
24+
if (concatType === 'object' && values.length > 0) {
25+
return `${openingBrace} ${values.join(join)} ${closingBrace}`;
26+
}
27+
28+
return `${openingBrace}${values.join(join)}${closingBrace}`;
3429
}
3530

3631
/**

src/targets/python/requests/client.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { getHeaderName } from '../../../helpers/headers';
1313
import { Client } from '../../targets';
1414
import { literalRepresentation } from '../helpers';
1515

16+
const builtInMethods = ['HEAD', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'];
17+
1618
export interface RequestsOptions {
1719
pretty?: true;
1820
}
@@ -106,6 +108,12 @@ export const requests: Client<RequestsOptions> = {
106108
break;
107109

108110
default: {
111+
if (postData.mimeType === 'application/x-www-form-urlencoded' && postData.paramsObj) {
112+
push(`payload = ${literalRepresentation(postData.paramsObj, opts)}`);
113+
hasPayload = true;
114+
break;
115+
}
116+
109117
const payload = JSON.stringify(postData.text);
110118
if (payload) {
111119
push(`payload = ${payload}`);
@@ -144,7 +152,9 @@ export const requests: Client<RequestsOptions> = {
144152
}
145153

146154
// Construct request
147-
let request = `response = requests.request("${method}", url`;
155+
let request = builtInMethods.includes(method)
156+
? `response = requests.${method.toLowerCase()}(url`
157+
: `response = requests.request("${method}", url`;
148158

149159
if (hasPayload) {
150160
if (jsonPayload) {

src/targets/python/requests/fixtures/application-form-encoded.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
url = "http://mockbin.com/har"
44

5-
payload = "foo=bar&hello=world"
5+
payload = {
6+
"foo": "bar",
7+
"hello": "world"
8+
}
69
headers = {"content-type": "application/x-www-form-urlencoded"}
710

8-
response = requests.request("POST", url, data=payload, headers=headers)
11+
response = requests.post(url, data=payload, headers=headers)
912

1013
print(response.text)

src/targets/python/requests/fixtures/application-json.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
"number": 1,
77
"string": "f\"oo",
88
"arr": [1, 2, 3],
9-
"nested": {"a": "b"},
10-
"arr_mix": [1, "a", {"arr_mix_nested": {}}],
9+
"nested": { "a": "b" },
10+
"arr_mix": [1, "a", { "arr_mix_nested": {} }],
1111
"boolean": False
1212
}
1313
headers = {"content-type": "application/json"}
1414

15-
response = requests.request("POST", url, json=payload, headers=headers)
15+
response = requests.post(url, json=payload, headers=headers)
1616

1717
print(response.text)

src/targets/python/requests/fixtures/cookies.py

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

55
headers = {"cookie": "foo=bar; bar=baz"}
66

7-
response = requests.request("POST", url, headers=headers)
7+
response = requests.post(url, headers=headers)
88

99
print(response.text)

src/targets/python/requests/fixtures/full.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
querystring = {"foo":["bar","baz"],"baz":"abc","key":"value"}
66

7-
payload = "foo=bar"
7+
payload = { "foo": "bar" }
88
headers = {
99
"cookie": "foo=bar; bar=baz",
1010
"accept": "application/json",
1111
"content-type": "application/x-www-form-urlencoded"
1212
}
1313

14-
response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
14+
response = requests.post(url, data=payload, headers=headers, params=querystring)
1515

1616
print(response.text)

src/targets/python/requests/fixtures/headers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"x-bar": "Foo"
99
}
1010

11-
response = requests.request("GET", url, headers=headers)
11+
response = requests.get(url, headers=headers)
1212

1313
print(response.text)

src/targets/python/requests/fixtures/https.py

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

33
url = "https://mockbin.com/har"
44

5-
response = requests.request("GET", url)
5+
response = requests.get(url)
66

77
print(response.text)

src/targets/python/requests/fixtures/jsonObj-multiline.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
url = "http://mockbin.com/har"
44

5-
payload = {"foo": "bar"}
5+
payload = { "foo": "bar" }
66
headers = {"content-type": "application/json"}
77

8-
response = requests.request("POST", url, json=payload, headers=headers)
8+
response = requests.post(url, json=payload, headers=headers)
99

1010
print(response.text)

src/targets/python/requests/fixtures/jsonObj-null-value.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
url = "http://mockbin.com/har"
44

5-
payload = {"foo": None}
5+
payload = { "foo": None }
66
headers = {"content-type": "application/json"}
77

8-
response = requests.request("POST", url, json=payload, headers=headers)
8+
response = requests.post(url, json=payload, headers=headers)
99

1010
print(response.text)

0 commit comments

Comments
 (0)