|
| 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 | +} |
0 commit comments