스크립트 추가
This commit is contained in:
@@ -22,6 +22,10 @@ public class ApiRequestDTO {
|
||||
|
||||
private String requestBody;
|
||||
|
||||
private String preRequestScript;
|
||||
|
||||
private String postRequestScript;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
@@ -93,4 +97,20 @@ public class ApiRequestDTO {
|
||||
public void setRequestBody(String requestBody) {
|
||||
this.requestBody = requestBody;
|
||||
}
|
||||
|
||||
public String getPreRequestScript() {
|
||||
return preRequestScript;
|
||||
}
|
||||
|
||||
public void setPreRequestScript(String preRequestScript) {
|
||||
this.preRequestScript = preRequestScript;
|
||||
}
|
||||
|
||||
public String getPostRequestScript() {
|
||||
return postRequestScript;
|
||||
}
|
||||
|
||||
public void setPostRequestScript(String postRequestScript) {
|
||||
this.postRequestScript = postRequestScript;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,13 @@ public class ApiRequestInfo {
|
||||
@Lob
|
||||
private String requestBody;
|
||||
|
||||
@Lob
|
||||
private String preRequestScript;
|
||||
|
||||
@Lob
|
||||
private String postRequestScript;
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
@@ -120,6 +127,22 @@ public class ApiRequestInfo {
|
||||
this.requestBody = requestBody;
|
||||
}
|
||||
|
||||
public String getPreRequestScript() {
|
||||
return preRequestScript;
|
||||
}
|
||||
|
||||
public void setPreRequestScript(String preRequestScript) {
|
||||
this.preRequestScript = preRequestScript;
|
||||
}
|
||||
|
||||
public String getPostRequestScript() {
|
||||
return postRequestScript;
|
||||
}
|
||||
|
||||
public void setPostRequestScript(String postRequestScript) {
|
||||
this.postRequestScript = postRequestScript;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
@@ -130,7 +153,7 @@ public class ApiRequestInfo {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, server, owner, method, path, headers, queryParams, requestBody);
|
||||
return Objects.hash(id, server, owner, method, path, headers, queryParams, requestBody, preRequestScript, postRequestScript);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -70,6 +70,9 @@ public class ApiRequestMgmtService {
|
||||
existing.setVariables(api.getVariables());
|
||||
existing.setServer(api.getServer());
|
||||
existing.setRequestBody(api.getRequestBody());
|
||||
existing.setPreRequestScript(api.getPreRequestScript());
|
||||
existing.setPostRequestScript(api.getPostRequestScript());
|
||||
|
||||
apiRequestRepository.save(existing);
|
||||
api = existing;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ public class NettyApiClient {
|
||||
ServerRepository serverRepository;
|
||||
|
||||
public CompletableFuture<ApiResponse> handleRequest(ApiRequestDTO originalRequest) throws InterruptedException, InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException {
|
||||
ApiRequestDTO apiRequest = replacePlaceholdersWithVariables(originalRequest);
|
||||
ApiRequestDTO apiRequest = originalRequest;
|
||||
Server server = serverRepository.findById(apiRequest.getServer()).orElseThrow(() -> new ServerNotFoundException("Server not found"));
|
||||
String host = server.getHostname();
|
||||
int port = server.getPort();
|
||||
@@ -108,37 +108,4 @@ public class NettyApiClient {
|
||||
return responseFuture;
|
||||
}
|
||||
|
||||
private ApiRequestDTO replacePlaceholdersWithVariables(ApiRequestDTO original) throws InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException {
|
||||
ApiRequestDTO request = (ApiRequestDTO) BeanUtils.cloneBean(original);
|
||||
if (request.getVariables() != null) {
|
||||
for (ApiRequestKeyValueDTO variable : request.getVariables()) {
|
||||
if (variable.isEnabled()) {
|
||||
String placeholder = "{{" + variable.getKey() + "}}";
|
||||
// Replace in path
|
||||
request.setPath(request.getPath().replace(placeholder, variable.getValue()));
|
||||
// Replace in requestBody
|
||||
if (request.getRequestBody() != null) {
|
||||
request.setRequestBody(request.getRequestBody().replace(placeholder, variable.getValue()));
|
||||
}
|
||||
|
||||
// Replace in headers
|
||||
if (request.getHeaders() != null) {
|
||||
for (ApiRequestKeyValueDTO header : request.getHeaders()) {
|
||||
header.setKey(header.getKey().replace(placeholder, variable.getValue()));
|
||||
header.setValue(header.getValue().replace(placeholder, variable.getValue()));
|
||||
}
|
||||
}
|
||||
// Replace in queryParams
|
||||
if (request.getQueryParams() != null) {
|
||||
for (ApiRequestKeyValueDTO queryParam : request.getQueryParams()) {
|
||||
queryParam.setKey(queryParam.getKey().replace(placeholder, variable.getValue()));
|
||||
queryParam.setValue(queryParam.getValue().replace(placeholder, variable.getValue()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user