@readme/httpsnippet 2.4.5 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/README.md +19 -9
  2. package/package.json +24 -27
  3. package/src/helpers/code-builder.js +85 -80
  4. package/src/helpers/form-data.js +28 -26
  5. package/src/helpers/headers.js +18 -8
  6. package/src/helpers/reducer.js +10 -14
  7. package/src/index.js +175 -151
  8. package/src/targets/c/index.js +3 -5
  9. package/src/targets/c/libcurl.js +20 -25
  10. package/src/targets/clojure/clj_http.js +126 -102
  11. package/src/targets/clojure/index.js +3 -5
  12. package/src/targets/csharp/httpclient.js +93 -90
  13. package/src/targets/csharp/index.js +3 -5
  14. package/src/targets/csharp/restsharp.js +20 -22
  15. package/src/targets/go/index.js +3 -5
  16. package/src/targets/go/native.js +47 -54
  17. package/src/targets/http/http1.1.js +27 -37
  18. package/src/targets/http/index.js +3 -5
  19. package/src/targets/index.js +2 -4
  20. package/src/targets/java/asynchttp.js +22 -23
  21. package/src/targets/java/index.js +3 -5
  22. package/src/targets/java/nethttp.js +20 -24
  23. package/src/targets/java/okhttp.js +31 -31
  24. package/src/targets/java/unirest.js +22 -19
  25. package/src/targets/javascript/axios.js +47 -42
  26. package/src/targets/javascript/fetch.js +66 -68
  27. package/src/targets/javascript/index.js +3 -5
  28. package/src/targets/javascript/jquery.js +50 -44
  29. package/src/targets/javascript/xhr.js +48 -44
  30. package/src/targets/kotlin/index.js +3 -5
  31. package/src/targets/kotlin/okhttp.js +31 -31
  32. package/src/targets/node/axios.js +37 -34
  33. package/src/targets/node/fetch.js +94 -87
  34. package/src/targets/node/index.js +4 -5
  35. package/src/targets/node/native.js +51 -45
  36. package/src/targets/node/request.js +69 -69
  37. package/src/targets/node/unirest.js +70 -68
  38. package/src/targets/objc/helpers.js +32 -25
  39. package/src/targets/objc/index.js +3 -5
  40. package/src/targets/objc/nsurlsession.js +108 -79
  41. package/src/targets/ocaml/cohttp.js +32 -30
  42. package/src/targets/ocaml/index.js +3 -5
  43. package/src/targets/php/curl.js +86 -82
  44. package/src/targets/php/guzzle.js +135 -0
  45. package/src/targets/php/helpers.js +31 -29
  46. package/src/targets/php/http1.js +38 -49
  47. package/src/targets/php/http2.js +75 -75
  48. package/src/targets/php/index.js +5 -5
  49. package/src/targets/powershell/common.js +35 -30
  50. package/src/targets/powershell/index.js +3 -5
  51. package/src/targets/powershell/restmethod.js +3 -5
  52. package/src/targets/powershell/webrequest.js +3 -5
  53. package/src/targets/python/helpers.js +42 -37
  54. package/src/targets/python/index.js +4 -5
  55. package/src/targets/python/python3.js +33 -45
  56. package/src/targets/python/requests.js +48 -72
  57. package/src/targets/r/httr.js +60 -73
  58. package/src/targets/r/index.js +3 -5
  59. package/src/targets/ruby/index.js +3 -5
  60. package/src/targets/ruby/native.js +41 -35
  61. package/src/targets/shell/curl.js +108 -43
  62. package/src/{helpers/shell.js → targets/shell/helpers.js} +7 -9
  63. package/src/targets/shell/httpie.js +45 -39
  64. package/src/targets/shell/index.js +4 -5
  65. package/src/targets/shell/wget.js +20 -22
  66. package/src/targets/swift/helpers.js +44 -35
  67. package/src/targets/swift/index.js +3 -5
  68. package/src/targets/swift/nsurlsession.js +93 -79
  69. package/src/.DS_Store +0 -0
@@ -8,130 +8,123 @@
8
8
  * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9
9
  */
10
10
 
11
- 'use strict'
12
-
13
- var CodeBuilder = require('../../helpers/code-builder')
11
+ const CodeBuilder = require('../../helpers/code-builder');
14
12
 
15
13
  module.exports = function (source, options) {
16
14
  // Let's Go!
17
- var code = new CodeBuilder('\t')
15
+ const code = new CodeBuilder('\t');
18
16
 
19
17
  // Define Options
20
- var opts = Object.assign({
18
+ const opts = {
21
19
  showBoilerplate: true,
22
20
  checkErrors: false,
23
21
  printBody: true,
24
- timeout: -1
25
- }, options)
22
+ timeout: -1,
23
+ ...options,
24
+ };
26
25
 
27
- var errorPlaceholder = opts.checkErrors ? 'err' : '_'
26
+ const errorPlaceholder = opts.checkErrors ? 'err' : '_';
28
27
 
29
- var indent = opts.showBoilerplate ? 1 : 0
28
+ const indent = opts.showBoilerplate ? 1 : 0;
30
29
 
31
- var errorCheck = function () {
30
+ const errorCheck = function () {
32
31
  if (opts.checkErrors) {
33
- code.push(indent, 'if err != nil {')
32
+ code
33
+ .push(indent, 'if err != nil {')
34
34
  .push(indent + 1, 'panic(err)')
35
- .push(indent, '}')
35
+ .push(indent, '}');
36
36
  }
37
- }
37
+ };
38
38
 
39
39
  // Create boilerplate
40
40
  if (opts.showBoilerplate) {
41
- code.push('package main')
42
- .blank()
43
- .push('import (')
44
- .push(indent, '"fmt"')
41
+ code.push('package main').blank().push('import (').push(indent, '"fmt"');
45
42
 
46
43
  if (opts.timeout > 0) {
47
- code.push(indent, '"time"')
44
+ code.push(indent, '"time"');
48
45
  }
49
46
 
50
47
  if (source.postData.text) {
51
- code.push(indent, '"strings"')
48
+ code.push(indent, '"strings"');
52
49
  }
53
50
 
54
- code.push(indent, '"net/http"')
51
+ code.push(indent, '"net/http"');
55
52
 
56
53
  if (opts.printBody) {
57
- code.push(indent, '"io/ioutil"')
54
+ code.push(indent, '"io/ioutil"');
58
55
  }
59
56
 
60
- code.push(')')
61
- .blank()
62
- .push('func main() {')
63
- .blank()
57
+ code.push(')').blank().push('func main() {').blank();
64
58
  }
65
59
 
66
60
  // Create client
67
- var client
61
+ let client;
68
62
  if (opts.timeout > 0) {
69
- client = 'client'
70
- code.push(indent, 'client := http.Client{')
63
+ client = 'client';
64
+ code
65
+ .push(indent, 'client := http.Client{')
71
66
  .push(indent + 1, 'Timeout: time.Duration(%s * time.Second),', opts.timeout)
72
67
  .push(indent, '}')
73
- .blank()
68
+ .blank();
74
69
  } else {
75
- client = 'http.DefaultClient'
70
+ client = 'http.DefaultClient';
76
71
  }
77
72
 
78
- code.push(indent, 'url := "%s"', source.fullUrl)
79
- .blank()
73
+ code.push(indent, 'url := "%s"', source.fullUrl).blank();
80
74
 
81
75
  // If we have body content or not create the var and reader or nil
82
76
  if (source.postData.text) {
83
- code.push(indent, 'payload := strings.NewReader(%s)', JSON.stringify(source.postData.text))
77
+ code
78
+ .push(indent, 'payload := strings.NewReader(%s)', JSON.stringify(source.postData.text))
84
79
  .blank()
85
80
  .push(indent, 'req, %s := http.NewRequest("%s", url, payload)', errorPlaceholder, source.method)
86
- .blank()
81
+ .blank();
87
82
  } else {
88
- code.push(indent, 'req, %s := http.NewRequest("%s", url, nil)', errorPlaceholder, source.method)
89
- .blank()
83
+ code.push(indent, 'req, %s := http.NewRequest("%s", url, nil)', errorPlaceholder, source.method).blank();
90
84
  }
91
85
 
92
- errorCheck()
86
+ errorCheck();
93
87
 
94
88
  // Add headers
95
89
  if (Object.keys(source.allHeaders).length) {
96
90
  Object.keys(source.allHeaders).forEach(function (key) {
97
- code.push(indent, 'req.Header.Add("%s", "%s")', key, source.allHeaders[key])
98
- })
91
+ code.push(indent, 'req.Header.Add("%s", "%s")', key, source.allHeaders[key]);
92
+ });
99
93
 
100
- code.blank()
94
+ code.blank();
101
95
  }
102
96
 
103
97
  // Make request
104
- code.push(indent, 'res, %s := %s.Do(req)', errorPlaceholder, client)
105
- errorCheck()
98
+ code.push(indent, 'res, %s := %s.Do(req)', errorPlaceholder, client);
99
+ errorCheck();
106
100
 
107
101
  // Get Body
108
102
  if (opts.printBody) {
109
- code.blank()
103
+ code
104
+ .blank()
110
105
  .push(indent, 'defer res.Body.Close()')
111
- .push(indent, 'body, %s := ioutil.ReadAll(res.Body)', errorPlaceholder)
112
- errorCheck()
106
+ .push(indent, 'body, %s := ioutil.ReadAll(res.Body)', errorPlaceholder);
107
+ errorCheck();
113
108
  }
114
109
 
115
110
  // Print it
116
- code.blank()
117
- .push(indent, 'fmt.Println(res)')
111
+ code.blank().push(indent, 'fmt.Println(res)');
118
112
 
119
113
  if (opts.printBody) {
120
- code.push(indent, 'fmt.Println(string(body))')
114
+ code.push(indent, 'fmt.Println(string(body))');
121
115
  }
122
116
 
123
117
  // End main block
124
118
  if (opts.showBoilerplate) {
125
- code.blank()
126
- .push('}')
119
+ code.blank().push('}');
127
120
  }
128
121
 
129
- return code.join()
130
- }
122
+ return code.join();
123
+ };
131
124
 
132
125
  module.exports.info = {
133
126
  key: 'native',
134
127
  title: 'NewRequest',
135
128
  link: 'http://golang.org/pkg/net/http/#NewRequest',
136
- description: 'Golang HTTP client request'
137
- }
129
+ description: 'Golang HTTP client request',
130
+ };
@@ -9,11 +9,9 @@
9
9
  * For any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
10
10
  */
11
11
 
12
- 'use strict'
13
-
14
- var CRLF = '\r\n'
15
- var CodeBuilder = require('../../helpers/code-builder')
16
- var util = require('util')
12
+ const CRLF = '\r\n';
13
+ const CodeBuilder = require('../../helpers/code-builder');
14
+ const { format } = require('util');
17
15
 
18
16
  /**
19
17
  * Request follows the request message format in accordance to RFC 7230, Section 3.
@@ -21,44 +19,39 @@ var util = require('util')
21
19
  * See more at https://tools.ietf.org/html/rfc7230#section-3.
22
20
  */
23
21
  module.exports = function (source, options) {
24
- var opts = Object.assign(
25
- {
26
- absoluteURI: false,
27
- autoContentLength: true,
28
- autoHost: true
29
- },
30
- options
31
- )
22
+ const opts = {
23
+ absoluteURI: false,
24
+ autoContentLength: true,
25
+ autoHost: true,
26
+ ...options,
27
+ };
32
28
 
33
29
  // RFC 7230 Section 3. Message Format
34
30
  // All lines have no indentation, and should be terminated with CRLF.
35
- var code = new CodeBuilder('', CRLF)
31
+ const code = new CodeBuilder('', CRLF);
36
32
 
37
33
  // RFC 7230 Section 5.3. Request Target
38
34
  // Determines if the Request-Line should use 'absolute-form' or 'origin-form'.
39
35
  // Basically it means whether the "http://domain.com" will prepend the full url.
40
- var requestUrl = opts.absoluteURI ? source.fullUrl : source.uriObj.path
36
+ const requestUrl = opts.absoluteURI ? source.fullUrl : source.uriObj.path;
41
37
 
42
38
  // RFC 7230 Section 3.1.1. Request-Line
43
- code.push('%s %s %s', source.method, requestUrl, source.httpVersion)
39
+ code.push('%s %s %s', source.method, requestUrl, source.httpVersion);
44
40
 
45
41
  // RFC 7231 Section 5. Header Fields
46
42
  Object.keys(source.allHeaders).forEach(function (key) {
47
43
  // Capitalize header keys, even though it's not required by the spec.
48
- var keyCapitalized = key.toLowerCase().replace(/(^|-)(\w)/g, function (x) {
49
- return x.toUpperCase()
50
- })
44
+ const keyCapitalized = key.toLowerCase().replace(/(^|-)(\w)/g, function (x) {
45
+ return x.toUpperCase();
46
+ });
51
47
 
52
- code.push(
53
- '%s',
54
- util.format('%s: %s', keyCapitalized, source.allHeaders[key])
55
- )
56
- })
48
+ code.push('%s', format('%s: %s', keyCapitalized, source.allHeaders[key]));
49
+ });
57
50
 
58
51
  // RFC 7230 Section 5.4. Host
59
52
  // Automatically set Host header if option is on and on header already exists.
60
53
  if (opts.autoHost && Object.keys(source.allHeaders).indexOf('host') === -1) {
61
- code.push('Host: %s', source.uriObj.host)
54
+ code.push('Host: %s', source.uriObj.host);
62
55
  }
63
56
 
64
57
  // RFC 7230 Section 3.3.3. Message Body Length
@@ -68,32 +61,29 @@ module.exports = function (source, options) {
68
61
  source.postData.text &&
69
62
  Object.keys(source.allHeaders).indexOf('content-length') === -1
70
63
  ) {
71
- code.push(
72
- 'Content-Length: %d',
73
- Buffer.byteLength(source.postData.text, 'ascii')
74
- )
64
+ code.push('Content-Length: %d', Buffer.byteLength(source.postData.text, 'ascii'));
75
65
  }
76
66
 
77
67
  // Add extra line after header section.
78
- code.blank()
68
+ code.blank();
79
69
 
80
70
  // Separate header section and message body section.
81
- var headerSection = code.join()
82
- var messageBody = ''
71
+ const headerSection = code.join();
72
+ let messageBody = '';
83
73
 
84
74
  // RFC 7230 Section 3.3. Message Body
85
75
  if (source.postData.text) {
86
- messageBody = source.postData.text
76
+ messageBody = source.postData.text;
87
77
  }
88
78
 
89
79
  // RFC 7230 Section 3. Message Format
90
80
  // Extra CRLF separating the headers from the body.
91
- return headerSection + CRLF + messageBody
92
- }
81
+ return headerSection + CRLF + messageBody;
82
+ };
93
83
 
94
84
  module.exports.info = {
95
85
  key: '1.1',
96
86
  title: 'HTTP/1.1',
97
87
  link: 'https://tools.ietf.org/html/rfc7230',
98
- description: 'HTTP/1.1 request string in accordance with RFC 7230'
99
- }
88
+ description: 'HTTP/1.1 request string in accordance with RFC 7230',
89
+ };
@@ -1,12 +1,10 @@
1
- 'use strict'
2
-
3
1
  module.exports = {
4
2
  info: {
5
3
  key: 'http',
6
4
  title: 'HTTP',
7
5
  extname: '',
8
- default: '1.1'
6
+ default: '1.1',
9
7
  },
10
8
 
11
- '1.1': require('./http1.1')
12
- }
9
+ 1.1: require('./http1.1'),
10
+ };
@@ -1,5 +1,3 @@
1
- 'use strict'
2
-
3
1
  module.exports = {
4
2
  c: require('./c'),
5
3
  clojure: require('./clojure'),
@@ -18,5 +16,5 @@ module.exports = {
18
16
  r: require('./r'),
19
17
  ruby: require('./ruby'),
20
18
  shell: require('./shell'),
21
- swift: require('./swift')
22
- }
19
+ swift: require('./swift'),
20
+ };
@@ -8,48 +8,47 @@
8
8
  * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9
9
  */
10
10
 
11
- 'use strict'
12
-
13
- var CodeBuilder = require('../../helpers/code-builder')
11
+ const CodeBuilder = require('../../helpers/code-builder');
14
12
 
15
13
  module.exports = function (source, options) {
16
- var opts = Object.assign({
17
- indent: ' '
18
- }, options)
14
+ const opts = {
15
+ indent: ' ',
16
+ ...options,
17
+ };
19
18
 
20
- var code = new CodeBuilder(opts.indent)
19
+ const code = new CodeBuilder(opts.indent);
21
20
 
22
- code.push('AsyncHttpClient client = new DefaultAsyncHttpClient();')
21
+ code.push('AsyncHttpClient client = new DefaultAsyncHttpClient();');
23
22
 
24
- code.push(`client.prepare("${source.method.toUpperCase()}", "${source.fullUrl}")`)
23
+ code.push(`client.prepare("${source.method.toUpperCase()}", "${source.fullUrl}")`);
25
24
 
26
25
  // Add headers, including the cookies
27
- var headers = Object.keys(source.allHeaders)
26
+ const headers = Object.keys(source.allHeaders);
28
27
 
29
28
  // construct headers
30
29
  if (headers.length) {
31
30
  headers.forEach(function (key) {
32
- code.push(1, '.setHeader("%s", "%s")', key, source.allHeaders[key])
33
- })
31
+ code.push(1, '.setHeader("%s", "%s")', key, source.allHeaders[key]);
32
+ });
34
33
  }
35
34
 
36
35
  if (source.postData.text) {
37
- code.push(1, '.setBody(%s)', JSON.stringify(source.postData.text))
36
+ code.push(1, '.setBody(%s)', JSON.stringify(source.postData.text));
38
37
  }
39
38
 
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();')
39
+ code.push(1, '.execute()');
40
+ code.push(1, '.toCompletableFuture()');
41
+ code.push(1, '.thenAccept(System.out::println)');
42
+ code.push(1, '.join();');
43
+ code.blank();
44
+ code.push('client.close();');
46
45
 
47
- return code.join()
48
- }
46
+ return code.join();
47
+ };
49
48
 
50
49
  module.exports.info = {
51
50
  key: 'asynchttp',
52
51
  title: 'AsyncHttp',
53
52
  link: 'https://github.com/AsyncHttpClient/async-http-client',
54
- description: 'Asynchronous Http and WebSocket Client library for Java'
55
- }
53
+ description: 'Asynchronous Http and WebSocket Client library for Java',
54
+ };
@@ -1,15 +1,13 @@
1
- 'use strict'
2
-
3
1
  module.exports = {
4
2
  info: {
5
3
  key: 'java',
6
4
  title: 'Java',
7
5
  extname: '.java',
8
- default: 'unirest'
6
+ default: 'unirest',
9
7
  },
10
8
 
11
9
  okhttp: require('./okhttp'),
12
10
  unirest: require('./unirest'),
13
11
  asynchttp: require('./asynchttp'),
14
- nethttp: require('./nethttp')
15
- }
12
+ nethttp: require('./nethttp'),
13
+ };
@@ -8,30 +8,26 @@
8
8
  * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9
9
  */
10
10
 
11
- 'use strict'
12
-
13
- var CodeBuilder = require('../../helpers/code-builder')
11
+ const CodeBuilder = require('../../helpers/code-builder');
14
12
 
15
13
  module.exports = function (source, options) {
16
- var opts = Object.assign(
17
- {
18
- indent: ' '
19
- },
20
- options
21
- )
14
+ const opts = {
15
+ indent: ' ',
16
+ ...options,
17
+ };
22
18
 
23
- var code = new CodeBuilder(opts.indent)
19
+ const code = new CodeBuilder(opts.indent);
24
20
 
25
- code.push('HttpRequest request = HttpRequest.newBuilder()')
26
- code.push(2, '.uri(URI.create("%s"))', source.fullUrl)
21
+ code.push('HttpRequest request = HttpRequest.newBuilder()');
22
+ code.push(2, '.uri(URI.create("%s"))', source.fullUrl);
27
23
 
28
- var headers = Object.keys(source.allHeaders)
24
+ const headers = Object.keys(source.allHeaders);
29
25
 
30
26
  // construct headers
31
27
  if (headers.length) {
32
28
  headers.forEach(function (key) {
33
- code.push(2, '.header("%s", "%s")', key, source.allHeaders[key])
34
- })
29
+ code.push(2, '.header("%s", "%s")', key, source.allHeaders[key]);
30
+ });
35
31
  }
36
32
 
37
33
  if (source.postData.text) {
@@ -40,24 +36,24 @@ module.exports = function (source, options) {
40
36
  '.method("%s", HttpRequest.BodyPublishers.ofString(%s))',
41
37
  source.method.toUpperCase(),
42
38
  JSON.stringify(source.postData.text)
43
- )
39
+ );
44
40
  } else {
45
- code.push(2, '.method("%s", HttpRequest.BodyPublishers.noBody())', source.method.toUpperCase())
41
+ code.push(2, '.method("%s", HttpRequest.BodyPublishers.noBody())', source.method.toUpperCase());
46
42
  }
47
43
 
48
- code.push(2, '.build();')
44
+ code.push(2, '.build();');
49
45
 
50
46
  code.push(
51
47
  'HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());'
52
- )
53
- code.push('System.out.println(response.body());')
48
+ );
49
+ code.push('System.out.println(response.body());');
54
50
 
55
- return code.join()
56
- }
51
+ return code.join();
52
+ };
57
53
 
58
54
  module.exports.info = {
59
55
  key: 'nethttp',
60
56
  title: 'java.net.http',
61
57
  link: 'https://openjdk.java.net/groups/net/httpclient/intro.html',
62
- description: 'Java Standardized HTTP Client API'
63
- }
58
+ description: 'Java Standardized HTTP Client API',
59
+ };
@@ -8,71 +8,71 @@
8
8
  * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9
9
  */
10
10
 
11
- 'use strict'
12
-
13
- var CodeBuilder = require('../../helpers/code-builder')
11
+ const CodeBuilder = require('../../helpers/code-builder');
14
12
 
15
13
  module.exports = function (source, options) {
16
- var opts = Object.assign({
17
- indent: ' '
18
- }, options)
14
+ const opts = {
15
+ indent: ' ',
16
+ ...options,
17
+ };
19
18
 
20
- var code = new CodeBuilder(opts.indent)
19
+ const code = new CodeBuilder(opts.indent);
21
20
 
22
- var methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD']
21
+ const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD'];
23
22
 
24
- var methodsWithBody = ['POST', 'PUT', 'DELETE', 'PATCH']
23
+ const methodsWithBody = ['POST', 'PUT', 'DELETE', 'PATCH'];
25
24
 
26
- code.push('OkHttpClient client = new OkHttpClient();')
27
- .blank()
25
+ code.push('OkHttpClient client = new OkHttpClient();').blank();
28
26
 
29
27
  if (source.postData.text) {
30
28
  if (source.postData.boundary) {
31
- code.push('MediaType mediaType = MediaType.parse("%s; boundary=%s");', source.postData.mimeType, source.postData.boundary)
29
+ code.push(
30
+ 'MediaType mediaType = MediaType.parse("%s; boundary=%s");',
31
+ source.postData.mimeType,
32
+ source.postData.boundary
33
+ );
32
34
  } else {
33
- code.push('MediaType mediaType = MediaType.parse("%s");', source.postData.mimeType)
35
+ code.push('MediaType mediaType = MediaType.parse("%s");', source.postData.mimeType);
34
36
  }
35
- code.push('RequestBody body = RequestBody.create(mediaType, %s);', JSON.stringify(source.postData.text))
37
+ code.push('RequestBody body = RequestBody.create(mediaType, %s);', JSON.stringify(source.postData.text));
36
38
  }
37
39
 
38
- code.push('Request request = new Request.Builder()')
39
- code.push(1, '.url("%s")', source.fullUrl)
40
+ code.push('Request request = new Request.Builder()');
41
+ code.push(1, '.url("%s")', source.fullUrl);
40
42
  if (methods.indexOf(source.method.toUpperCase()) === -1) {
41
43
  if (source.postData.text) {
42
- code.push(1, '.method("%s", body)', source.method.toUpperCase())
44
+ code.push(1, '.method("%s", body)', source.method.toUpperCase());
43
45
  } else {
44
- code.push(1, '.method("%s", null)', source.method.toUpperCase())
46
+ code.push(1, '.method("%s", null)', source.method.toUpperCase());
45
47
  }
46
48
  } else if (methodsWithBody.indexOf(source.method.toUpperCase()) >= 0) {
47
49
  if (source.postData.text) {
48
- code.push(1, '.%s(body)', source.method.toLowerCase())
50
+ code.push(1, '.%s(body)', source.method.toLowerCase());
49
51
  } else {
50
- code.push(1, '.%s(null)', source.method.toLowerCase())
52
+ code.push(1, '.%s(null)', source.method.toLowerCase());
51
53
  }
52
54
  } else {
53
- code.push(1, '.%s()', source.method.toLowerCase())
55
+ code.push(1, '.%s()', source.method.toLowerCase());
54
56
  }
55
57
 
56
58
  // Add headers, including the cookies
57
- var headers = Object.keys(source.allHeaders)
59
+ const headers = Object.keys(source.allHeaders);
58
60
 
59
61
  // construct headers
60
62
  if (headers.length) {
61
63
  headers.forEach(function (key) {
62
- code.push(1, '.addHeader("%s", "%s")', key, source.allHeaders[key])
63
- })
64
+ code.push(1, '.addHeader("%s", "%s")', key, source.allHeaders[key]);
65
+ });
64
66
  }
65
67
 
66
- code.push(1, '.build();')
67
- .blank()
68
- .push('Response response = client.newCall(request).execute();')
68
+ code.push(1, '.build();').blank().push('Response response = client.newCall(request).execute();');
69
69
 
70
- return code.join()
71
- }
70
+ return code.join();
71
+ };
72
72
 
73
73
  module.exports.info = {
74
74
  key: 'okhttp',
75
75
  title: 'OkHttp',
76
76
  link: 'http://square.github.io/okhttp/',
77
- description: 'An HTTP Request Client Library'
78
- }
77
+ description: 'An HTTP Request Client Library',
78
+ };
@@ -8,47 +8,50 @@
8
8
  * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9
9
  */
10
10
 
11
- 'use strict'
12
-
13
- var CodeBuilder = require('../../helpers/code-builder')
11
+ const CodeBuilder = require('../../helpers/code-builder');
14
12
 
15
13
  module.exports = function (source, options) {
16
- var opts = Object.assign({
17
- indent: ' '
18
- }, options)
14
+ const opts = {
15
+ indent: ' ',
16
+ ...options,
17
+ };
19
18
 
20
- var code = new CodeBuilder(opts.indent)
19
+ const code = new CodeBuilder(opts.indent);
21
20
 
22
- var methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS' ]
21
+ const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'];
23
22
 
24
23
  if (methods.indexOf(source.method.toUpperCase()) === -1) {
25
- code.push('HttpResponse<String> response = Unirest.customMethod("%s","%s")', source.method.toUpperCase(), source.fullUrl)
24
+ code.push(
25
+ 'HttpResponse<String> response = Unirest.customMethod("%s","%s")',
26
+ source.method.toUpperCase(),
27
+ source.fullUrl
28
+ );
26
29
  } else {
27
- code.push('HttpResponse<String> response = Unirest.%s("%s")', source.method.toLowerCase(), source.fullUrl)
30
+ code.push('HttpResponse<String> response = Unirest.%s("%s")', source.method.toLowerCase(), source.fullUrl);
28
31
  }
29
32
 
30
33
  // Add headers, including the cookies
31
- var headers = Object.keys(source.allHeaders)
34
+ const headers = Object.keys(source.allHeaders);
32
35
 
33
36
  // construct headers
34
37
  if (headers.length) {
35
38
  headers.forEach(function (key) {
36
- code.push(1, '.header("%s", "%s")', key, source.allHeaders[key])
37
- })
39
+ code.push(1, '.header("%s", "%s")', key, source.allHeaders[key]);
40
+ });
38
41
  }
39
42
 
40
43
  if (source.postData.text) {
41
- code.push(1, '.body(%s)', JSON.stringify(source.postData.text))
44
+ code.push(1, '.body(%s)', JSON.stringify(source.postData.text));
42
45
  }
43
46
 
44
- code.push(1, '.asString();')
47
+ code.push(1, '.asString();');
45
48
 
46
- return code.join()
47
- }
49
+ return code.join();
50
+ };
48
51
 
49
52
  module.exports.info = {
50
53
  key: 'unirest',
51
54
  title: 'Unirest',
52
55
  link: 'http://unirest.io/java.html',
53
- description: 'Lightweight HTTP Request Client Library'
54
- }
56
+ description: 'Lightweight HTTP Request Client Library',
57
+ };