Skip to content

Commit 9ba9fae

Browse files
committed
feat: implement Sprint RestClient target
1 parent b6dd396 commit 9ba9fae

24 files changed

+291
-1
lines changed

TARGETS.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ Currently the following output targets are supported:
4040
| --------- | ------- | -------------------------------- |
4141
| `indent` | ` ` | line break & indent output value |
4242

43+
### [RestClient](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestClient.html)
44+
45+
> Spring Framework REST client
46+
47+
###### Options
48+
49+
| Option | Default | Description |
50+
| --------- | ------- | -------------------------------- |
51+
| `indent` | ` ` | line break & indent output value |
52+
4353
----
4454

4555
## JavaScript

src/targets/java/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ module.exports = {
1111
okhttp: require('./okhttp'),
1212
unirest: require('./unirest'),
1313
asynchttp: require('./asynchttp'),
14-
nethttp: require('./nethttp')
14+
nethttp: require('./nethttp'),
15+
restclient: require('./restclient')
1516
}

src/targets/java/restclient.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for Java using Spring RestClient.
4+
*
5+
* @author
6+
* @jamezrin
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+
const CodeBuilder = require('../../helpers/code-builder')
14+
15+
const standardMethods = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'TRACE']
16+
17+
module.exports = function (source, options) {
18+
const opts = Object.assign({
19+
indent: ' '
20+
}, options)
21+
22+
const code = new CodeBuilder(opts.indent)
23+
24+
code.push('RestClient restClient = RestClient.create();')
25+
.blank()
26+
27+
code.push('ResponseEntity<String> response = restClient')
28+
29+
if (standardMethods.includes(source.method.toUpperCase())) {
30+
code.push(1, '.method(HttpMethod.%s)', source.method.toUpperCase())
31+
} else {
32+
code.push(1, '.method(HttpMethod.valueOf("%s"))', source.method.toUpperCase())
33+
}
34+
35+
code.push(1, '.uri("%s")', source.fullUrl)
36+
37+
if (source.cookies && source.cookies.length) {
38+
source.cookies.forEach(function (cookie) {
39+
code.push(1, '.cookie("%s", "%s")', cookie.name, cookie.value)
40+
})
41+
}
42+
43+
const headers = Object.keys(source.allHeaders).filter(function (key) {
44+
return key.toLowerCase() !== 'cookie'
45+
})
46+
if (headers.length) {
47+
headers.forEach(function (key) {
48+
code.push(1, '.header("%s", "%qd")', key, source.allHeaders[key])
49+
})
50+
}
51+
52+
if (source.postData && source.postData.text) {
53+
if (source.postData.mimeType === 'application/json') {
54+
code.push(1, '.contentType(MediaType.APPLICATION_JSON)')
55+
code.push(1, '.body(%s)', JSON.stringify(source.postData.text))
56+
} else if (source.postData.mimeType === 'application/x-www-form-urlencoded') {
57+
code.push(1, '.contentType(MediaType.APPLICATION_FORM_URLENCODED)')
58+
code.push(1, '.body(%s)', JSON.stringify(source.postData.text))
59+
} else if (source.postData.mimeType && source.postData.mimeType.startsWith('multipart/form-data')) {
60+
code.push(1, '.contentType(MediaType.parseMediaType("multipart/form-data"))')
61+
code.push(1, '.body(%s)', JSON.stringify(source.postData.text))
62+
} else {
63+
if (source.postData.mimeType) {
64+
code.push(1, '.contentType(MediaType.parseMediaType("%s"))', source.postData.mimeType)
65+
}
66+
code.push(1, '.body(%s)', JSON.stringify(source.postData.text))
67+
}
68+
}
69+
70+
code.push(1, '.retrieve()')
71+
code.push(1, '.toEntity(String.class);')
72+
73+
return code.join()
74+
}
75+
76+
module.exports.info = {
77+
key: 'restclient',
78+
title: 'Spring RestClient',
79+
link: 'https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestClient.html',
80+
description: 'Spring Framework REST client'
81+
}

test/fixtures/available-targets.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@
226226
"title": "java.net.http",
227227
"link": "https://openjdk.java.net/groups/net/httpclient/intro.html",
228228
"description": "Java Standardized HTTP Client API"
229+
},
230+
{
231+
"key": "restclient",
232+
"title": "Spring RestClient",
233+
"link": "https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestClient.html",
234+
"description": "Spring Framework REST client"
229235
}
230236
]
231237
},
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
RestClient restClient = RestClient.create();
2+
3+
ResponseEntity<String> response = restClient
4+
.method(HttpMethod.POST)
5+
.uri("http://mockbin.com/har")
6+
.header("content-type", "application/x-www-form-urlencoded")
7+
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
8+
.body("foo=bar&hello=world")
9+
.retrieve()
10+
.toEntity(String.class);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
RestClient restClient = RestClient.create();
2+
3+
ResponseEntity<String> response = restClient
4+
.method(HttpMethod.POST)
5+
.uri("http://mockbin.com/har")
6+
.header("content-type", "application/json")
7+
.contentType(MediaType.APPLICATION_JSON)
8+
.body("{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}")
9+
.retrieve()
10+
.toEntity(String.class);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
RestClient restClient = RestClient.create();
2+
3+
ResponseEntity<String> response = restClient
4+
.method(HttpMethod.GET)
5+
.uri("http://mockbin.com/har")
6+
.header("accept-encoding", "deflate, gzip, br")
7+
.retrieve()
8+
.toEntity(String.class);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
RestClient restClient = RestClient.create();
2+
3+
ResponseEntity<String> response = restClient
4+
.method(HttpMethod.POST)
5+
.uri("http://mockbin.com/har")
6+
.cookie("foo", "bar")
7+
.cookie("bar", "baz")
8+
.retrieve()
9+
.toEntity(String.class);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
RestClient restClient = RestClient.create();
2+
3+
ResponseEntity<String> response = restClient
4+
.method(HttpMethod.valueOf("PROPFIND"))
5+
.uri("http://mockbin.com/har")
6+
.retrieve()
7+
.toEntity(String.class);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
RestClient restClient = RestClient.create();
2+
3+
ResponseEntity<String> response = restClient
4+
.method(HttpMethod.POST)
5+
.uri("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value")
6+
.cookie("foo", "bar")
7+
.cookie("bar", "baz")
8+
.header("accept", "application/json")
9+
.header("content-type", "application/x-www-form-urlencoded")
10+
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
11+
.body("foo=bar")
12+
.retrieve()
13+
.toEntity(String.class);

0 commit comments

Comments
 (0)