|
3 | 3 | var util = require('util'); |
4 | 4 |
|
5 | 5 | module.exports = function(options) { |
6 | | - var opts = util._extend({ |
7 | | - indent : ' ' |
8 | | - }, options); |
9 | | - |
10 | | - var code = []; |
11 | | - |
12 | | - code.push(util.format( |
13 | | - 'HttpResponse<JsonNode> jsonResponse = Unirest.%s("%s")', this.source.method.toLowerCase(), |
14 | | - this.source.fullUrl)); |
15 | | - |
16 | | - // construct headers |
17 | | - if (this.source.headers && this.source.headers.length) { |
18 | | - this.source.headers.map(function(header) { |
19 | | - code.push(util.format('.header("%s", "%s")', header.name, |
20 | | - header.value)); |
21 | | - }); |
22 | | - } |
23 | | - |
24 | | - |
25 | | - //construct cookies argument |
26 | | - if (this.source.cookies && this.source.cookies.length) { |
27 | | - var cookies = this.source.cookies.map(function(cookie) { |
28 | | - return encodeURIComponent(cookie.name) + '=' |
29 | | - + encodeURIComponent(cookie.value); |
30 | | - }); |
31 | | - code.push(util.format('.header("Cookie", "%s")', cookies.join('; '))); |
32 | | - } |
33 | | - |
34 | | - //construct postdata |
35 | | - if (this.source.postData) { |
36 | | - if(this.source.postData.text){ |
37 | | - code.push(util.format('.body(%s)', JSON |
38 | | - .stringify(this.source.postData.text))); |
39 | | - } |
40 | | - } |
41 | | - |
42 | | - code.push(".asJson();"); |
43 | | - return code.join( '\n' + opts.indent); |
44 | | -}; |
| 6 | + var self = this; |
| 7 | + var opts = util._extend({ |
| 8 | + indent : ' ' |
| 9 | + }, options); |
| 10 | + |
| 11 | + var code = []; |
| 12 | + |
| 13 | + var methods = [ "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS" ]; |
| 14 | + |
| 15 | + if (methods.indexOf(self.source.method.toUpperCase()) == -1) { |
| 16 | + return self.source.method.toUpperCase() + " method not supported by Unirest liabrary."; |
| 17 | + } |
45 | 18 |
|
| 19 | + code.push(util.format('HttpResponse<String> response = Unirest.%s("%s")', self.source.method.toLowerCase(), self.source.fullUrl)); |
46 | 20 |
|
| 21 | + // Add headers, including the cookies |
| 22 | + var headers = Object.keys(self.source.allHeaders); |
| 23 | + |
| 24 | + // construct headers |
| 25 | + if (headers.length) { |
| 26 | + headers.map(function(key) { |
| 27 | + code.push(util.format('.header("%s", "%s")', key, self.source.allHeaders[key])); |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 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 | + } |
| 36 | + } |
| 37 | + |
| 38 | + code.push(".asString();"); |
| 39 | + return code.join('\n' + opts.indent); |
| 40 | +}; |
47 | 41 |
|
48 | 42 | module.exports.info = { |
49 | | - key: 'unirest', |
50 | | - title: 'JAVA', |
51 | | - link : 'http://unirest.io/java.html', |
52 | | - description : 'Unirest Java interface' |
| 43 | + key : 'unirest', |
| 44 | + title : 'JAVA', |
| 45 | + link : 'http://unirest.io/java.html', |
| 46 | + description : 'Unirest Java interface' |
53 | 47 | }; |
0 commit comments