Skip to content

Commit cf3045a

Browse files
authored
Add Python support in code samples (#2453)
1 parent a679e72 commit cf3045a

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

.changeset/brave-camels-trade.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/react-openapi': minor
3+
---
4+
5+
Add Python support in Code Samples

packages/react-openapi/src/code-samples.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,25 @@ export const codeSampleGenerators: CodeSampleGenerator[] = [
6565
return lines.map((line, index) => (index > 0 ? indent(line, 2) : line)).join(separator);
6666
},
6767
},
68+
{
69+
id: 'python',
70+
label: 'Python',
71+
syntax: 'python',
72+
generate: ({ method, url, headers, body }) => {
73+
let code = 'import requests\n\n';
74+
code += `response = requests.${method.toLowerCase()}(\n`;
75+
code += indent(`"${url}",\n`, 4);
76+
if (headers) {
77+
code += indent(`headers=${JSON.stringify(headers)},\n`, 4);
78+
}
79+
if (body) {
80+
code += indent(`json=${JSON.stringify(body)}\n`, 4);
81+
}
82+
code += ')\n';
83+
code += `data = response.json()`;
84+
return code;
85+
},
86+
},
6887
];
6988

7089
function indent(code: string, spaces: number) {

0 commit comments

Comments
 (0)