mirror of
https://gitee.com/ssssssss-team/magic-api.git
synced 2026-06-09 18:32:16 +08:00
增加调用函数的方法。
This commit is contained in:
@@ -136,6 +136,10 @@ public class MagicFunctionManager {
|
||||
return mappings.values().stream().distinct().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public FunctionInfo getFunctionInfo(String path) {
|
||||
return mappings.get(path);
|
||||
}
|
||||
|
||||
private boolean hasConflict(TreeNode<Group> group, String newPath) {
|
||||
// 获取要移动的接口
|
||||
List<FunctionInfo> infos = mappings.values().stream()
|
||||
|
||||
@@ -19,7 +19,7 @@ public interface MagicAPIService extends MagicModule {
|
||||
*
|
||||
* @param method 请求方法
|
||||
* @param path 请求路径
|
||||
* @param context 请求上下文,主要给脚本中使用
|
||||
* @param context 变量信息
|
||||
*/
|
||||
Object execute(String method, String path, Map<String, Object> context);
|
||||
|
||||
@@ -28,10 +28,18 @@ public interface MagicAPIService extends MagicModule {
|
||||
*
|
||||
* @param method 请求方法
|
||||
* @param path 请求路径
|
||||
* @param context 请求上下文,主要给脚本中使用
|
||||
* @param context 变量信息
|
||||
*/
|
||||
Object call(String method, String path, Map<String, Object> context);
|
||||
|
||||
/**
|
||||
* 执行MagicAPI中的函数
|
||||
*
|
||||
* @param path 函数路径
|
||||
* @param context 变量信息
|
||||
*/
|
||||
Object invoke(String path, Map<String, Object> context);
|
||||
|
||||
/**
|
||||
* 保存接口
|
||||
*
|
||||
|
||||
@@ -180,6 +180,19 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(String path, Map<String, Object> context) {
|
||||
FunctionInfo functionInfo = magicFunctionManager.getFunctionInfo(path);
|
||||
if (functionInfo == null) {
|
||||
throw new MagicServiceException(String.format("找不到对应函数 [%s]", path));
|
||||
}
|
||||
MagicScriptContext scriptContext = new MagicScriptContext(context);
|
||||
scriptContext.putMapIntoContext(context);
|
||||
SimpleScriptContext simpleScriptContext = new SimpleScriptContext();
|
||||
simpleScriptContext.setAttribute(MagicScript.CONTEXT_ROOT, scriptContext, ScriptContext.ENGINE_SCOPE);
|
||||
return ((MagicScript) ScriptManager.compile("MagicScript", functionInfo.getScript())).eval(simpleScriptContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String saveApi(ApiInfo info) {
|
||||
// 非空验证
|
||||
|
||||
Reference in New Issue
Block a user