1
+ import yaml from 'js-yaml' ;
1
2
import {
2
3
isCSV ,
3
4
isFormData ,
8
9
isPlainObject ,
9
10
isText ,
10
11
isXML ,
12
+ isYAML ,
11
13
} from './contentTypeChecks' ;
12
14
import { json2xml } from './json2xml' ;
13
15
import { stringifyOpenAPI } from './stringifyOpenAPI' ;
@@ -154,7 +156,8 @@ ${headerString}${bodyString}`;
154
156
label : 'Python' ,
155
157
syntax : 'python' ,
156
158
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` ;
158
161
159
162
if ( body ) {
160
163
const lines = BodyGenerators . getPythonBody ( body , headers ) ;
@@ -174,7 +177,6 @@ ${headerString}${bodyString}`;
174
177
code += indent ( `headers=${ stringifyOpenAPI ( headers ) } ,\n` , 4 ) ;
175
178
}
176
179
177
- const contentType = headers ?. [ 'Content-Type' ] ;
178
180
if ( body ) {
179
181
if ( body === 'files' ) {
180
182
code += indent ( `files=${ body } \n` , 4 ) ;
@@ -256,6 +258,8 @@ const BodyGenerators = {
256
258
} else if ( isPDF ( contentType ) ) {
257
259
// We use --data-binary to avoid cURL converting newlines to \r\n
258
260
body = `--data-binary '@${ String ( body ) } '` ;
261
+ } else if ( isYAML ( contentType ) ) {
262
+ body = `--data-binary $'${ yaml . dump ( body ) . replace ( / ' / g, '' ) . replace ( / \\ n / g, '\n' ) } '` ;
259
263
} else {
260
264
body = `--data '${ stringifyOpenAPI ( body , null , 2 ) . replace ( / \\ n / g, '\n' ) } '` ;
261
265
}
@@ -325,6 +329,9 @@ const BodyGenerators = {
325
329
code += indent ( convertBodyToXML ( body ) , 4 ) ;
326
330
code += '`;\n\n' ;
327
331
body = 'xml' ;
332
+ } else if ( isYAML ( contentType ) ) {
333
+ code += `const yamlBody = \`\n${ indent ( yaml . dump ( body ) , 4 ) } \`;\n\n` ;
334
+ body = 'yamlBody' ;
328
335
} else if ( isText ( contentType ) ) {
329
336
body = stringifyOpenAPI ( body , null , 2 ) ;
330
337
} else {
@@ -355,6 +362,9 @@ const BodyGenerators = {
355
362
} else if ( isXML ( contentType ) ) {
356
363
// Convert JSON to XML if needed
357
364
body = JSON . stringify ( convertBodyToXML ( body ) ) ;
365
+ } else if ( isYAML ( contentType ) ) {
366
+ code += `yamlBody = \"\"\"\n${ indent ( yaml . dump ( body ) , 4 ) } \"\"\"\n\n` ;
367
+ body = 'yamlBody' ;
358
368
} else {
359
369
body = stringifyOpenAPI (
360
370
body ,
@@ -399,6 +409,7 @@ const BodyGenerators = {
399
409
// Convert JSON to XML if needed
400
410
return `"${ convertBodyToXML ( body ) } "` ;
401
411
} ,
412
+ yaml : ( ) => `"${ yaml . dump ( body ) . replace ( / " / g, '\\"' ) } "` ,
402
413
csv : ( ) => `"${ stringifyOpenAPI ( body ) . replace ( / " / g, '' ) } "` ,
403
414
default : ( ) => `${ stringifyOpenAPI ( body , null , 2 ) } ` ,
404
415
} ;
@@ -407,6 +418,7 @@ const BodyGenerators = {
407
418
if ( isFormUrlEncoded ( contentType ) ) return typeHandlers . formUrlEncoded ( ) ;
408
419
if ( isText ( contentType ) ) return typeHandlers . text ( ) ;
409
420
if ( isXML ( contentType ) ) return typeHandlers . xml ( ) ;
421
+ if ( isYAML ( contentType ) ) return typeHandlers . yaml ( ) ;
410
422
if ( isCSV ( contentType ) ) return typeHandlers . csv ( ) ;
411
423
412
424
return typeHandlers . default ( ) ;
0 commit comments