1212
1313const CodeBuilder = require ( '../../helpers/code-builder' )
1414
15- // Based off org.springframework.http.HttpMethod
1615const standardMethods = [ 'GET' , 'HEAD' , 'POST' , 'PUT' , 'PATCH' , 'DELETE' , 'OPTIONS' , 'TRACE' ]
1716
18- // Based off org.springframework.http.MediaType
1917const standardMediaTypes = {
2018 'application/atom+xml' : 'APPLICATION_ATOM_XML' ,
2119 'application/cbor' : 'APPLICATION_CBOR' ,
@@ -45,6 +43,20 @@ const standardMediaTypes = {
4543 'text/xml' : 'TEXT_XML'
4644}
4745
46+ const jsonMimeTypes = [
47+ 'application/json' ,
48+ 'text/json' ,
49+ 'text/x-json' ,
50+ 'application/x-json'
51+ ]
52+
53+ const multipartMimeTypes = [
54+ 'multipart/form-data' ,
55+ 'multipart/mixed' ,
56+ 'multipart/related' ,
57+ 'multipart/alternative'
58+ ]
59+
4860module . exports = function ( source , options ) {
4961 const opts = Object . assign ( {
5062 indent : ' ' ,
@@ -56,6 +68,43 @@ module.exports = function (source, options) {
5668 code . push ( 'RestClient restClient = RestClient.create();' )
5769 . blank ( )
5870
71+ if ( source . postData && source . postData . mimeType === 'application/x-www-form-urlencoded' && source . postData . params ) {
72+ code . push ( 'MultiValueMap<String, String> formDataMap = new LinkedMultiValueMap<>();' )
73+ source . postData . params . forEach ( function ( param ) {
74+ code . push ( 'formDataMap.add("%qd", "%qd");' , param . name , param . value )
75+ } )
76+ code . blank ( )
77+ }
78+
79+ if ( source . postData && multipartMimeTypes . includes ( source . postData . mimeType ) && source . postData . params ) {
80+ code . push ( 'MultipartBodyBuilder multipartBuilder = new MultipartBodyBuilder();' )
81+
82+ source . postData . params . forEach ( function ( param ) {
83+ if ( param . fileName ) {
84+ if ( param . value ) {
85+ code . push ( 'multipartBuilder.part("%s", "%qd")' , param . name , param . value )
86+ code . push ( 1 , '.filename("%s")' , param . fileName )
87+ } else {
88+ code . push ( 'multipartBuilder.part("%s", new FileSystemResource("%s"))' , param . name , param . fileName )
89+ }
90+
91+ if ( param . contentType ) {
92+ const mediaTypeConstant = standardMediaTypes [ param . contentType ]
93+ if ( mediaTypeConstant ) {
94+ code . push ( 1 , '.contentType(MediaType.%s);' , mediaTypeConstant )
95+ } else {
96+ code . push ( 1 , '.contentType(MediaType.parseMediaType("%s"));' , param . contentType )
97+ }
98+ } else {
99+ code . push ( 1 , ';' )
100+ }
101+ } else {
102+ code . push ( 'multipartBuilder.part("%s", "%qd");' , param . name , param . value || '' )
103+ }
104+ } )
105+ code . blank ( )
106+ }
107+
59108 code . push ( 'ResponseEntity<%s> response = restClient' , opts . entityClass )
60109
61110 if ( standardMethods . includes ( source . method . toUpperCase ( ) ) ) {
@@ -91,21 +140,30 @@ module.exports = function (source, options) {
91140 const headers = Object . keys ( source . headersObj )
92141 if ( headers . length ) {
93142 headers . forEach ( function ( key ) {
94- code . push ( 1 , '.header("%s", "%qd")' , key , source . headersObj [ key ] )
143+ if ( key . toLowerCase ( ) !== 'content-type' ) {
144+ code . push ( 1 , '.header("%s", "%qd")' , key , source . headersObj [ key ] )
145+ }
95146 } )
96147 }
97148
98- if ( source . postData && source . postData . text ) {
99- if ( source . postData . mimeType ) {
100- const mappedEnumConst = standardMediaTypes [ source . postData . mimeType ]
101- if ( mappedEnumConst ) {
102- code . push ( 1 , '.contentType(MediaType.%s)' , mappedEnumConst )
103- } else {
104- code . push ( 1 , '.contentType(MediaType.parseMediaType("%s"))' , source . postData . mimeType )
105- }
149+ if ( source . postData && ( source . postData . params || source . postData . text ) ) {
150+ const mediaTypeEnumConstant = standardMediaTypes [ source . postData . mimeType ]
151+
152+ if ( mediaTypeEnumConstant ) {
153+ code . push ( 1 , '.contentType(MediaType.%s)' , mediaTypeEnumConstant )
154+ } else {
155+ code . push ( 1 , '.contentType(MediaType.parseMediaType("%s"))' , source . postData . mimeType )
106156 }
107157
108- code . push ( 1 , '.body(%s)' , JSON . stringify ( source . postData . text ) )
158+ if ( source . postData . mimeType === 'application/x-www-form-urlencoded' && source . postData . params ) {
159+ code . push ( 1 , '.body(formDataMap)' )
160+ } else if ( multipartMimeTypes . includes ( source . postData . mimeType ) && source . postData . params ) {
161+ code . push ( 1 , '.body(multipartBuilder.build())' )
162+ } else if ( source . postData . text ) {
163+ code . push ( 1 , '.body(%s)' , JSON . stringify ( source . postData . text ) )
164+ } else if ( source . postData . jsonObj && jsonMimeTypes . includes ( source . postData . mimeType ) ) {
165+ code . push ( 1 , '.body(%s)' , JSON . stringify ( JSON . stringify ( source . postData . jsonObj ) ) )
166+ }
109167 }
110168
111169 code . push ( 1 , '.retrieve()' )
0 commit comments