Skip to content

Commit ab80f72

Browse files
authored
Merge pull request #172 from seanghay/kotlin-okhttp
Add Kotlin for OkHttp Support
2 parents 692f540 + 39d3681 commit ab80f72

22 files changed

+263
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"java",
2121
"javascript",
2222
"jquery",
23+
"kotlin",
2324
"objc",
2425
"objective-c",
2526
"ocaml",

src/targets/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = {
88
http: require('./http'),
99
java: require('./java'),
1010
javascript: require('./javascript'),
11+
kotlin: require('./kotlin'),
1112
node: require('./node'),
1213
objc: require('./objc'),
1314
ocaml: require('./ocaml'),

src/targets/kotlin/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
3+
module.exports = {
4+
info: {
5+
key: 'kotlin',
6+
title: 'Kotlin',
7+
extname: '.kt',
8+
default: 'okhttp'
9+
},
10+
11+
okhttp: require('./okhttp')
12+
}

src/targets/kotlin/okhttp.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for Kotlin using OkHttp.
4+
*
5+
* @author
6+
* @seanghay
7+
*
8+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9+
*/
10+
11+
'use strict'
12+
13+
var CodeBuilder = require('../../helpers/code-builder')
14+
15+
module.exports = function (source, options) {
16+
var opts = Object.assign({
17+
indent: ' '
18+
}, options)
19+
20+
var code = new CodeBuilder(opts.indent)
21+
22+
var methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD']
23+
24+
var methodsWithBody = ['POST', 'PUT', 'DELETE', 'PATCH']
25+
26+
code.push('val client = OkHttpClient()')
27+
.blank()
28+
29+
if (source.postData.text) {
30+
if (source.postData.boundary) {
31+
code.push('val mediaType = MediaType.parse("%s; boundary=%s")', source.postData.mimeType, source.postData.boundary)
32+
} else {
33+
code.push('val mediaType = MediaType.parse("%s")', source.postData.mimeType)
34+
}
35+
code.push('val body = RequestBody.create(mediaType, %s)', JSON.stringify(source.postData.text))
36+
}
37+
38+
code.push('val request = Request.Builder()')
39+
code.push(1, '.url("%s")', source.fullUrl)
40+
if (methods.indexOf(source.method.toUpperCase()) === -1) {
41+
if (source.postData.text) {
42+
code.push(1, '.method("%s", body)', source.method.toUpperCase())
43+
} else {
44+
code.push(1, '.method("%s", null)', source.method.toUpperCase())
45+
}
46+
} else if (methodsWithBody.indexOf(source.method.toUpperCase()) >= 0) {
47+
if (source.postData.text) {
48+
code.push(1, '.%s(body)', source.method.toLowerCase())
49+
} else {
50+
code.push(1, '.%s(null)', source.method.toLowerCase())
51+
}
52+
} else {
53+
code.push(1, '.%s()', source.method.toLowerCase())
54+
}
55+
56+
// Add headers, including the cookies
57+
var headers = Object.keys(source.allHeaders)
58+
59+
// construct headers
60+
if (headers.length) {
61+
headers.forEach(function (key) {
62+
code.push(1, '.addHeader("%s", "%s")', key, source.allHeaders[key])
63+
})
64+
}
65+
66+
code.push(1, '.build()')
67+
.blank()
68+
.push('val response = client.newCall(request).execute()')
69+
70+
return code.join()
71+
}
72+
73+
module.exports.info = {
74+
key: 'okhttp',
75+
title: 'OkHttp',
76+
link: 'http://square.github.io/okhttp/',
77+
description: 'An HTTP Request Client Library'
78+
}

test/fixtures/available-targets.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,5 +320,19 @@
320320
"title": "HTTP/1.1"
321321
}
322322
]
323+
},
324+
{
325+
"key": "kotlin",
326+
"title": "Kotlin",
327+
"extname": ".kt",
328+
"default": "okhttp",
329+
"clients": [
330+
{
331+
"key": "okhttp",
332+
"title": "OkHttp",
333+
"link": "http://square.github.io/okhttp/",
334+
"description": "An HTTP Request Client Library"
335+
}
336+
]
323337
}
324338
]

test/fixtures/output/.DS_Store

6 KB
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
val client = OkHttpClient()
2+
3+
val mediaType = MediaType.parse("application/x-www-form-urlencoded")
4+
val body = RequestBody.create(mediaType, "foo=bar&hello=world")
5+
val request = Request.Builder()
6+
.url("http://mockbin.com/har")
7+
.post(body)
8+
.addHeader("content-type", "application/x-www-form-urlencoded")
9+
.build()
10+
11+
val response = client.newCall(request).execute()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
val client = OkHttpClient()
2+
3+
val mediaType = MediaType.parse("application/json")
4+
val body = RequestBody.create(mediaType, "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}")
5+
val request = Request.Builder()
6+
.url("http://mockbin.com/har")
7+
.post(body)
8+
.addHeader("content-type", "application/json")
9+
.build()
10+
11+
val response = client.newCall(request).execute()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
val client = OkHttpClient()
2+
3+
val request = Request.Builder()
4+
.url("http://mockbin.com/har")
5+
.post(null)
6+
.addHeader("cookie", "foo=bar; bar=baz")
7+
.build()
8+
9+
val response = client.newCall(request).execute()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
val client = OkHttpClient()
2+
3+
val request = Request.Builder()
4+
.url("http://mockbin.com/har")
5+
.method("PROPFIND", null)
6+
.build()
7+
8+
val response = client.newCall(request).execute()

0 commit comments

Comments
 (0)