Skip to content

Commit

Permalink
Sahar/java okhttp snippet (#6)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
saharbechor authored Mar 30, 2022
1 parent 6bd49cf commit 2211549
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions src/targets/java/okhttp.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

'use strict'

const _ = require('lodash')
const CodeBuilder = require('../../helpers/code-builder')

module.exports = function (source, options) {
Expand All @@ -26,17 +27,43 @@ 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(1, '.setType(MultipartBody.FORM)')

source.postData.params.forEach((param) => {
if (param.fileName) {
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(1, '.addFormDataPart(%s, %s)', JSON.stringify(param.name), value)
}
})

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(1, '.add(%s, %s)', JSON.stringify(param.name), value)
})

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))
.push('RequestBody body = RequestBody.create(mediaType, value);')
}

if (source.postData.params) {
code.blank()
}

code.push('Request request = new Request.Builder()')
code.push(1, '.url("%s")', source.fullUrl)
.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())
Expand All @@ -53,15 +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.forEach(function (key) {
code.push(1, '.addHeader("%s", "%s")', key, source.allHeaders[key])
})
}
_(source.allHeaders)
.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();')
.blank()
Expand Down

0 comments on commit 2211549

Please sign in to comment.