mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 13:10:44 +00:00
Working version of ApiScriptUtils. Moved actual api imlpementation out of javalin class, into implemnetation class.
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -26,7 +26,7 @@ import java.io.Serializable;
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
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.QRuntimeException;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QField;
|
||||
@ -49,7 +49,7 @@ public class APILog extends QRecordEntity
|
||||
@QField(isEditable = false)
|
||||
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;
|
||||
|
||||
@QField(possibleValueSourceName = "apiMethod")
|
||||
|
@ -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_USER = "apiLogUser";
|
||||
@ -105,21 +105,32 @@ public class APILogMetaDataProvider
|
||||
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<>();
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// 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<>();
|
||||
ApiInstanceMetaDataContainer apiInstanceMetaDataContainer = ApiInstanceMetaDataContainer.of(instance);
|
||||
for(Map.Entry<String, ApiInstanceMetaData> entry : apiInstanceMetaDataContainer.getApis().entrySet())
|
||||
{
|
||||
apiNamePossibleValues.add(new QPossibleValue<>(entry.getKey()));
|
||||
|
||||
ApiInstanceMetaData apiInstanceMetaData = entry.getValue();
|
||||
allVersions.addAll(apiInstanceMetaData.getPastVersions());
|
||||
allVersions.addAll(apiInstanceMetaData.getSupportedVersions());
|
||||
allVersions.addAll(apiInstanceMetaData.getFutureVersions());
|
||||
}
|
||||
|
||||
instance.addPossibleValueSource(new QPossibleValueSource()
|
||||
.withName("apiName")
|
||||
.withType(QPossibleValueSourceType.ENUM)
|
||||
.withEnumValues(apiNamePossibleValues));
|
||||
|
||||
for(APIVersion version : allVersions)
|
||||
{
|
||||
apiVersionPossibleValues.add(new QPossibleValue<>(version.toString()));
|
@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user