ApiInfo.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package org.ssssssss.magicapi.model;
  2. import com.fasterxml.jackson.databind.JsonNode;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.Objects;
  7. /**
  8. * 接口信息
  9. */
  10. public class ApiInfo extends MagicEntity{
  11. /**
  12. * 请求方法
  13. */
  14. private String method = "GET";
  15. /**
  16. * 请求路径
  17. */
  18. private String path;
  19. /**
  20. * 设置的请求参数
  21. */
  22. private String parameter;
  23. /**
  24. * 设置的接口选项
  25. */
  26. private String option;
  27. /**
  28. * 请求体
  29. */
  30. private String requestBody;
  31. /**
  32. * 请求头
  33. */
  34. private String requestHeader;
  35. /**
  36. * 输出结果
  37. */
  38. private String responseBody;
  39. /**
  40. * 接口描述
  41. */
  42. private String description;
  43. /**
  44. * 接口选项json
  45. */
  46. private JsonNode jsonNode;
  47. public String getMethod() {
  48. return method;
  49. }
  50. public void setMethod(String method) {
  51. this.method = method;
  52. }
  53. public String getPath() {
  54. return path;
  55. }
  56. public void setPath(String path) {
  57. this.path = path;
  58. }
  59. public String getParameter() {
  60. return parameter;
  61. }
  62. public void setParameter(String parameter) {
  63. this.parameter = parameter;
  64. }
  65. public String getResponseBody() {
  66. return responseBody;
  67. }
  68. public String getRequestBody() {
  69. return requestBody;
  70. }
  71. public void setRequestBody(String requestBody) {
  72. this.requestBody = requestBody;
  73. }
  74. public String getRequestHeader() {
  75. return requestHeader;
  76. }
  77. public void setRequestHeader(String requestHeader) {
  78. this.requestHeader = requestHeader;
  79. }
  80. public void setResponseBody(String responseBody) {
  81. this.responseBody = responseBody;
  82. }
  83. public Map<String, Object> getOptionMap() {
  84. Map<String, Object> map = new HashMap<>();
  85. if (this.jsonNode == null) {
  86. return null;
  87. } else if (this.jsonNode.isArray()) {
  88. for (JsonNode node : this.jsonNode) {
  89. map.put(node.get("name").asText(), node.get("value").asText());
  90. }
  91. } else {
  92. this.jsonNode.fieldNames().forEachRemaining(it -> map.put(it, this.jsonNode.get(it)));
  93. }
  94. return map;
  95. }
  96. public String getDescription() {
  97. return description;
  98. }
  99. public void setDescription(String description) {
  100. this.description = description;
  101. }
  102. public String getOption() {
  103. return option;
  104. }
  105. public void setOptionValue(String optionValue) {
  106. this.setOption(optionValue);
  107. }
  108. public void setOption(String option) {
  109. this.option = option;
  110. try {
  111. this.jsonNode = new ObjectMapper().readTree(option);
  112. } catch (Throwable ignored) {
  113. }
  114. }
  115. public Object getOptionValue(String key) {
  116. if (this.jsonNode == null) {
  117. return null;
  118. }
  119. if (this.jsonNode.isArray()) {
  120. for (JsonNode node : this.jsonNode) {
  121. if (node.isObject() && Objects.equals(key, node.get("name").asText())) {
  122. return node.get("value").asText();
  123. }
  124. }
  125. } else if (this.jsonNode.isObject()) {
  126. return this.jsonNode.get(key).asText();
  127. }
  128. return null;
  129. }
  130. @Override
  131. public boolean equals(Object o) {
  132. if (this == o) return true;
  133. if (o == null || getClass() != o.getClass()) return false;
  134. ApiInfo apiInfo = (ApiInfo) o;
  135. return Objects.equals(id, apiInfo.id) &&
  136. Objects.equals(method, apiInfo.method) &&
  137. Objects.equals(path, apiInfo.path) &&
  138. Objects.equals(script, apiInfo.script) &&
  139. Objects.equals(name, apiInfo.name) &&
  140. Objects.equals(groupId, apiInfo.groupId) &&
  141. Objects.equals(parameter, apiInfo.parameter) &&
  142. Objects.equals(option, apiInfo.option) &&
  143. Objects.equals(requestBody, apiInfo.requestBody) &&
  144. Objects.equals(requestHeader, apiInfo.requestHeader) &&
  145. Objects.equals(responseBody, apiInfo.responseBody) &&
  146. Objects.equals(description, apiInfo.description);
  147. }
  148. @Override
  149. public int hashCode() {
  150. return Objects.hash(id, method, path, script, name, groupId, parameter, option, requestBody, requestHeader, responseBody, description);
  151. }
  152. public ApiInfo copy() {
  153. ApiInfo info = new ApiInfo();
  154. info.setId(this.id);
  155. info.setMethod(this.method);
  156. info.setName(this.name);
  157. info.setPath(this.path);
  158. info.setScript(this.script);
  159. info.setGroupId(this.groupId);
  160. info.setParameter(parameter);
  161. info.setOption(this.option);
  162. info.setRequestBody(this.requestBody);
  163. info.setRequestHeader(this.requestHeader);
  164. info.setResponseBody(this.responseBody);
  165. info.setDescription(this.description);
  166. return info;
  167. }
  168. }