mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-17 20:50:44 +00:00
Merge pull request #30 from Kingsrook/integration/sprint-29
Integration/sprint 29
This commit is contained in:
@ -0,0 +1,136 @@
|
||||
/*
|
||||
* 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.backend.module.api.actions;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QRuntimeException;
|
||||
import com.kingsrook.qqq.backend.core.logging.QLogger;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
||||
import com.kingsrook.qqq.backend.core.utils.StringUtils;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import static com.kingsrook.qqq.backend.core.logging.LogUtils.logPair;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public class APIRecordUtils
|
||||
{
|
||||
private static final QLogger LOG = QLogger.getLogger(APIRecordUtils.class);
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Take a QRecord whose field names are formatted in JSONQuery-style
|
||||
** (e.g., 'key' or 'key.subKey' or 'key[index].subKey')
|
||||
** and convert it to a JSONObject.
|
||||
*******************************************************************************/
|
||||
public static JSONObject jsonQueryStyleQRecordToJSONObject(QTableMetaData table, QRecord record, boolean includeNonTableFields)
|
||||
{
|
||||
try
|
||||
{
|
||||
JSONObject body = new JSONObject();
|
||||
for(Map.Entry<String, Serializable> entry : record.getValues().entrySet())
|
||||
{
|
||||
String fieldName = entry.getKey();
|
||||
Serializable value = entry.getValue();
|
||||
|
||||
if(fieldName.contains("."))
|
||||
{
|
||||
JSONObject tmp = body;
|
||||
String[] parts = fieldName.split("\\.");
|
||||
for(int i = 0; i < parts.length - 1; i++)
|
||||
{
|
||||
String thisPart = parts[i];
|
||||
if(thisPart.contains("["))
|
||||
{
|
||||
String arrayName = thisPart.replaceFirst("\\[.*", "");
|
||||
if(!tmp.has(arrayName))
|
||||
{
|
||||
tmp.put(arrayName, new JSONArray());
|
||||
}
|
||||
|
||||
JSONArray array = tmp.getJSONArray(arrayName);
|
||||
Integer arrayIndex = Integer.parseInt(thisPart.replaceFirst(".*\\[", "").replaceFirst("].*", ""));
|
||||
if(array.opt(arrayIndex) == null)
|
||||
{
|
||||
array.put(arrayIndex, new JSONObject());
|
||||
}
|
||||
tmp = array.getJSONObject(arrayIndex);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!tmp.has(thisPart))
|
||||
{
|
||||
tmp.put(thisPart, new JSONObject());
|
||||
}
|
||||
tmp = tmp.getJSONObject(thisPart);
|
||||
}
|
||||
}
|
||||
tmp.put(parts[parts.length - 1], value);
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
QFieldMetaData field = table.getField(fieldName);
|
||||
body.put(getFieldBackendName(field), value);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
if(includeNonTableFields)
|
||||
{
|
||||
LOG.debug("Putting non-table-field in record", logPair("name", fieldName));
|
||||
body.put(fieldName, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return body;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
throw (new QRuntimeException("Error converting record to JSON Object", e));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private static String getFieldBackendName(QFieldMetaData field)
|
||||
{
|
||||
String backendName = field.getBackendName();
|
||||
if(!StringUtils.hasContent(backendName))
|
||||
{
|
||||
backendName = field.getName();
|
||||
}
|
||||
return (backendName);
|
||||
}
|
||||
|
||||
}
|
@ -682,23 +682,13 @@ public class BaseAPIActionUtil
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
switch(backendMetaData.getAuthorizationType())
|
||||
{
|
||||
case BASIC_AUTH_API_KEY:
|
||||
request.addHeader("Authorization", getBasicAuthenticationHeader(backendMetaData.getApiKey()));
|
||||
break;
|
||||
|
||||
case BASIC_AUTH_USERNAME_PASSWORD:
|
||||
request.addHeader("Authorization", getBasicAuthenticationHeader(backendMetaData.getUsername(), backendMetaData.getPassword()));
|
||||
break;
|
||||
|
||||
case API_KEY_HEADER:
|
||||
request.addHeader("API-Key", backendMetaData.getApiKey());
|
||||
break;
|
||||
|
||||
case OAUTH2:
|
||||
request.setHeader("Authorization", "Bearer " + getOAuth2Token());
|
||||
break;
|
||||
|
||||
case API_KEY_QUERY_PARAM:
|
||||
case BASIC_AUTH_API_KEY -> request.addHeader("Authorization", getBasicAuthenticationHeader(backendMetaData.getApiKey()));
|
||||
case BASIC_AUTH_USERNAME_PASSWORD -> request.addHeader("Authorization", getBasicAuthenticationHeader(backendMetaData.getUsername(), backendMetaData.getPassword()));
|
||||
case API_KEY_HEADER -> request.addHeader("API-Key", backendMetaData.getApiKey());
|
||||
case API_TOKEN -> request.addHeader("Authorization", "Token " + backendMetaData.getApiKey());
|
||||
case OAUTH2 -> request.setHeader("Authorization", "Bearer " + getOAuth2Token());
|
||||
case API_KEY_QUERY_PARAM ->
|
||||
{
|
||||
try
|
||||
{
|
||||
String uri = request.getURI().toString();
|
||||
@ -710,10 +700,8 @@ public class BaseAPIActionUtil
|
||||
{
|
||||
throw (new QException("Error setting authorization query parameter", e));
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new IllegalArgumentException("Unexpected authorization type: " + backendMetaData.getAuthorizationType());
|
||||
}
|
||||
default -> throw new IllegalArgumentException("Unexpected authorization type: " + backendMetaData.getAuthorizationType());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,6 +28,7 @@ package com.kingsrook.qqq.backend.module.api.model;
|
||||
public enum AuthorizationType
|
||||
{
|
||||
API_KEY_HEADER,
|
||||
API_TOKEN,
|
||||
BASIC_AUTH_API_KEY,
|
||||
BASIC_AUTH_USERNAME_PASSWORD,
|
||||
OAUTH2,
|
||||
|
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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.backend.module.api.actions;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.context.QContext;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
||||
import com.kingsrook.qqq.backend.module.api.BaseTest;
|
||||
import com.kingsrook.qqq.backend.module.api.TestUtils;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Unit test for APIRecordUtils
|
||||
*******************************************************************************/
|
||||
class APIRecordUtilsTest extends BaseTest
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void test()
|
||||
{
|
||||
QTableMetaData table = QContext.getQInstance().getTable(TestUtils.MOCK_TABLE_NAME);
|
||||
table.withField(new QFieldMetaData("myField", QFieldType.INTEGER).withBackendName("myBackendName"));
|
||||
|
||||
QRecord record = new QRecord()
|
||||
.withValue("foo", 1)
|
||||
.withValue("bar.baz", 2)
|
||||
.withValue("list[0].a", 3)
|
||||
.withValue("list[1].a", 4)
|
||||
.withValue("list[1].b", 5)
|
||||
.withValue("myField", 6);
|
||||
|
||||
JSONObject jsonObject = APIRecordUtils.jsonQueryStyleQRecordToJSONObject(table, record, true);
|
||||
assertEquals(1, jsonObject.getInt("foo"));
|
||||
assertEquals(2, jsonObject.getJSONObject("bar").getInt("baz"));
|
||||
assertEquals(3, jsonObject.getJSONArray("list").getJSONObject(0).getInt("a"));
|
||||
assertEquals(4, jsonObject.getJSONArray("list").getJSONObject(1).getInt("a"));
|
||||
assertEquals(5, jsonObject.getJSONArray("list").getJSONObject(1).getInt("b"));
|
||||
assertEquals(6, jsonObject.getInt("myBackendName"));
|
||||
assertFalse(jsonObject.has("myField"));
|
||||
|
||||
///////////////////////////////////////////////////////
|
||||
// if we say "false" for includeNonTableFields, then //
|
||||
// we should only get the myField field //
|
||||
///////////////////////////////////////////////////////
|
||||
jsonObject = APIRecordUtils.jsonQueryStyleQRecordToJSONObject(table, record, false);
|
||||
assertFalse(jsonObject.has("foo"));
|
||||
assertEquals(6, jsonObject.getInt("myBackendName"));
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user