11var _ = require ( '../lodash' ) ,
22 sanitize = require ( './sanitize' ) . sanitize ,
3+ trueToken = '__PYTHON#%0True__' ,
4+ falseToken = '__PYTHON#%0False__' ,
5+ nullToken = '__PYTHON#%0NULL__' ,
36 contentTypeHeaderMap = {
47 'aac' : 'audio/aac' ,
58 'abw' : 'application/x-abiword' ,
@@ -74,15 +77,47 @@ var _ = require('../lodash'),
7477 '7-zip' : 'application/x-7z-compressed'
7578 } ;
7679
80+ /**
81+ * Convert true, false and null to Python equivalent True, False and None
82+ *
83+ * @param {String } key
84+ * @param {Object } value
85+ */
86+ function replacer ( key , value ) {
87+ if ( typeof value === 'boolean' ) {
88+ return value ? trueToken : falseToken ;
89+ }
90+ else if ( value === null ) {
91+ return nullToken ;
92+ }
93+ return value ;
94+ }
95+
96+ /**
97+ * Convert JSON into a valid Python dict
98+ * The "true", "false" and "null" tokens are not valid in Python
99+ * so we need to convert them to "True", "False" and "None"
100+ *
101+ * @param {Object } jsonBody - JSON object to be converted
102+ * @param {Number } indentCount - Number of spaces to insert at each indentation level
103+ */
104+ function pythonify ( jsonBody , indentCount ) {
105+ return JSON . stringify ( jsonBody , replacer , indentCount )
106+ . replace ( new RegExp ( `"${ trueToken } "` , 'g' ) , 'True' )
107+ . replace ( new RegExp ( `"${ falseToken } "` , 'g' ) , 'False' )
108+ . replace ( new RegExp ( `"${ nullToken } "` , 'g' ) , 'None' ) ;
109+ }
110+
77111/**
78112 * Used to parse the body of the postman SDK-request and return in the desired format
79113 *
80114 * @param {Object } request - postman SDK-request object
81115 * @param {String } indentation - used for indenting snippet's structure
82116 * @param {Boolean } bodyTrim - whether to trim request body fields
117+ * @param {String } contentType - content-type of body
83118 * @returns {String } - request body
84119 */
85- module . exports = function ( request , indentation , bodyTrim ) {
120+ module . exports = function ( request , indentation , bodyTrim , contentType ) {
86121 // used to check whether body is present in the request or not
87122 if ( request . body ) {
88123 var requestBody = '' ,
@@ -92,14 +127,25 @@ module.exports = function (request, indentation, bodyTrim) {
92127
93128 switch ( request . body . mode ) {
94129 case 'raw' :
95- if ( ! _ . isEmpty ( request . body [ request . body . mode ] ) ) {
96- requestBody += `payload=${ sanitize ( request . body [ request . body . mode ] ,
97- request . body . mode , bodyTrim ) } \n`;
130+ if ( _ . isEmpty ( request . body [ request . body . mode ] ) ) {
131+ requestBody = 'payload = {}\n' ;
98132 }
99- else {
100- requestBody = 'payload={}\n' ;
133+ // Match any application type whose underlying structure is json
134+ // For example application/vnd.api+json
135+ // All of them have +json as suffix
136+ if ( contentType && ( contentType === 'application/json' || contentType . match ( / \+ j s o n $ / ) ) ) {
137+ try {
138+ let jsonBody = JSON . parse ( request . body [ request . body . mode ] ) ;
139+ return `payload = json.dumps(${ pythonify ( jsonBody , indentation . length ) } )\n` ;
140+
141+ }
142+ catch ( error ) {
143+ // Do nothing
144+ }
101145 }
102- return requestBody ;
146+ return `payload = ${ sanitize ( request . body [ request . body . mode ] ,
147+ request . body . mode , bodyTrim ) } \n`;
148+
103149 case 'graphql' :
104150 // eslint-disable-next-line no-case-declarations
105151 let query = request . body [ request . body . mode ] . query ,
0 commit comments