From 49ebd3d296fd1abb5a25cbd9ab0795baf7412877 Mon Sep 17 00:00:00 2001 From: mxd <838425805@qq.com> Date: Wed, 14 Apr 2021 22:12:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E`http`=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ssssssss/magicapi/modules/HttpModule.java | 161 ++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 src/main/java/org/ssssssss/magicapi/modules/HttpModule.java diff --git a/src/main/java/org/ssssssss/magicapi/modules/HttpModule.java b/src/main/java/org/ssssssss/magicapi/modules/HttpModule.java new file mode 100644 index 00000000..aab6d622 --- /dev/null +++ b/src/main/java/org/ssssssss/magicapi/modules/HttpModule.java @@ -0,0 +1,161 @@ +package org.ssssssss.magicapi.modules; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.http.*; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; +import org.ssssssss.magicapi.config.MagicModule; +import org.ssssssss.script.annotation.Comment; + +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * http 模块 + * @since 1.1.0 + */ +public class HttpModule implements MagicModule { + + private final RestTemplate template; + + private String url; + + private final HttpHeaders httpHeaders = new HttpHeaders(); + + private HttpMethod method = HttpMethod.GET; + + private HttpEntity entity = null; + + private Object requestBody; + + private final Class responseType = Object.class; + + private final MultiValueMap params = new LinkedMultiValueMap<>(); + + private final MultiValueMap data = new LinkedMultiValueMap<>(); + + private final Map variables = new HashMap<>(); + + private ResponseEntity responseEntity; + + public HttpModule(RestTemplate template) { + this.template = template; + } + + public HttpModule(RestTemplate template, String url) { + this.template = template; + this.url = url; + } + + @Override + public String getModuleName() { + return "http"; + } + + @Comment("创建连接") + public HttpModule connection(@Comment("目标URL") String url) { + return new HttpModule(template, url); + } + + @Comment("设置URL参数") + public HttpModule param(@Comment("参数名") String key, @Comment("参数值") Object... values) { + if (values != null) { + for (Object value : values) { + this.params.add(key, value); + } + } + return this; + } + + @Comment("设置form参数") + public HttpModule data(@Comment("参数名") String key, @Comment("参数值") Object... values) { + if (values != null) { + for (Object value : values) { + this.data.add(key, value); + } + } + return this; + } + + @Comment("设置header") + public HttpModule header(@Comment("header名") String key, @Comment("header值") String value) { + httpHeaders.add(key, value); + return this; + } + + @Comment("设置请求方法,默认GET") + public HttpModule method(@Comment("请求方法") HttpMethod method) { + this.method = method; + return this; + } + + @Comment("设置`RequestBody`") + public HttpModule body(@Comment("`RequestBody`") Object requestBody) { + this.requestBody = requestBody; + this.contentType(MediaType.APPLICATION_JSON); + return this; + } + + @Comment("自定义`HttpEntity`") + public HttpModule entity(@Comment("`HttpEntity`")HttpEntity entity) { + this.entity = entity; + return this; + } + + @Comment("设置`ContentType`") + public HttpModule contentType(@Comment("Content-Type值") String contentType) { + return contentType(MediaType.parseMediaType(contentType)); + } + + @Comment("设置`ContentType`") + public HttpModule contentType(@Comment("Content-Type值") MediaType mediaType) { + this.httpHeaders.setContentType(mediaType); + return this; + } + + @Comment("发送`POST`请求") + public ResponseEntity post() { + this.method(HttpMethod.POST); + return this.execute(); + } + + @Comment("发送`GET`请求") + public ResponseEntity get() { + this.method(HttpMethod.GET); + return this.execute(); + } + + @Comment("发送`PUT`请求") + public ResponseEntity put() { + this.method(HttpMethod.PUT); + return this.execute(); + } + + @Comment("发送`DELETE`请求") + public ResponseEntity delete() { + this.method(HttpMethod.DELETE); + return this.execute(); + } + + @Comment("执行请求") + public ResponseEntity execute() { + if (!this.params.isEmpty()) { + String params = this.params.entrySet().stream() + .map(it -> it.getValue().stream() + .map(value -> it.getKey() + "=" + value) + .collect(Collectors.joining("&")) + ).collect(Collectors.joining("&")); + if (StringUtils.isNotBlank(params)) { + this.url += (this.url.contains("?") ? "&" : "?") + params; + } + } + if (!this.data.isEmpty()) { + this.entity = new HttpEntity<>(this.data, this.httpHeaders); + } else if (this.entity == null && this.requestBody != null) { + this.entity = new HttpEntity<>(this.requestBody, this.httpHeaders); + } + return template.exchange(url, this.method, entity, Object.class, responseType, variables); + } +}