diff --git a/libai/pom.xml b/libai/pom.xml index bec9f85..2aacb53 100644 --- a/libai/pom.xml +++ b/libai/pom.xml @@ -11,7 +11,9 @@ ai.api libai-base .. + 1.4.8 + libai diff --git a/libai/src/main/java/ai/api/AIConfiguration.java b/libai/src/main/java/ai/api/AIConfiguration.java index bf93c1b..2561c8a 100644 --- a/libai/src/main/java/ai/api/AIConfiguration.java +++ b/libai/src/main/java/ai/api/AIConfiguration.java @@ -60,6 +60,7 @@ public class AIConfiguration implements Cloneable { protected static final String CURRENT_PROTOCOL_VERSION = "20150910"; protected static final String QUESTION_ENDPOINT = "query"; protected static final String USER_ENTITIES_ENDPOINT = "userEntities"; + protected static final String ENTITIES_ENDPOINT = "entities"; protected static final String CONTEXTS_ENDPOINT = "contexts"; private final String apiKey; @@ -204,20 +205,23 @@ public AIConfiguration clone() { } String getQuestionUrl(final String sessionId) { - if (StringUtils.isEmpty(protocolVersion)) { - return String.format("%s%s?sessionId=%s", serviceUrl, QUESTION_ENDPOINT, sessionId); - } else { - return String.format("%s%s?v=%s&sessionId=%s", serviceUrl, QUESTION_ENDPOINT, protocolVersion, - sessionId); - } + return formatEndpoint(sessionId, protocolVersion, serviceUrl, QUESTION_ENDPOINT); } String getUserEntitiesEndpoint(final String sessionId) { + return formatEndpoint(sessionId, protocolVersion, serviceUrl, USER_ENTITIES_ENDPOINT); + } + + String getEntitiesEndpoint(final String sessionId) { + return formatEndpoint(sessionId, protocolVersion, serviceUrl, ENTITIES_ENDPOINT); + } + + private String formatEndpoint(String sessionId, String protocolVersion, String serviceUrl, String userEntitiesEndpoint) { if (StringUtils.isEmpty(protocolVersion)) { - return String.format("%s%s?sessionId=%s", serviceUrl, USER_ENTITIES_ENDPOINT, sessionId); + return String.format("%s%s?sessionId=%s", serviceUrl, userEntitiesEndpoint, sessionId); } else { - return String.format("%s%s?v=%s&sessionId=%s", serviceUrl, USER_ENTITIES_ENDPOINT, - protocolVersion, sessionId); + return String.format("%s%s?v=%s&sessionId=%s", serviceUrl, userEntitiesEndpoint, + protocolVersion, sessionId); } } diff --git a/libai/src/main/java/ai/api/AIDataService.java b/libai/src/main/java/ai/api/AIDataService.java index 0465460..69aeb76 100644 --- a/libai/src/main/java/ai/api/AIDataService.java +++ b/libai/src/main/java/ai/api/AIDataService.java @@ -34,12 +34,7 @@ import java.io.InputStream; import java.lang.reflect.Type; import java.net.*; -import java.util.Calendar; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.TimeZone; +import java.util.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -181,7 +176,7 @@ public AIResponse request(final AIRequest request, final RequestExtras requestEx final String queryData = GSON.toJson(request); final String response = doTextRequest(config.getQuestionUrl(getSessionId(serviceContext)), - queryData, additionalHeaders); + queryData, additionalHeaders, "POST"); if (StringUtils.isEmpty(response)) { throw new AIServiceException( @@ -556,7 +551,7 @@ public AIResponse uploadUserEntities(final Collection userEntities) } /** - * Add a bunch of new entity to an agent entity list + * Add a bunch of new entity to an agent user entity list * * @param userEntities collection of a new entity data * @param serviceContext custom service context that should be used instead of the default context @@ -565,6 +560,69 @@ public AIResponse uploadUserEntities(final Collection userEntities) */ public AIResponse uploadUserEntities(final Collection userEntities, AIServiceContext serviceContext) throws AIServiceException { + return getEntitiesAiResponse(userEntities, config.getUserEntitiesEndpoint(getSessionId(serviceContext)), "POST"); + } + + + /** + * Add a bunch of new entity to an agent entity list + * + * @param entity collection of a new entity data + * @return response object from service. Never null + * @throws AIServiceException + */ + public AIResponse uploadEntity(final Entity entity) throws AIServiceException { + final ArrayList entities = new ArrayList<>(); + entities.add(entity); + return getEntitiesAiResponse(entities, config.getEntitiesEndpoint(getSessionId(UNDEFINED_SERVICE_CONTEXT)), "POST"); + } + + + /** + * Udate entries to existing entity + * + * @param entity new entity data + * @return response object from service. Never null + * @throws AIServiceException + */ + public AIResponse updateEntityData(final Entity entity) throws AIServiceException { + final ArrayList entities = new ArrayList<>(); + entities.add(entity); + return getEntitiesAiResponse(entities, config.getEntitiesEndpoint(getSessionId(UNDEFINED_SERVICE_CONTEXT)), "PUT"); + } + + + /** + * Add a bunch of new entity to an agent entity list + * + * @param entities collection of a new entity data + * @return response object from service. Never null + * @throws AIServiceException + */ + public AIResponse uploadEntities(final Collection entities) throws AIServiceException { + return getEntitiesAiResponse(entities, config.getEntitiesEndpoint(getSessionId(UNDEFINED_SERVICE_CONTEXT)), "POST"); + } + + + /** + * Add a bunch of new entity to an agent entity list + * + * @param entities collection of a new entity data + * @param serviceContext custom service context that should be used instead of the default context + * @return response object from service. Never null + * @throws AIServiceException + */ + public AIResponse uploadEntities(final Collection entities, + AIServiceContext serviceContext) throws AIServiceException { + return getEntitiesAiResponse(entities, config.getEntitiesEndpoint(getSessionId(serviceContext))); + } + + private AIResponse getEntitiesAiResponse(Collection userEntities, String endpoint) throws AIServiceException { + return getEntitiesAiResponse(userEntities,endpoint,"POST"); + } + + + private AIResponse getEntitiesAiResponse(Collection userEntities, String endpoint, String requestMethod) throws AIServiceException { if (userEntities == null || userEntities.size() == 0) { throw new AIServiceException("Empty entities list"); } @@ -572,7 +630,7 @@ public AIResponse uploadUserEntities(final Collection userEntities, final String requestData = GSON.toJson(userEntities); try { final String response = - doTextRequest(config.getUserEntitiesEndpoint(getSessionId(serviceContext)), requestData); + doTextRequest(endpoint, requestData, requestMethod); if (StringUtils.isEmpty(response)) { throw new AIServiceException( "Empty response from ai service. Please check configuration and Internet connection."); @@ -634,19 +692,34 @@ protected String doTextRequest(final String requestJson) */ protected String doTextRequest(final String endpoint, final String requestJson) throws MalformedURLException, AIServiceException { - return doTextRequest(endpoint, requestJson, null); + return doTextRequest(endpoint, requestJson, "POST"); } + /** + * @param endpoint Cannot be null + * @param requestJson Cannot be null + * @param requestMethod HTTP method to perform the request + * @return Response string + * @throws MalformedURLException + * @throws AIServiceException + */ + protected String doTextRequest(final String endpoint, final String requestJson, String requestMethod) + throws MalformedURLException, AIServiceException { + return doTextRequest(endpoint, requestJson, null, requestMethod); + } + + /** * @param endpoint Cannot be null * @param requestJson Cannot be null * @param additionalHeaders + * @param requestMethod * @return Response string * @throws MalformedURLException * @throws AIServiceException */ protected String doTextRequest(final String endpoint, final String requestJson, - final Map additionalHeaders) + final Map additionalHeaders, String requestMethod) throws MalformedURLException, AIServiceException { // TODO call doRequest method assert endpoint != null; @@ -667,7 +740,7 @@ protected String doTextRequest(final String endpoint, final String requestJson, connection = (HttpURLConnection) url.openConnection(); } - connection.setRequestMethod("POST"); + connection.setRequestMethod(requestMethod); connection.setDoOutput(true); connection.addRequestProperty("Authorization", "Bearer " + config.getApiKey()); connection.addRequestProperty("Content-Type", "application/json; charset=utf-8"); diff --git a/libai/src/test/java/ai/api/AIDataServiceTest.java b/libai/src/test/java/ai/api/AIDataServiceTest.java index a9f177a..8d387f5 100644 --- a/libai/src/test/java/ai/api/AIDataServiceTest.java +++ b/libai/src/test/java/ai/api/AIDataServiceTest.java @@ -26,7 +26,7 @@ public void testCustomContext() throws AIServiceException { @Override protected String doTextRequest(String endpoint, String requestJson, - Map additionalHeaders) throws MalformedURLException, AIServiceException { + Map additionalHeaders, String requestMethod) throws MalformedURLException, AIServiceException { endpointValue.set(endpoint); requestJsonValue.set(requestJson); return "{}"; diff --git a/libai/src/test/java/ai/api/test/ProtocolProdTest.java b/libai/src/test/java/ai/api/test/ProtocolProdTest.java index 0b0ce21..281cc9a 100644 --- a/libai/src/test/java/ai/api/test/ProtocolProdTest.java +++ b/libai/src/test/java/ai/api/test/ProtocolProdTest.java @@ -44,6 +44,11 @@ protected String getBrAccessToken(){ return ""; } + protected String getDevAccessToken(){ + + return "fdee465864a146b08260f4134d010a04"; + } + protected String getPtBrAccessToken(){ return "42db6ad6a51c47088318a8104833b66c"; } diff --git a/libai/src/test/java/ai/api/test/ProtocolTestBase.java b/libai/src/test/java/ai/api/test/ProtocolTestBase.java index 7fd2fb6..2d78380 100644 --- a/libai/src/test/java/ai/api/test/ProtocolTestBase.java +++ b/libai/src/test/java/ai/api/test/ProtocolTestBase.java @@ -57,6 +57,8 @@ public abstract class ProtocolTestBase { protected abstract String getJaAccessToken(); + protected abstract String getDevAccessToken(); + protected ProtocolTestBase() { } @@ -538,6 +540,24 @@ public void userEntitiesTest() throws AIServiceException{ } + @Test + public void entitiesTest() throws AIServiceException { + final AIConfiguration config = new AIConfiguration(getDevAccessToken(), + AIConfiguration.SupportedLanguages.English); + + updateConfig(config); + + final AIDataService aiDataService = new AIDataService(config); + + final Entity dwarfsEntity = createDwarfsEntity(); + + final AIResponse uploadResult = aiDataService.updateEntityData(dwarfsEntity); + assertNotNull(uploadResult); + assertFalse(uploadResult.isError()); + } + + + @Test(expected = AIServiceException.class) public void userEntitiesEmptyCollectionTest() throws AIServiceException { final AIDataService aiDataService = createDataService(); diff --git a/pom.xml b/pom.xml index f1f0b79..eea7845 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,9 @@ 4.0.0 ai.api libai-base + 1.4.8 + pom Api.ai SDK diff --git a/speech/gcp/pom.xml b/speech/gcp/pom.xml index 306427e..7d2d81b 100644 --- a/speech/gcp/pom.xml +++ b/speech/gcp/pom.xml @@ -6,7 +6,9 @@ ai.api libai-base + 1.4.8 + ../../pom.xml diff --git a/web/servlet/pom.xml b/web/servlet/pom.xml index 9dcd0ba..a1d4d22 100644 --- a/web/servlet/pom.xml +++ b/web/servlet/pom.xml @@ -4,7 +4,9 @@ ai.api libai-base + 1.4.8 + ../../pom.xml ai.api.libai.web