Skip to content

Commit cc8e368

Browse files
authored
Merge pull request #163 from windard/add_java_asynchttp
add java async http client
2 parents 78789f6 + fa57cba commit cc8e368

19 files changed

+212
-1
lines changed

src/targets/java/asynchttp.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @description
3+
* Asynchronous Http and WebSocket Client library for Java
4+
*
5+
* @author
6+
* @windard
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+
code.push('AsyncHttpClient client = new DefaultAsyncHttpClient();')
23+
24+
code.push(`client.prepare${source.method[0].toUpperCase()}${source.method.substring(1).toLowerCase()}("${source.fullUrl}")`)
25+
26+
// Add headers, including the cookies
27+
var headers = Object.keys(source.allHeaders)
28+
29+
// construct headers
30+
if (headers.length) {
31+
headers.forEach(function (key) {
32+
code.push(1, '.setHeader("%s", "%s")', key, source.allHeaders[key])
33+
})
34+
}
35+
36+
if (source.postData.text) {
37+
code.push(1, '.setBody(%s)', JSON.stringify(source.postData.text))
38+
}
39+
40+
code.push(1, '.execute()')
41+
code.push(1, '.toCompletableFuture()')
42+
code.push(1, '.thenAccept(System.out::println)')
43+
code.push(1, '.join();')
44+
code.blank()
45+
code.push('client.close();')
46+
47+
return code.join()
48+
}
49+
50+
module.exports.info = {
51+
key: 'asynchttp',
52+
title: 'AsyncHttp',
53+
link: 'https://github.com/AsyncHttpClient/async-http-client',
54+
description: 'Asynchronous Http and WebSocket Client library for Java'
55+
}

src/targets/java/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ module.exports = {
99
},
1010

1111
okhttp: require('./okhttp'),
12-
unirest: require('./unirest')
12+
unirest: require('./unirest'),
13+
asynchttp: require('./asynchttp')
14+
1315
}

test/fixtures/available-targets.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@
196196
"title": "Unirest",
197197
"link": "http://unirest.io/java.html",
198198
"description": "Lightweight HTTP Request Client Library"
199+
},
200+
{
201+
"key": "asynchttp",
202+
"title": "AsyncHttp",
203+
"link": "https://github.com/AsyncHttpClient/async-http-client",
204+
"description": "Asynchronous Http and WebSocket Client library for Java"
199205
}
200206
]
201207
},
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
AsyncHttpClient client = new DefaultAsyncHttpClient();
2+
client.preparePost("http://mockbin.com/har")
3+
.setHeader("content-type", "application/x-www-form-urlencoded")
4+
.setBody("foo=bar&hello=world")
5+
.execute()
6+
.toCompletableFuture()
7+
.thenAccept(System.out::println)
8+
.join();
9+
10+
client.close();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
AsyncHttpClient client = new DefaultAsyncHttpClient();
2+
client.preparePost("http://mockbin.com/har")
3+
.setHeader("content-type", "application/json")
4+
.setBody("{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}")
5+
.execute()
6+
.toCompletableFuture()
7+
.thenAccept(System.out::println)
8+
.join();
9+
10+
client.close();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
AsyncHttpClient client = new DefaultAsyncHttpClient();
2+
client.preparePost("http://mockbin.com/har")
3+
.setHeader("cookie", "foo=bar; bar=baz")
4+
.execute()
5+
.toCompletableFuture()
6+
.thenAccept(System.out::println)
7+
.join();
8+
9+
client.close();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
AsyncHttpClient client = new DefaultAsyncHttpClient();
2+
client.preparePropfind("http://mockbin.com/har")
3+
.execute()
4+
.toCompletableFuture()
5+
.thenAccept(System.out::println)
6+
.join();
7+
8+
client.close();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
AsyncHttpClient client = new DefaultAsyncHttpClient();
2+
client.preparePost("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value")
3+
.setHeader("cookie", "foo=bar; bar=baz")
4+
.setHeader("accept", "application/json")
5+
.setHeader("content-type", "application/x-www-form-urlencoded")
6+
.setBody("foo=bar")
7+
.execute()
8+
.toCompletableFuture()
9+
.thenAccept(System.out::println)
10+
.join();
11+
12+
client.close();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
AsyncHttpClient client = new DefaultAsyncHttpClient();
2+
client.prepareGet("http://mockbin.com/har")
3+
.setHeader("accept", "application/json")
4+
.setHeader("x-foo", "Bar")
5+
.execute()
6+
.toCompletableFuture()
7+
.thenAccept(System.out::println)
8+
.join();
9+
10+
client.close();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
AsyncHttpClient client = new DefaultAsyncHttpClient();
2+
client.prepareGet("https://mockbin.com/har")
3+
.execute()
4+
.toCompletableFuture()
5+
.thenAccept(System.out::println)
6+
.join();
7+
8+
client.close();

0 commit comments

Comments
 (0)