Skip to content

Commit 16ba6dd

Browse files
author
Shashi Ranjan
committed
added custom methods support
1 parent 22ff5d9 commit 16ba6dd

12 files changed

Lines changed: 46 additions & 13 deletions

src/targets/java/unirest.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,35 @@ module.exports = function (options) {
1212

1313
var methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS' ]
1414

15+
code.push('//Import unirest libarary (http://unirest.io/java.html) \n')
16+
1517
if (methods.indexOf(self.source.method.toUpperCase()) === -1) {
16-
return self.source.method.toUpperCase() + ' method not supported by Unirest liabrary.'
18+
code.push(util.format('HttpResponse<String> response = Unirest.customMethod("%s","%s")', self.source.method.toUpperCase(), self.source.fullUrl))
19+
} else {
20+
code.push(util.format('HttpResponse<String> response = Unirest.%s("%s")', self.source.method.toLowerCase(), self.source.fullUrl))
1721
}
1822

19-
code.push(util.format('HttpResponse<String> response = Unirest.%s("%s")', self.source.method.toLowerCase(), self.source.fullUrl))
20-
2123
// Add headers, including the cookies
2224
var headers = Object.keys(self.source.allHeaders)
2325

2426
// construct headers
2527
if (headers.length) {
2628
headers.map(function (key) {
27-
code.push(util.format('.header("%s", "%s")', key, self.source.allHeaders[key]))
29+
code.push(opts.indent + util.format('.header("%s", "%s")', key, self.source.allHeaders[key]))
2830
})
2931
}
3032

31-
// construct postdata
32-
if (self.source.postData) {
33-
if (self.source.postData.text) {
34-
code.push(util.format('.body(%s)', JSON.stringify(self.source.postData.text)))
35-
}
33+
if (self.source.postData.text) {
34+
code.push(opts.indent + util.format('.body(%s)', JSON.stringify(self.source.postData.text)))
3635
}
3736

38-
code.push('.asString();')
39-
return code.join('\n' + opts.indent)
37+
code.push(opts.indent + '.asString();')
38+
return code.join('\n')
4039
}
4140

4241
module.exports.info = {
4342
key: 'unirest',
44-
title: 'Unirest',
43+
title: 'JAVA',
4544
link: 'http://unirest.io/java.html',
46-
description: 'Lightweight HTTP Request Client Library'
45+
description: 'Unirest Java interface'
4746
}

test/fixtures/available-targets.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@
2020
"description": "a free software package for retrieving files using HTTP, HTTPS",
2121
"extname": ".sh"
2222
},
23+
{
24+
"key": "java",
25+
"title": "JAVA",
26+
"extname": ".java",
27+
"default": "unirest",
28+
"clients": [
29+
{
30+
"key": "unirest",
31+
"title": "JAVA",
32+
"link": "http://unirest.io/java.html",
33+
"description": "Unirest Java interface"
34+
}
35+
]
36+
},
2337
{
2438
"key": "node",
2539
"title": "Node.js",

test/fixtures/output/java/unirest/application-form-encoded.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//Import unirest libarary (http://unirest.io/java.html)
2+
13
HttpResponse<String> response = Unirest.post("http://mockbin.com/har")
24
.header("content-type", "application/x-www-form-urlencoded")
35
.body("foo=bar&hello=world")

test/fixtures/output/java/unirest/application-json.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//Import unirest libarary (http://unirest.io/java.html)
2+
13
HttpResponse<String> response = Unirest.post("http://mockbin.com/har")
24
.header("content-type", "application/json")
35
.body("{\"number\": 1, \"string\": \"f\\\"oo\", \"arr\": [1, 2, 3], \"nested\": {\"a\": \"b\"}, \"arr_mix\": [1, \"a\", {\"arr_mix_nested\": {}}] }")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//Import unirest libarary (http://unirest.io/java.html)
2+
13
HttpResponse<String> response = Unirest.post("http://mockbin.com/har")
24
.header("cookie", "foo=bar; bar=baz")
35
.asString();

test/fixtures/output/java/unirest/full.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//Import unirest libarary (http://unirest.io/java.html)
2+
13
HttpResponse<String> response = Unirest.post("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value")
24
.header("cookie", "foo=bar; bar=baz")
35
.header("accept", "application/json")

test/fixtures/output/java/unirest/headers.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//Import unirest libarary (http://unirest.io/java.html)
2+
13
HttpResponse<String> response = Unirest.get("http://mockbin.com/har")
24
.header("accept", "application/json")
35
.header("x-foo", "Bar")

test/fixtures/output/java/unirest/multipart-data.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//Import unirest libarary (http://unirest.io/java.html)
2+
13
HttpResponse<String> response = Unirest.post("http://mockbin.com/har")
24
.header("content-type", "multipart/form-data; boundary=---011000010111000001101001")
35
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--")

test/fixtures/output/java/unirest/multipart-file.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//Import unirest libarary (http://unirest.io/java.html)
2+
13
HttpResponse<String> response = Unirest.post("http://mockbin.com/har")
24
.header("content-type", "multipart/form-data; boundary=---011000010111000001101001")
35
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--")

test/fixtures/output/java/unirest/multipart-form-data.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//Import unirest libarary (http://unirest.io/java.html)
2+
13
HttpResponse<String> response = Unirest.post("http://mockbin.com/har")
24
.header("content-type", "multipart/form-data; boundary=---011000010111000001101001")
35
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--")

0 commit comments

Comments
 (0)