Add Python support in code samples (#2453)

This commit is contained in:
vibhanshub
2024-09-06 14:29:50 +01:00
committed by GitHub
parent a679e7283f
commit cf3045a718
2 changed files with 24 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@gitbook/react-openapi': minor
---
Add Python support in Code Samples
@@ -65,6 +65,25 @@ export const codeSampleGenerators: CodeSampleGenerator[] = [
return lines.map((line, index) => (index > 0 ? indent(line, 2) : line)).join(separator);
},
},
{
id: 'python',
label: 'Python',
syntax: 'python',
generate: ({ method, url, headers, body }) => {
let code = 'import requests\n\n';
code += `response = requests.${method.toLowerCase()}(\n`;
code += indent(`"${url}",\n`, 4);
if (headers) {
code += indent(`headers=${JSON.stringify(headers)},\n`, 4);
}
if (body) {
code += indent(`json=${JSON.stringify(body)}\n`, 4);
}
code += ')\n';
code += `data = response.json()`;
return code;
},
},
];
function indent(code: string, spaces: number) {