Skip to content

Commit cc1c8b1

Browse files
committed
Merge pull request Kong#60 from shashiranjan84/master
Java implementation using OkHttp
2 parents 77ee019 + dfb3e3f commit cc1c8b1

16 files changed

Lines changed: 220 additions & 0 deletions

‎src/targets/java/okhttp.js‎

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

‎test/fixtures/available-targets.json‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@
179179
"extname": ".java",
180180
"default": "unirest",
181181
"clients": [
182+
{
183+
"key": "okhttp",
184+
"title": "OkHttp",
185+
"link": "http://square.github.io/okhttp/",
186+
"description": "An HTTP Request Client Library"
187+
},
182188
{
183189
"key": "unirest",
184190
"title": "Unirest",
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
OkHttpClient client = new OkHttpClient();
2+
3+
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
4+
RequestBody body = RequestBody.create(mediaType, "foo=bar&hello=world");
5+
Request request = new Request.Builder()
6+
.url("http://mockbin.com/har")
7+
.post(body)
8+
.addHeader("content-type", "application/x-www-form-urlencoded")
9+
.build();
10+
11+
Response response = client.newCall(request).execute();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
OkHttpClient client = new OkHttpClient();
2+
3+
MediaType mediaType = MediaType.parse("application/json");
4+
RequestBody body = RequestBody.create(mediaType, "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}]}");
5+
Request request = new Request.Builder()
6+
.url("http://mockbin.com/har")
7+
.post(body)
8+
.addHeader("content-type", "application/json")
9+
.build();
10+
11+
Response response = client.newCall(request).execute();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
OkHttpClient client = new OkHttpClient();
2+
3+
Request request = new Request.Builder()
4+
.url("http://mockbin.com/har")
5+
.post(null)
6+
.addHeader("cookie", "foo=bar; bar=baz")
7+
.build();
8+
9+
Response response = client.newCall(request).execute();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
OkHttpClient client = new OkHttpClient();
2+
3+
Request request = new Request.Builder()
4+
.url("http://mockbin.com/har")
5+
.method("PROPFIND", null)
6+
.build();
7+
8+
Response response = client.newCall(request).execute();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
OkHttpClient client = new OkHttpClient();
2+
3+
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
4+
RequestBody body = RequestBody.create(mediaType, "foo=bar");
5+
Request request = new Request.Builder()
6+
.url("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value")
7+
.post(body)
8+
.addHeader("cookie", "foo=bar; bar=baz")
9+
.addHeader("accept", "application/json")
10+
.addHeader("content-type", "application/x-www-form-urlencoded")
11+
.build();
12+
13+
Response response = client.newCall(request).execute();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
OkHttpClient client = new OkHttpClient();
2+
3+
Request request = new Request.Builder()
4+
.url("http://mockbin.com/har")
5+
.get()
6+
.addHeader("accept", "application/json")
7+
.addHeader("x-foo", "Bar")
8+
.build();
9+
10+
Response response = client.newCall(request).execute();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
OkHttpClient client = new OkHttpClient();
2+
3+
Request request = new Request.Builder()
4+
.url("https://mockbin.com/har")
5+
.get()
6+
.build();
7+
8+
Response response = client.newCall(request).execute();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
OkHttpClient client = new OkHttpClient();
2+
3+
MediaType mediaType = MediaType.parse("multipart/form-data; boundary=---011000010111000001101001");
4+
RequestBody body = RequestBody.create(mediaType, "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--");
5+
Request request = new Request.Builder()
6+
.url("http://mockbin.com/har")
7+
.post(body)
8+
.addHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001")
9+
.build();
10+
11+
Response response = client.newCall(request).execute();

0 commit comments

Comments
 (0)