HttpModule.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package org.ssssssss.magicapi.modules;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.springframework.http.*;
  4. import org.springframework.util.LinkedMultiValueMap;
  5. import org.springframework.util.MultiValueMap;
  6. import org.springframework.web.client.RestTemplate;
  7. import org.ssssssss.magicapi.config.MagicModule;
  8. import org.ssssssss.script.annotation.Comment;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import java.util.Objects;
  12. import java.util.stream.Collectors;
  13. /**
  14. * http 模块
  15. *
  16. * @since 1.1.0
  17. */
  18. public class HttpModule implements MagicModule {
  19. private final RestTemplate template;
  20. private final HttpHeaders httpHeaders = new HttpHeaders();
  21. private final Class<?> responseType = Object.class;
  22. private final MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
  23. private final MultiValueMap<String, Object> data = new LinkedMultiValueMap<>();
  24. private final Map<String, ?> variables = new HashMap<>();
  25. private String url;
  26. private HttpMethod method = HttpMethod.GET;
  27. private HttpEntity<Object> entity = null;
  28. private Object requestBody;
  29. private ResponseEntity<Object> responseEntity;
  30. public HttpModule(RestTemplate template) {
  31. this.template = template;
  32. }
  33. public HttpModule(RestTemplate template, String url) {
  34. this.template = template;
  35. this.url = url;
  36. }
  37. @Override
  38. public String getModuleName() {
  39. return "http";
  40. }
  41. @Comment("创建连接")
  42. public HttpModule connection(@Comment("目标URL") String url) {
  43. return new HttpModule(template, url);
  44. }
  45. @Comment("设置URL参数")
  46. public HttpModule param(@Comment("参数名") String key, @Comment("参数值") Object... values) {
  47. if (values != null) {
  48. for (Object value : values) {
  49. this.params.add(key, value);
  50. }
  51. }
  52. return this;
  53. }
  54. @Comment("批量设置URL参数")
  55. public HttpModule param(@Comment("参数值") Map<String, Object> values) {
  56. values.forEach((key, value) -> param(key, Objects.toString(value, "")));
  57. return this;
  58. }
  59. @Comment("设置form参数")
  60. public HttpModule data(@Comment("参数名") String key, @Comment("参数值") Object... values) {
  61. if (values != null) {
  62. for (Object value : values) {
  63. this.data.add(key, value);
  64. }
  65. }
  66. return this;
  67. }
  68. @Comment("批量设置form参数")
  69. public HttpModule data(@Comment("参数值") Map<String, Object> values) {
  70. values.forEach((key, value) -> data(key, Objects.toString(value, "")));
  71. return this;
  72. }
  73. @Comment("设置header")
  74. public HttpModule header(@Comment("header名") String key, @Comment("header值") String value) {
  75. httpHeaders.add(key, value);
  76. return this;
  77. }
  78. @Comment("批量设置header")
  79. public HttpModule header(@Comment("header值") Map<String, Object> values) {
  80. values.entrySet()
  81. .stream()
  82. .filter(it -> it.getValue() != null)
  83. .forEach(entry -> header(entry.getKey(), entry.getValue().toString()));
  84. return this;
  85. }
  86. @Comment("设置请求方法,默认GET")
  87. public HttpModule method(@Comment("请求方法") HttpMethod method) {
  88. this.method = method;
  89. return this;
  90. }
  91. @Comment("设置`RequestBody`")
  92. public HttpModule body(@Comment("`RequestBody`") Object requestBody) {
  93. this.requestBody = requestBody;
  94. this.contentType(MediaType.APPLICATION_JSON);
  95. return this;
  96. }
  97. @Comment("自定义`HttpEntity`")
  98. public HttpModule entity(@Comment("`HttpEntity`") HttpEntity<Object> entity) {
  99. this.entity = entity;
  100. return this;
  101. }
  102. @Comment("设置`ContentType`")
  103. public HttpModule contentType(@Comment("Content-Type值") String contentType) {
  104. return contentType(MediaType.parseMediaType(contentType));
  105. }
  106. @Comment("设置`ContentType`")
  107. public HttpModule contentType(@Comment("Content-Type值") MediaType mediaType) {
  108. this.httpHeaders.setContentType(mediaType);
  109. return this;
  110. }
  111. @Comment("发送`POST`请求")
  112. public ResponseEntity<Object> post() {
  113. this.method(HttpMethod.POST);
  114. return this.execute();
  115. }
  116. @Comment("发送`GET`请求")
  117. public ResponseEntity<Object> get() {
  118. this.method(HttpMethod.GET);
  119. return this.execute();
  120. }
  121. @Comment("发送`PUT`请求")
  122. public ResponseEntity<Object> put() {
  123. this.method(HttpMethod.PUT);
  124. return this.execute();
  125. }
  126. @Comment("发送`DELETE`请求")
  127. public ResponseEntity<Object> delete() {
  128. this.method(HttpMethod.DELETE);
  129. return this.execute();
  130. }
  131. @Comment("执行请求")
  132. public ResponseEntity<Object> execute() {
  133. if (!this.params.isEmpty()) {
  134. String params = this.params.entrySet().stream()
  135. .map(it -> it.getValue().stream()
  136. .map(value -> it.getKey() + "=" + value)
  137. .collect(Collectors.joining("&"))
  138. ).collect(Collectors.joining("&"));
  139. if (StringUtils.isNotBlank(params)) {
  140. this.url += (this.url.contains("?") ? "&" : "?") + params;
  141. }
  142. }
  143. if (!this.data.isEmpty()) {
  144. this.entity = new HttpEntity<>(this.data, this.httpHeaders);
  145. } else if (this.entity == null && this.requestBody != null) {
  146. this.entity = new HttpEntity<>(this.requestBody, this.httpHeaders);
  147. }
  148. return template.exchange(url, this.method, entity, Object.class, responseType, variables);
  149. }
  150. }