diff --git a/src/targets/kotlin/index.js b/src/targets/kotlin/index.js new file mode 100644 index 00000000..2d02ec4e --- /dev/null +++ b/src/targets/kotlin/index.js @@ -0,0 +1,12 @@ +'use strict' + +module.exports = { + info: { + key: 'kotlin', + title: 'Kotlin', + extname: '.kt', + default: 'okhttp' + }, + + okhttp: require('./okhttp') +} \ No newline at end of file diff --git a/src/targets/kotlin/okhttp.js b/src/targets/kotlin/okhttp.js new file mode 100644 index 00000000..b22d8250 --- /dev/null +++ b/src/targets/kotlin/okhttp.js @@ -0,0 +1,79 @@ +/** + * @description + * HTTP code snippet generator for Kotlin using OkHttp. + * + * @author + * @Kshitij-Jain + * + * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author. + */ + +'use strict' + +var util = require('util') +var CodeBuilder = require('../../helpers/code-builder') + +module.exports = function (source, options) { + var opts = util._extend({ + indent: ' ' + }, options) + + var code = new CodeBuilder(opts.indent) + + var methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD'] + + var methodsWithBody = ['POST', 'PUT', 'DELETE', 'PATCH'] + + code.push('OkHttpClient client = new OkHttpClient();') + .blank() + + if (source.postData.text) { + if (source.postData.boundary) { + code.push('val mediaType = MediaType.parse("%s; boundary=%s");', source.postData.mimeType, source.postData.boundary) + } else { + code.push('val mediaType = MediaType.parse("%s");', source.postData.mimeType) + } + code.push('val body = RequestBody.create(mediaType, %s);', JSON.stringify(source.postData.text)) + } + + code.push('val request = Request.Builder()') + code.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()) + } else { + code.push(1, '.method("%s", null)', source.method.toUpperCase()) + } + } else if (methodsWithBody.indexOf(source.method.toUpperCase()) >= 0) { + if (source.postData.text) { + code.push(1, '.%s(body)', source.method.toLowerCase()) + } else { + code.push(1, '.%s(null)', source.method.toLowerCase()) + } + } else { + code.push(1, '.%s()', source.method.toLowerCase()) + } + + // Add headers, including the cookies + var headers = Object.keys(source.allHeaders) + + // construct headers + if (headers.length) { + headers.forEach(function (key) { + code.push(1, '.addHeader("%s", "%s")', key, source.allHeaders[key]) + }) + } + + code.push(1, '.build();') + .blank() + .push('val response = client.newCall(request).execute();') + + return code.join() +} + +module.exports.info = { + key: 'okhttp', + title: 'OkHttp', + link: 'http://square.github.io/okhttp/', + description: 'An HTTP Request Client Library' +}