Skip to content

Commit 2211549

Browse files
authored
Sahar/java okhttp snippet (#6)
* update okhttp to work on version >3 * indentation * code blank at the end only on put/post * code push, indentation as first argument * handle headers with lodash methods * retrieve comment about headers pickBy
1 parent 6bd49cf commit 2211549

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

src/targets/java/okhttp.js

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
'use strict'
1212

13+
const _ = require('lodash')
1314
const CodeBuilder = require('../../helpers/code-builder')
1415

1516
module.exports = function (source, options) {
@@ -26,17 +27,43 @@ module.exports = function (source, options) {
2627
code.push('OkHttpClient client = new OkHttpClient();')
2728
.blank()
2829

29-
if (source.postData.text) {
30-
if (source.postData.boundary) {
31-
code.push('MediaType mediaType = MediaType.parse("%s; boundary=%s");', source.postData.mimeType, source.postData.boundary)
32-
} else {
33-
code.push('MediaType mediaType = MediaType.parse("%s");', source.postData.mimeType)
34-
}
35-
code.push('RequestBody body = RequestBody.create(mediaType, %s);', JSON.stringify(source.postData.text))
30+
if (source.postData.mimeType === 'multipart/form-data') {
31+
code.push('RequestBody body = new MultipartBody.Builder()')
32+
.push(1, '.setType(MultipartBody.FORM)')
33+
34+
source.postData.params.forEach((param) => {
35+
if (param.fileName) {
36+
code.push(1, '.addFormDataPart(%s, %s,', JSON.stringify(param.name), JSON.stringify(param.fileName))
37+
.push(2, 'RequestBody.create(MediaType.parse("text/plain"), fileInput))')
38+
} else {
39+
const value = JSON.stringify(param.value.toString()) || ""
40+
code.push(1, '.addFormDataPart(%s, %s)', JSON.stringify(param.name), value)
41+
}
42+
})
43+
44+
code.push(1, '.build();')
45+
} else if (source.postData.mimeType === 'application/x-www-form-urlencoded') {
46+
code.push('RequestBody body = new FormBody.Builder()')
47+
48+
source.postData.params.forEach((param) => {
49+
const value = JSON.stringify(param.value.toString()) || ""
50+
code.push(1, '.add(%s, %s)', JSON.stringify(param.name), value)
51+
})
52+
53+
code.push(1, '.build();')
54+
} else if (source.postData.text) {
55+
code.push('MediaType mediaType = MediaType.parse("%s");', source.postData.mimeType)
56+
.push('String value = %s;', JSON.stringify(source.postData.text))
57+
.push('RequestBody body = RequestBody.create(mediaType, value);')
58+
}
59+
60+
if (source.postData.params) {
61+
code.blank()
3662
}
3763

3864
code.push('Request request = new Request.Builder()')
39-
code.push(1, '.url("%s")', source.fullUrl)
65+
.push(1, '.url("%s")', source.fullUrl)
66+
4067
if (methods.indexOf(source.method.toUpperCase()) === -1) {
4168
if (source.postData.text) {
4269
code.push(1, '.method("%s", body)', source.method.toUpperCase())
@@ -53,15 +80,10 @@ module.exports = function (source, options) {
5380
code.push(1, '.%s()', source.method.toLowerCase())
5481
}
5582

56-
// Add headers, including the cookies
57-
const headers = Object.keys(source.allHeaders)
58-
5983
// construct headers
60-
if (headers.length) {
61-
headers.forEach(function (key) {
62-
code.push(1, '.addHeader("%s", "%s")', key, source.allHeaders[key])
63-
})
64-
}
84+
_(source.allHeaders)
85+
.pickBy((value, key) => !(value.toLowerCase().includes('multipart/form-data'))) // Remove content type header if form-data
86+
.forEach((value, key) => { code.push(1, '.addHeader("%s", "%s")', key, value) })
6587

6688
code.push(1, '.build();')
6789
.blank()

0 commit comments

Comments
 (0)