Skip to content

Commit fa2852e

Browse files
authored
Merge pull request #1 from windard/add_java_asynchttp
Add java asynchttp
2 parents 78789f6 + 0fe2422 commit fa2852e

19 files changed

+182
-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+
// var methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT' ]
23+
24+
code.push('Dsl.asyncHttpClient()')
25+
26+
code.push(1, '.prepare%s%s("%s")', source.method.slice(0, 1).toUpperCase(), source.method.slice(1).toLowerCase(), source.fullUrl)
27+
28+
// Add headers, including the cookies
29+
var headers = Object.keys(source.allHeaders)
30+
31+
// construct headers
32+
if (headers.length) {
33+
headers.forEach(function (key) {
34+
code.push(1, '.setHeader("%s", "%s")', key, source.allHeaders[key])
35+
})
36+
}
37+
38+
if (source.postData.text) {
39+
code.push(1, '.setBody(%s)', JSON.stringify(source.postData.text))
40+
}
41+
42+
code.push(1, '.execute()')
43+
code.push(1, '.toCompletableFuture()')
44+
code.push(1, '.thenAccept(System.out::println)')
45+
code.push(1, '.join();')
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Dsl.asyncHttpClient()
2+
.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();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Dsl.asyncHttpClient()
2+
.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();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Dsl.asyncHttpClient()
2+
.preparePost("http://mockbin.com/har")
3+
.setHeader("cookie", "foo=bar; bar=baz")
4+
.execute()
5+
.toCompletableFuture()
6+
.thenAccept(System.out::println)
7+
.join();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Dsl.asyncHttpClient()
2+
.preparePropfind("http://mockbin.com/har")
3+
.execute()
4+
.toCompletableFuture()
5+
.thenAccept(System.out::println)
6+
.join();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Dsl.asyncHttpClient()
2+
.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();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Dsl.asyncHttpClient()
2+
.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();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Dsl.asyncHttpClient()
2+
.prepareGet("https://mockbin.com/har")
3+
.execute()
4+
.toCompletableFuture()
5+
.thenAccept(System.out::println)
6+
.join();

0 commit comments

Comments
 (0)