From 83f3cff4fa509e01c9851913b46b86f1f8084c59 Mon Sep 17 00:00:00 2001 From: Sahar Bechor Date: Mon, 28 Mar 2022 18:06:10 +0300 Subject: [PATCH 1/6] update okhttp to work on version >3 --- src/targets/java/okhttp.js | 46 ++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/src/targets/java/okhttp.js b/src/targets/java/okhttp.js index 3dc756419..6c7f4e919 100644 --- a/src/targets/java/okhttp.js +++ b/src/targets/java/okhttp.js @@ -26,17 +26,40 @@ module.exports = function (source, options) { code.push('OkHttpClient client = new OkHttpClient();') .blank() - if (source.postData.text) { - if (source.postData.boundary) { - code.push('MediaType mediaType = MediaType.parse("%s; boundary=%s");', source.postData.mimeType, source.postData.boundary) - } else { - code.push('MediaType mediaType = MediaType.parse("%s");', source.postData.mimeType) - } - code.push('RequestBody body = RequestBody.create(mediaType, %s);', JSON.stringify(source.postData.text)) + if (source.postData.mimeType === 'multipart/form-data') { + code.push('RequestBody body = new MultipartBody.Builder()') + .push('%s.setType(MultipartBody.FORM)', opts.indent) + + source.postData.params.forEach((param) => { + if (param.fileName) { + code.push('%s.addFormDataPart(%s, %s,', opts.indent, JSON.stringify(param.name), JSON.stringify(param.fileName)) + .push('%s%sRequestBody.create(MediaType.parse("text/plain"), fileInput))', opts.indent, opts.indent) + } else { + const value = JSON.stringify(param.value.toString()) || "" + code.push('%s.addFormDataPart(%s, %s)', opts.indent, JSON.stringify(param.name), value) + } + }) + + code.push('%s.build();', opts.indent) + } else if (source.postData.mimeType === 'application/x-www-form-urlencoded') { + code.push('RequestBody body = new FormBody.Builder()') + + source.postData.params.forEach((param) => { + const value = JSON.stringify(param.value.toString()) || "" + code.push('%s.add(%s, %s)', opts.indent, JSON.stringify(param.name), value) + }) + + code.push('%s.build();', opts.indent) + } else if (source.postData.text) { + code.push('MediaType mediaType = MediaType.parse("%s");', source.postData.mimeType) + .push('String value = %s;', JSON.stringify(source.postData.text)) + .push('RequestBody body = RequestBody.create(mediaType, value);') } - code.push('Request request = new Request.Builder()') - code.push(1, '.url("%s")', source.fullUrl) + code.blank() + .push('Request request = new Request.Builder()') + .push(1, '.url("%s")', source.fullUrl) + if (methods.indexOf(source.method.toUpperCase()) === -1) { if (source.postData.text) { code.push(1, '.method("%s", body)', source.method.toUpperCase()) @@ -58,9 +81,8 @@ module.exports = function (source, options) { // construct headers if (headers.length) { - headers.forEach(function (key) { - code.push(1, '.addHeader("%s", "%s")', key, source.allHeaders[key]) - }) + headers.filter(key => !(source.allHeaders[key].toLowerCase().includes('multipart/form-data'))) // Remove content type header if form-data + .forEach((key) => { code.push(1, '.addHeader("%s", "%s")', key, source.allHeaders[key]) }) } code.push(1, '.build();') From e1883cd96843e610a913e7f7d996336b1e927d28 Mon Sep 17 00:00:00 2001 From: Sahar Bechor Date: Mon, 28 Mar 2022 18:09:35 +0300 Subject: [PATCH 2/6] indentation --- src/targets/java/okhttp.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/targets/java/okhttp.js b/src/targets/java/okhttp.js index 6c7f4e919..c054a3f37 100644 --- a/src/targets/java/okhttp.js +++ b/src/targets/java/okhttp.js @@ -33,7 +33,7 @@ module.exports = function (source, options) { source.postData.params.forEach((param) => { if (param.fileName) { code.push('%s.addFormDataPart(%s, %s,', opts.indent, JSON.stringify(param.name), JSON.stringify(param.fileName)) - .push('%s%sRequestBody.create(MediaType.parse("text/plain"), fileInput))', opts.indent, opts.indent) + .push('%s%sRequestBody.create(MediaType.parse("text/plain"), fileInput))', opts.indent, opts.indent) } else { const value = JSON.stringify(param.value.toString()) || "" code.push('%s.addFormDataPart(%s, %s)', opts.indent, JSON.stringify(param.name), value) From c465ff2a6d7d83248e843ac710dba0505a396036 Mon Sep 17 00:00:00 2001 From: Sahar Bechor Date: Tue, 29 Mar 2022 09:29:37 +0300 Subject: [PATCH 3/6] code blank at the end only on put/post --- src/targets/java/okhttp.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/targets/java/okhttp.js b/src/targets/java/okhttp.js index c054a3f37..0a3571741 100644 --- a/src/targets/java/okhttp.js +++ b/src/targets/java/okhttp.js @@ -56,8 +56,11 @@ module.exports = function (source, options) { .push('RequestBody body = RequestBody.create(mediaType, value);') } - code.blank() - .push('Request request = new Request.Builder()') + if (source.postData.params) { + code.blank() + } + + code.push('Request request = new Request.Builder()') .push(1, '.url("%s")', source.fullUrl) if (methods.indexOf(source.method.toUpperCase()) === -1) { From 2dc1f474d10e887d4e6ec4535da9b9827af35b7a Mon Sep 17 00:00:00 2001 From: Sahar Bechor Date: Tue, 29 Mar 2022 15:31:16 +0300 Subject: [PATCH 4/6] code push, indentation as first argument --- src/targets/java/okhttp.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/targets/java/okhttp.js b/src/targets/java/okhttp.js index 0a3571741..da7dfb645 100644 --- a/src/targets/java/okhttp.js +++ b/src/targets/java/okhttp.js @@ -28,28 +28,28 @@ module.exports = function (source, options) { if (source.postData.mimeType === 'multipart/form-data') { code.push('RequestBody body = new MultipartBody.Builder()') - .push('%s.setType(MultipartBody.FORM)', opts.indent) + .push(1, '.setType(MultipartBody.FORM)') source.postData.params.forEach((param) => { if (param.fileName) { - code.push('%s.addFormDataPart(%s, %s,', opts.indent, JSON.stringify(param.name), JSON.stringify(param.fileName)) - .push('%s%sRequestBody.create(MediaType.parse("text/plain"), fileInput))', opts.indent, opts.indent) + code.push(1, '.addFormDataPart(%s, %s,', JSON.stringify(param.name), JSON.stringify(param.fileName)) + .push(2, 'RequestBody.create(MediaType.parse("text/plain"), fileInput))') } else { const value = JSON.stringify(param.value.toString()) || "" - code.push('%s.addFormDataPart(%s, %s)', opts.indent, JSON.stringify(param.name), value) + code.push(1, '.addFormDataPart(%s, %s)', JSON.stringify(param.name), value) } }) - code.push('%s.build();', opts.indent) + code.push(1, '.build();') } else if (source.postData.mimeType === 'application/x-www-form-urlencoded') { code.push('RequestBody body = new FormBody.Builder()') source.postData.params.forEach((param) => { const value = JSON.stringify(param.value.toString()) || "" - code.push('%s.add(%s, %s)', opts.indent, JSON.stringify(param.name), value) + code.push(1, '.add(%s, %s)', JSON.stringify(param.name), value) }) - code.push('%s.build();', opts.indent) + code.push(1, '.build();') } else if (source.postData.text) { code.push('MediaType mediaType = MediaType.parse("%s");', source.postData.mimeType) .push('String value = %s;', JSON.stringify(source.postData.text)) From 3415a23f7a56e7643b22b6b245a50ced7eba39f8 Mon Sep 17 00:00:00 2001 From: Sahar Bechor Date: Tue, 29 Mar 2022 15:46:11 +0300 Subject: [PATCH 5/6] handle headers with lodash methods --- src/targets/java/okhttp.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/targets/java/okhttp.js b/src/targets/java/okhttp.js index da7dfb645..8029186c9 100644 --- a/src/targets/java/okhttp.js +++ b/src/targets/java/okhttp.js @@ -10,6 +10,7 @@ 'use strict' +const _ = require('lodash') const CodeBuilder = require('../../helpers/code-builder') module.exports = function (source, options) { @@ -79,14 +80,10 @@ module.exports = function (source, options) { code.push(1, '.%s()', source.method.toLowerCase()) } - // Add headers, including the cookies - const headers = Object.keys(source.allHeaders) - // construct headers - if (headers.length) { - headers.filter(key => !(source.allHeaders[key].toLowerCase().includes('multipart/form-data'))) // Remove content type header if form-data - .forEach((key) => { code.push(1, '.addHeader("%s", "%s")', key, source.allHeaders[key]) }) - } + _(source.allHeaders) + .pickBy((value, key) => !(value.toLowerCase().includes('multipart/form-data'))) + .forEach((value, key) => { code.push(1, '.addHeader("%s", "%s")', key, value) }) code.push(1, '.build();') .blank() From d58d3aa9dce4edd6eabfb66c0f43fabbe60a68b0 Mon Sep 17 00:00:00 2001 From: Sahar Bechor Date: Tue, 29 Mar 2022 15:52:08 +0300 Subject: [PATCH 6/6] retrieve comment about headers pickBy --- src/targets/java/okhttp.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/targets/java/okhttp.js b/src/targets/java/okhttp.js index 8029186c9..bea940b41 100644 --- a/src/targets/java/okhttp.js +++ b/src/targets/java/okhttp.js @@ -82,7 +82,7 @@ module.exports = function (source, options) { // construct headers _(source.allHeaders) - .pickBy((value, key) => !(value.toLowerCase().includes('multipart/form-data'))) + .pickBy((value, key) => !(value.toLowerCase().includes('multipart/form-data'))) // Remove content type header if form-data .forEach((value, key) => { code.push(1, '.addHeader("%s", "%s")', key, value) }) code.push(1, '.build();')