HttpModule.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. public HttpModule(RestTemplate template) {
  30. this.template = template;
  31. }
  32. public HttpModule(RestTemplate template, String url) {
  33. this.template = template;
  34. this.url = url;
  35. }
  36. @Override
  37. public String getModuleName() {
  38. return "http";
  39. }
  40. @Comment("创建连接")
  41. public HttpModule connect(@Comment("目标URL") String url) {
  42. return new HttpModule(template, url);
  43. }
  44. @Comment("设置URL参数")
  45. public HttpModule param(@Comment("参数名") String key, @Comment("参数值") Object... values) {
  46. if (values != null) {
  47. for (Object value : values) {
  48. this.params.add(key, value);
  49. }
  50. }
  51. return this;
  52. }
  53. @Comment("批量设置URL参数")
  54. public HttpModule param(@Comment("参数值") Map<String, Object> values) {
  55. values.forEach((key, value) -> param(key, Objects.toString(value, "")));
  56. return this;
  57. }
  58. @Comment("设置form参数")
  59. public HttpModule data(@Comment("参数名") String key, @Comment("参数值") Object... values) {
  60. if (values != null) {
  61. for (Object value : values) {
  62. this.data.add(key, value);
  63. }
  64. }
  65. return this;
  66. }
  67. @Comment("批量设置form参数")
  68. public HttpModule data(@Comment("参数值") Map<String, Object> values) {
  69. values.forEach((key, value) -> data(key, Objects.toString(value, "")));
  70. return this;
  71. }
  72. @Comment("设置header")
  73. public HttpModule header(@Comment("header名") String key, @Comment("header值") String value) {
  74. httpHeaders.add(key, value);
  75. return this;
  76. }
  77. @Comment("批量设置header")
  78. public HttpModule header(@Comment("header值") Map<String, Object> values) {
  79. values.entrySet()
  80. .stream()
  81. .filter(it -> it.getValue() != null)
  82. .forEach(entry -> header(entry.getKey(), entry.getValue().toString()));
  83. return this;
  84. }
  85. @Comment("设置请求方法,默认GET")
  86. public HttpModule method(@Comment("请求方法") HttpMethod method) {
  87. this.method = method;
  88. return this;
  89. }
  90. @Comment("设置`RequestBody`")
  91. public HttpModule body(@Comment("`RequestBody`") Object requestBody) {
  92. this.requestBody = requestBody;
  93. this.contentType(MediaType.APPLICATION_JSON);
  94. return this;
  95. }
  96. @Comment("自定义`HttpEntity`")
  97. public HttpModule entity(@Comment("`HttpEntity`") HttpEntity<Object> entity) {
  98. this.entity = entity;
  99. return this;
  100. }
  101. @Comment("设置`ContentType`")
  102. public HttpModule contentType(@Comment("Content-Type值") String contentType) {
  103. return contentType(MediaType.parseMediaType(contentType));
  104. }
  105. @Comment("设置`ContentType`")
  106. public HttpModule contentType(@Comment("Content-Type值") MediaType mediaType) {
  107. this.httpHeaders.setContentType(mediaType);
  108. return this;
  109. }
  110. @Comment("发送`POST`请求")
  111. public ResponseEntity<Object> post() {
  112. this.method(HttpMethod.POST);
  113. return this.execute();
  114. }
  115. @Comment("发送`GET`请求")
  116. public ResponseEntity<Object> get() {
  117. this.method(HttpMethod.GET);
  118. return this.execute();
  119. }
  120. @Comment("发送`PUT`请求")
  121. public ResponseEntity<Object> put() {
  122. this.method(HttpMethod.PUT);
  123. return this.execute();
  124. }
  125. @Comment("发送`DELETE`请求")
  126. public ResponseEntity<Object> delete() {
  127. this.method(HttpMethod.DELETE);
  128. return this.execute();
  129. }
  130. @Comment("执行请求")
  131. public ResponseEntity<Object> execute() {
  132. if (!this.params.isEmpty()) {
  133. String params = this.params.entrySet().stream()
  134. .map(it -> it.getValue().stream()
  135. .map(value -> it.getKey() + "=" + value)
  136. .collect(Collectors.joining("&"))
  137. ).collect(Collectors.joining("&"));
  138. if (StringUtils.isNotBlank(params)) {
  139. this.url += (this.url.contains("?") ? "&" : "?") + params;
  140. }
  141. }
  142. if (!this.data.isEmpty()) {
  143. this.entity = new HttpEntity<>(this.data, this.httpHeaders);
  144. } else if (this.entity == null && this.requestBody != null) {
  145. this.entity = new HttpEntity<>(this.requestBody, this.httpHeaders);
  146. }
  147. return template.exchange(url, this.method, entity, Object.class, responseType, variables);
  148. }
  149. }