1+ import yaml from 'js-yaml' ;
12import {
23 isCSV ,
34 isFormData ,
89 isPlainObject ,
910 isText ,
1011 isXML ,
12+ isYAML ,
1113} from './contentTypeChecks' ;
1214import { json2xml } from './json2xml' ;
1315import { stringifyOpenAPI } from './stringifyOpenAPI' ;
@@ -154,7 +156,8 @@ ${headerString}${bodyString}`;
154156 label : 'Python' ,
155157 syntax : 'python' ,
156158 generate : ( { method, url, headers, body } ) => {
157- let code = 'import requests\n\n' ;
159+ const contentType = headers ?. [ 'Content-Type' ] ;
160+ let code = `${ isJSON ( contentType ) ? 'import json\n' : '' } import requests\n\n` ;
158161
159162 if ( body ) {
160163 const lines = BodyGenerators . getPythonBody ( body , headers ) ;
@@ -174,7 +177,6 @@ ${headerString}${bodyString}`;
174177 code += indent ( `headers=${ stringifyOpenAPI ( headers ) } ,\n` , 4 ) ;
175178 }
176179
177- const contentType = headers ?. [ 'Content-Type' ] ;
178180 if ( body ) {
179181 if ( body === 'files' ) {
180182 code += indent ( `files=${ body } \n` , 4 ) ;
@@ -256,6 +258,8 @@ const BodyGenerators = {
256258 } else if ( isPDF ( contentType ) ) {
257259 // We use --data-binary to avoid cURL converting newlines to \r\n
258260 body = `--data-binary '@${ String ( body ) } '` ;
261+ } else if ( isYAML ( contentType ) ) {
262+ body = `--data-binary $'${ yaml . dump ( body ) . replace ( / ' / g, '' ) . replace ( / \\ n / g, '\n' ) } '` ;
259263 } else {
260264 body = `--data '${ stringifyOpenAPI ( body , null , 2 ) . replace ( / \\ n / g, '\n' ) } '` ;
261265 }
@@ -325,6 +329,9 @@ const BodyGenerators = {
325329 code += indent ( convertBodyToXML ( body ) , 4 ) ;
326330 code += '`;\n\n' ;
327331 body = 'xml' ;
332+ } else if ( isYAML ( contentType ) ) {
333+ code += `const yamlBody = \`\n${ indent ( yaml . dump ( body ) , 4 ) } \`;\n\n` ;
334+ body = 'yamlBody' ;
328335 } else if ( isText ( contentType ) ) {
329336 body = stringifyOpenAPI ( body , null , 2 ) ;
330337 } else {
@@ -355,6 +362,9 @@ const BodyGenerators = {
355362 } else if ( isXML ( contentType ) ) {
356363 // Convert JSON to XML if needed
357364 body = JSON . stringify ( convertBodyToXML ( body ) ) ;
365+ } else if ( isYAML ( contentType ) ) {
366+ code += `yamlBody = \"\"\"\n${ indent ( yaml . dump ( body ) , 4 ) } \"\"\"\n\n` ;
367+ body = 'yamlBody' ;
358368 } else {
359369 body = stringifyOpenAPI (
360370 body ,
@@ -399,6 +409,7 @@ const BodyGenerators = {
399409 // Convert JSON to XML if needed
400410 return `"${ convertBodyToXML ( body ) } "` ;
401411 } ,
412+ yaml : ( ) => `"${ yaml . dump ( body ) . replace ( / " / g, '\\"' ) } "` ,
402413 csv : ( ) => `"${ stringifyOpenAPI ( body ) . replace ( / " / g, '' ) } "` ,
403414 default : ( ) => `${ stringifyOpenAPI ( body , null , 2 ) } ` ,
404415 } ;
@@ -407,6 +418,7 @@ const BodyGenerators = {
407418 if ( isFormUrlEncoded ( contentType ) ) return typeHandlers . formUrlEncoded ( ) ;
408419 if ( isText ( contentType ) ) return typeHandlers . text ( ) ;
409420 if ( isXML ( contentType ) ) return typeHandlers . xml ( ) ;
421+ if ( isYAML ( contentType ) ) return typeHandlers . yaml ( ) ;
410422 if ( isCSV ( contentType ) ) return typeHandlers . csv ( ) ;
411423
412424 return typeHandlers . default ( ) ;
0 commit comments