Working version of ApiScriptUtils. Moved actual api imlpementation out of javalin class, into implemnetation class.

This commit is contained in:
2023-04-27 18:55:34 -05:00
parent 5de42b9390
commit 4003323b88
6 changed files with 1453 additions and 1000 deletions

View File

@ -22,6 +22,7 @@
package com.kingsrook.qqq.backend.core.actions.scripts; package com.kingsrook.qqq.backend.core.actions.scripts;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -55,6 +56,7 @@ import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
import com.kingsrook.qqq.backend.core.model.scripts.Script; import com.kingsrook.qqq.backend.core.model.scripts.Script;
import com.kingsrook.qqq.backend.core.model.scripts.ScriptRevision; import com.kingsrook.qqq.backend.core.model.scripts.ScriptRevision;
import com.kingsrook.qqq.backend.core.utils.CollectionUtils; import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
import static com.kingsrook.qqq.backend.core.logging.LogUtils.logPair;
/******************************************************************************* /*******************************************************************************
@ -126,7 +128,7 @@ public class RunAdHocRecordScriptAction
executeCodeInput.getContext().put("scriptUtils", input.getScriptUtils()); executeCodeInput.getContext().put("scriptUtils", input.getScriptUtils());
} }
executeCodeInput.getContext().put("api", new ScriptApi()); addApiUtilityToContext(executeCodeInput.getContext(), scriptRevision);
executeCodeInput.setCodeReference(new QCodeReference().withInlineCode(scriptRevision.getContents()).withCodeType(QCodeType.JAVA_SCRIPT)); // todo - code type as attribute of script!! executeCodeInput.setCodeReference(new QCodeReference().withInlineCode(scriptRevision.getContents()).withCodeType(QCodeType.JAVA_SCRIPT)); // todo - code type as attribute of script!!
@ -158,6 +160,34 @@ public class RunAdHocRecordScriptAction
/*******************************************************************************
** Try to (dynamically) load the ApiScriptUtils object from the api middleware
** module -- in case the runtime doesn't have that module deployed (e.g, not in
** the project pom).
*******************************************************************************/
private void addApiUtilityToContext(Map<String, Serializable> context, ScriptRevision scriptRevision)
{
try
{
Class<?> apiScriptUtilsClass = Class.forName("com.kingsrook.qqq.api.utils.ApiScriptUtils");
Object apiScriptUtilsObject = apiScriptUtilsClass.getConstructor().newInstance();
context.put("api", (Serializable) apiScriptUtilsObject);
}
catch(ClassNotFoundException e)
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// this is the only exception we're kinda expecting here - so catch for it specifically, and just log.trace - others, warn //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LOG.trace("Couldn't load ApiScriptUtils class - qqq-middleware-api not on the classpath?");
}
catch(Exception e)
{
LOG.warn("Error adding api utility to script context", e, logPair("scriptRevisionId", scriptRevision.getId()));
}
}
/******************************************************************************* /*******************************************************************************
** **
*******************************************************************************/ *******************************************************************************/

View File

@ -26,7 +26,7 @@ import java.io.Serializable;
import java.time.Instant; import java.time.Instant;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.kingsrook.qqq.api.model.metadata.APILogMetaDataProvider; import com.kingsrook.qqq.api.model.metadata.ApiInstanceMetaDataProvider;
import com.kingsrook.qqq.backend.core.exceptions.QException; import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.exceptions.QRuntimeException; import com.kingsrook.qqq.backend.core.exceptions.QRuntimeException;
import com.kingsrook.qqq.backend.core.model.data.QField; import com.kingsrook.qqq.backend.core.model.data.QField;
@ -49,7 +49,7 @@ public class APILog extends QRecordEntity
@QField(isEditable = false) @QField(isEditable = false)
private Instant timestamp; private Instant timestamp;
@QField(possibleValueSourceName = APILogMetaDataProvider.TABLE_NAME_API_LOG_USER, label = "User") @QField(possibleValueSourceName = ApiInstanceMetaDataProvider.TABLE_NAME_API_LOG_USER, label = "User")
private Integer apiLogUserId; private Integer apiLogUserId;
@QField(possibleValueSourceName = "apiMethod") @QField(possibleValueSourceName = "apiMethod")

View File

@ -50,7 +50,7 @@ import com.kingsrook.qqq.backend.core.model.metadata.tables.UniqueKey;
/******************************************************************************* /*******************************************************************************
** **
*******************************************************************************/ *******************************************************************************/
public class APILogMetaDataProvider public class ApiInstanceMetaDataProvider
{ {
public static final String TABLE_NAME_API_LOG = "apiLog"; public static final String TABLE_NAME_API_LOG = "apiLog";
public static final String TABLE_NAME_API_LOG_USER = "apiLogUser"; public static final String TABLE_NAME_API_LOG_USER = "apiLogUser";
@ -105,21 +105,32 @@ public class APILogMetaDataProvider
new QPossibleValue<>(500, "500 (Internal Server Error)") new QPossibleValue<>(500, "500 (Internal Server Error)")
))); )));
////////////////////////////////////////////////////////////////////////////
// loop over api names and versions, building out possible values sources //
////////////////////////////////////////////////////////////////////////////
List<QPossibleValue<?>> apiNamePossibleValues = new ArrayList<>();
List<QPossibleValue<?>> apiVersionPossibleValues = new ArrayList<>(); List<QPossibleValue<?>> apiVersionPossibleValues = new ArrayList<>();
//////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// todo... this, this whole thing, should probably have "which api" as another field too... ugh. // // todo... apiName should maybe be a field on apiLog table, eh? //
//////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
TreeSet<APIVersion> allVersions = new TreeSet<>(); TreeSet<APIVersion> allVersions = new TreeSet<>();
ApiInstanceMetaDataContainer apiInstanceMetaDataContainer = ApiInstanceMetaDataContainer.of(instance); ApiInstanceMetaDataContainer apiInstanceMetaDataContainer = ApiInstanceMetaDataContainer.of(instance);
for(Map.Entry<String, ApiInstanceMetaData> entry : apiInstanceMetaDataContainer.getApis().entrySet()) for(Map.Entry<String, ApiInstanceMetaData> entry : apiInstanceMetaDataContainer.getApis().entrySet())
{ {
apiNamePossibleValues.add(new QPossibleValue<>(entry.getKey()));
ApiInstanceMetaData apiInstanceMetaData = entry.getValue(); ApiInstanceMetaData apiInstanceMetaData = entry.getValue();
allVersions.addAll(apiInstanceMetaData.getPastVersions()); allVersions.addAll(apiInstanceMetaData.getPastVersions());
allVersions.addAll(apiInstanceMetaData.getSupportedVersions()); allVersions.addAll(apiInstanceMetaData.getSupportedVersions());
allVersions.addAll(apiInstanceMetaData.getFutureVersions()); allVersions.addAll(apiInstanceMetaData.getFutureVersions());
} }
instance.addPossibleValueSource(new QPossibleValueSource()
.withName("apiName")
.withType(QPossibleValueSourceType.ENUM)
.withEnumValues(apiNamePossibleValues));
for(APIVersion version : allVersions) for(APIVersion version : allVersions)
{ {
apiVersionPossibleValues.add(new QPossibleValue<>(version.toString())); apiVersionPossibleValues.add(new QPossibleValue<>(version.toString()));

View File

@ -0,0 +1,249 @@
/*
* QQQ - Low-code Application Framework for Engineers.
* Copyright (C) 2021-2023. Kingsrook, LLC
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
* contact@kingsrook.com
* https://github.com/Kingsrook/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.kingsrook.qqq.api.utils;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.kingsrook.qqq.api.actions.ApiImplementation;
import com.kingsrook.qqq.api.model.APIVersion;
import com.kingsrook.qqq.api.model.metadata.ApiInstanceMetaData;
import com.kingsrook.qqq.api.model.metadata.ApiInstanceMetaDataContainer;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QException;
/*******************************************************************************
** Object injected into script context, for interfacing with a QQQ API.
*******************************************************************************/
public class ApiScriptUtils implements Serializable
{
private String apiName;
private String apiVersion;
/*******************************************************************************
** Constructor
**
*******************************************************************************/
public ApiScriptUtils()
{
}
/*******************************************************************************
** Constructor
**
*******************************************************************************/
public ApiScriptUtils(String apiName, String apiVersion)
{
setApiName(apiName);
this.apiVersion = apiVersion;
}
/*******************************************************************************
** Setter for apiName
**
*******************************************************************************/
public void setApiName(String apiName)
{
ApiInstanceMetaDataContainer apiInstanceMetaDataContainer = ApiInstanceMetaDataContainer.of(QContext.getQInstance());
if(apiInstanceMetaDataContainer.getApis().containsKey(apiName))
{
this.apiName = apiName;
}
else
{
throw (new IllegalArgumentException("[" + apiName + "] is not a valid API name. Valid values are: " + apiInstanceMetaDataContainer.getApis().keySet()));
}
}
/*******************************************************************************
** Setter for apiVersion
**
*******************************************************************************/
public void setApiVersion(String apiVersion)
{
if(apiName == null)
{
throw (new IllegalArgumentException("You must set apiName before setting apiVersion."));
}
ApiInstanceMetaDataContainer apiInstanceMetaDataContainer = ApiInstanceMetaDataContainer.of(QContext.getQInstance());
ApiInstanceMetaData apiInstanceMetaData = apiInstanceMetaDataContainer.getApis().get(apiName);
if(apiInstanceMetaData.getSupportedVersions().contains(new APIVersion(apiVersion)))
{
this.apiVersion = apiVersion;
}
else
{
throw (new IllegalArgumentException("[" + apiVersion + "] is not a supported version for this API. Supported versions are: " + apiInstanceMetaData.getSupportedVersions()));
}
}
/*******************************************************************************
**
*******************************************************************************/
private void validateApiNameAndVersion(String description)
{
if(apiName == null || apiVersion == null)
{
throw (new IllegalStateException("Both apiName and apiVersion must be set before calling this method (" + description + ")."));
}
}
/*******************************************************************************
**
*******************************************************************************/
public Map<String, Serializable> get(String tableApiName, Object primaryKey) throws QException
{
validateApiNameAndVersion("get(" + tableApiName + "," + primaryKey + ")");
return (ApiImplementation.get(getApiInstanceMetaData(), apiVersion, tableApiName, String.valueOf(primaryKey)));
}
/*******************************************************************************
**
*******************************************************************************/
public Map<String, Serializable> query(String urlPart) throws QException
{
validateApiNameAndVersion("query(" + urlPart + ")");
String[] urlParts = urlPart.split("\\?", 2);
Map<String, List<String>> paramMap = parseQueryString(urlParts.length > 1 ? urlParts[1] : null);
return (ApiImplementation.query(getApiInstanceMetaData(), apiVersion, urlParts[0], paramMap));
}
/*******************************************************************************
**
*******************************************************************************/
public Map<String, Serializable> insert(String tableApiName, Object body) throws QException
{
validateApiNameAndVersion("insert(" + tableApiName + ")");
return (ApiImplementation.insert(getApiInstanceMetaData(), apiVersion, tableApiName, String.valueOf(body)));
}
/*******************************************************************************
**
*******************************************************************************/
public List<Map<String, Serializable>> bulkInsert(String tableApiName, Object body) throws QException
{
validateApiNameAndVersion("bulkInsert(" + tableApiName + ")");
return (ApiImplementation.bulkInsert(getApiInstanceMetaData(), apiVersion, tableApiName, String.valueOf(body)));
}
/*******************************************************************************
**
*******************************************************************************/
public void update(String tableApiName, Object primaryKey, Object body) throws QException
{
validateApiNameAndVersion("update(" + tableApiName + "," + primaryKey + ")");
ApiImplementation.update(getApiInstanceMetaData(), apiVersion, tableApiName, String.valueOf(primaryKey), String.valueOf(body));
}
/*******************************************************************************
**
*******************************************************************************/
public List<Map<String, Serializable>> bulkUpdate(String tableApiName, Object body) throws QException
{
validateApiNameAndVersion("bulkUpdate(" + tableApiName + ")");
return (ApiImplementation.bulkUpdate(getApiInstanceMetaData(), apiVersion, tableApiName, String.valueOf(body)));
}
/*******************************************************************************
**
*******************************************************************************/
public void delete(String tableApiName, Object primaryKey) throws QException
{
validateApiNameAndVersion("delete(" + tableApiName + "," + primaryKey + ")");
ApiImplementation.delete(getApiInstanceMetaData(), apiVersion, tableApiName, String.valueOf(primaryKey));
}
/*******************************************************************************
**
*******************************************************************************/
public List<Map<String, Serializable>> bulkDelete(String tableApiName, Object body) throws QException
{
validateApiNameAndVersion("bulkDelete(" + tableApiName + ")");
return (ApiImplementation.bulkDelete(getApiInstanceMetaData(), apiVersion, tableApiName, String.valueOf(body)));
}
/*******************************************************************************
**
*******************************************************************************/
private ApiInstanceMetaData getApiInstanceMetaData()
{
ApiInstanceMetaDataContainer apiInstanceMetaDataContainer = ApiInstanceMetaDataContainer.of(QContext.getQInstance());
ApiInstanceMetaData apiInstanceMetaData = apiInstanceMetaDataContainer.getApiInstanceMetaData(apiName);
return apiInstanceMetaData;
}
/*******************************************************************************
**
*******************************************************************************/
private static Map<String, List<String>> parseQueryString(String queryString)
{
Map<String, List<String>> paramMap = new LinkedHashMap<>();
if(queryString != null)
{
for(String nameValuePair : queryString.split("&"))
{
String[] nameValue = nameValuePair.split("=", 2);
if(nameValue.length == 2)
{
paramMap.computeIfAbsent(nameValue[0], (k) -> new ArrayList<>());
paramMap.get(nameValue[0]).add(nameValue[1]);
}
}
}
return paramMap;
}
}