From 63926652001d1f2a81e1a3e53edc077dcfb23c63 Mon Sep 17 00:00:00 2001 From: sam Date: Fri, 24 Feb 2017 14:00:54 +0100 Subject: [PATCH 1/6] Refactor endpoint format --- .../src/main/java/ai/api/AIConfiguration.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/libai/src/main/java/ai/api/AIConfiguration.java b/libai/src/main/java/ai/api/AIConfiguration.java index 1c24be9..079d10d 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,19 @@ 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); + } + + 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); } } From cb3c61e507edfabc24ca6062308819ef828f58ad Mon Sep 17 00:00:00 2001 From: sam Date: Fri, 24 Feb 2017 14:23:02 +0100 Subject: [PATCH 2/6] failing auth stage --- .../src/main/java/ai/api/AIConfiguration.java | 4 ++ libai/src/main/java/ai/api/AIDataService.java | 54 ++++++++++++++++--- .../java/ai/api/test/ProtocolTestBase.java | 19 +++++++ 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/libai/src/main/java/ai/api/AIConfiguration.java b/libai/src/main/java/ai/api/AIConfiguration.java index 079d10d..1b60193 100644 --- a/libai/src/main/java/ai/api/AIConfiguration.java +++ b/libai/src/main/java/ai/api/AIConfiguration.java @@ -212,6 +212,10 @@ 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, userEntitiesEndpoint, sessionId); diff --git a/libai/src/main/java/ai/api/AIDataService.java b/libai/src/main/java/ai/api/AIDataService.java index 75387b8..3286655 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; @@ -555,7 +550,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 @@ -564,6 +559,49 @@ public AIResponse uploadUserEntities(final Collection userEntities) */ public AIResponse uploadUserEntities(final Collection userEntities, AIServiceContext serviceContext) throws AIServiceException { + return getEntitiesAiResponse(userEntities, config.getUserEntitiesEndpoint(getSessionId(serviceContext))); + } + + + /** + * 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))); + } + + /** + * 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))); + } + + + /** + * 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 { if (userEntities == null || userEntities.size() == 0) { throw new AIServiceException("Empty entities list"); } @@ -571,7 +609,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); if (StringUtils.isEmpty(response)) { throw new AIServiceException( "Empty response from ai service. Please check configuration and Internet connection."); diff --git a/libai/src/test/java/ai/api/test/ProtocolTestBase.java b/libai/src/test/java/ai/api/test/ProtocolTestBase.java index 4acfd7e..d026d6c 100644 --- a/libai/src/test/java/ai/api/test/ProtocolTestBase.java +++ b/libai/src/test/java/ai/api/test/ProtocolTestBase.java @@ -539,6 +539,25 @@ public void userEntitiesTest() throws AIServiceException{ } + @Test + public void entitiesTest() throws AIServiceException { + final AIConfiguration config = new AIConfiguration(getAccessToken(), + AIConfiguration.SupportedLanguages.English); + + updateConfig(config); + + final AIDataService aiDataService = new AIDataService(config); + final AIDataService secondDataService = new AIDataService(config, aiDataService.getContext()); + + final Entity dwarfsEntity = createDwarfsEntity(); + + final AIResponse uploadResult = aiDataService.uploadEntity(dwarfsEntity); + assertNotNull(uploadResult); + assertFalse(uploadResult.isError()); + } + + + @Test(expected = AIServiceException.class) public void userEntitiesEmptyCollectionTest() throws AIServiceException { final AIDataService aiDataService = createDataService(); From 33c7f153006f12b59d7bcf575eee169b620a50b9 Mon Sep 17 00:00:00 2001 From: sam Date: Fri, 24 Feb 2017 14:25:08 +0100 Subject: [PATCH 3/6] version 1.5.0 --- libai/pom.xml | 2 +- pom.xml | 2 +- speech/gcp/pom.xml | 2 +- web/servlet/pom.xml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libai/pom.xml b/libai/pom.xml index 3b72e00..4f68435 100644 --- a/libai/pom.xml +++ b/libai/pom.xml @@ -11,7 +11,7 @@ ai.api libai-base .. - 1.4.6 + 1.5.0 libai diff --git a/pom.xml b/pom.xml index 4d72469..a497265 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 ai.api libai-base - 1.4.6 + 1.5.0 pom Api.ai SDK diff --git a/speech/gcp/pom.xml b/speech/gcp/pom.xml index 980fd1b..ec4939a 100644 --- a/speech/gcp/pom.xml +++ b/speech/gcp/pom.xml @@ -6,7 +6,7 @@ ai.api libai-base - 1.4.6 + 1.5.0 ../../pom.xml diff --git a/web/servlet/pom.xml b/web/servlet/pom.xml index cd929ac..79948de 100644 --- a/web/servlet/pom.xml +++ b/web/servlet/pom.xml @@ -4,7 +4,7 @@ ai.api libai-base - 1.4.6 + 1.5.0 ../../pom.xml ai.api.libai.web From 114366bc673a92ee047e2396c9c0138c6d7b82da Mon Sep 17 00:00:00 2001 From: sam Date: Fri, 24 Feb 2017 17:19:46 +0100 Subject: [PATCH 4/6] my dev access token to a test agent... they should be moved to envVars! --- libai/src/test/java/ai/api/test/ProtocolProdTest.java | 5 +++++ libai/src/test/java/ai/api/test/ProtocolTestBase.java | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-) 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 d026d6c..85d2cbd 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() { } @@ -541,13 +543,12 @@ public void userEntitiesTest() throws AIServiceException{ @Test public void entitiesTest() throws AIServiceException { - final AIConfiguration config = new AIConfiguration(getAccessToken(), + final AIConfiguration config = new AIConfiguration(getDevAccessToken(), AIConfiguration.SupportedLanguages.English); updateConfig(config); final AIDataService aiDataService = new AIDataService(config); - final AIDataService secondDataService = new AIDataService(config, aiDataService.getContext()); final Entity dwarfsEntity = createDwarfsEntity(); From 9ee6f8afc651cb747b7356060660dec4258d3b4a Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 25 Feb 2017 10:16:29 +0100 Subject: [PATCH 5/6] Added method params with overridable defaults to support update of entities --- libai/src/main/java/ai/api/AIDataService.java | 51 ++++++++++++++++--- .../test/java/ai/api/AIDataServiceTest.java | 2 +- .../java/ai/api/test/ProtocolTestBase.java | 2 +- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/libai/src/main/java/ai/api/AIDataService.java b/libai/src/main/java/ai/api/AIDataService.java index 3286655..0d40b16 100644 --- a/libai/src/main/java/ai/api/AIDataService.java +++ b/libai/src/main/java/ai/api/AIDataService.java @@ -176,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( @@ -559,7 +559,7 @@ public AIResponse uploadUserEntities(final Collection userEntities) */ public AIResponse uploadUserEntities(final Collection userEntities, AIServiceContext serviceContext) throws AIServiceException { - return getEntitiesAiResponse(userEntities, config.getUserEntitiesEndpoint(getSessionId(serviceContext))); + return getEntitiesAiResponse(userEntities, config.getUserEntitiesEndpoint(getSessionId(serviceContext)), "POST"); } @@ -573,9 +573,24 @@ public AIResponse uploadUserEntities(final Collection userEntities, 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))); + return getEntitiesAiResponse(entities, config.getEntitiesEndpoint(getSessionId(UNDEFINED_SERVICE_CONTEXT)), "POST"); } + + /** + * Send new entries to existing entity + * + * @param entity collection of a new entity data + * @return response object from service. Never null + * @throws AIServiceException + */ + public AIResponse addEntityData(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 * @@ -584,7 +599,7 @@ public AIResponse uploadEntity(final Entity entity) throws AIServiceException { * @throws AIServiceException */ public AIResponse uploadEntities(final Collection entities) throws AIServiceException { - return getEntitiesAiResponse(entities, config.getEntitiesEndpoint(getSessionId(UNDEFINED_SERVICE_CONTEXT))); + return getEntitiesAiResponse(entities, config.getEntitiesEndpoint(getSessionId(UNDEFINED_SERVICE_CONTEXT)), "POST"); } @@ -602,6 +617,11 @@ public AIResponse uploadEntities(final Collection entities, } 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"); } @@ -609,7 +629,7 @@ private AIResponse getEntitiesAiResponse(Collection userEntities, String final String requestData = GSON.toJson(userEntities); try { final String response = - doTextRequest(endpoint, requestData); + doTextRequest(endpoint, requestData, requestMethod); if (StringUtils.isEmpty(response)) { throw new AIServiceException( "Empty response from ai service. Please check configuration and Internet connection."); @@ -671,19 +691,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; @@ -704,7 +739,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/ProtocolTestBase.java b/libai/src/test/java/ai/api/test/ProtocolTestBase.java index 85d2cbd..5d451af 100644 --- a/libai/src/test/java/ai/api/test/ProtocolTestBase.java +++ b/libai/src/test/java/ai/api/test/ProtocolTestBase.java @@ -552,7 +552,7 @@ public void entitiesTest() throws AIServiceException { final Entity dwarfsEntity = createDwarfsEntity(); - final AIResponse uploadResult = aiDataService.uploadEntity(dwarfsEntity); + final AIResponse uploadResult = aiDataService.addEntityData(dwarfsEntity); assertNotNull(uploadResult); assertFalse(uploadResult.isError()); } From fe318af2ad023092e746a7467766a81e6fda3f13 Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 1 Mar 2017 09:22:03 +0100 Subject: [PATCH 6/6] Renaming add to update --- libai/src/main/java/ai/api/AIDataService.java | 6 +++--- libai/src/test/java/ai/api/test/ProtocolTestBase.java | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libai/src/main/java/ai/api/AIDataService.java b/libai/src/main/java/ai/api/AIDataService.java index 0d40b16..9ccc85f 100644 --- a/libai/src/main/java/ai/api/AIDataService.java +++ b/libai/src/main/java/ai/api/AIDataService.java @@ -578,13 +578,13 @@ public AIResponse uploadEntity(final Entity entity) throws AIServiceException { /** - * Send new entries to existing entity + * Udate entries to existing entity * - * @param entity collection of a new entity data + * @param entity new entity data * @return response object from service. Never null * @throws AIServiceException */ - public AIResponse addEntityData(final Entity entity) 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"); diff --git a/libai/src/test/java/ai/api/test/ProtocolTestBase.java b/libai/src/test/java/ai/api/test/ProtocolTestBase.java index 5d451af..4fd236e 100644 --- a/libai/src/test/java/ai/api/test/ProtocolTestBase.java +++ b/libai/src/test/java/ai/api/test/ProtocolTestBase.java @@ -552,7 +552,7 @@ public void entitiesTest() throws AIServiceException { final Entity dwarfsEntity = createDwarfsEntity(); - final AIResponse uploadResult = aiDataService.addEntityData(dwarfsEntity); + final AIResponse uploadResult = aiDataService.updateEntityData(dwarfsEntity); assertNotNull(uploadResult); assertFalse(uploadResult.isError()); }