Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Added entity upload support #13

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libai/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
<groupId>ai.api</groupId>
<artifactId>libai-base</artifactId>
<relativePath>..</relativePath>

<version>1.4.8</version>

</parent>
<build>
<finalName>libai</finalName>
Expand Down
22 changes: 13 additions & 9 deletions libai/src/main/java/ai/api/AIConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
97 changes: 85 additions & 12 deletions libai/src/main/java/ai/api/AIDataService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -556,7 +551,7 @@ public AIResponse uploadUserEntities(final Collection<Entity> 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
Expand All @@ -565,14 +560,77 @@ public AIResponse uploadUserEntities(final Collection<Entity> userEntities)
*/
public AIResponse uploadUserEntities(final Collection<Entity> 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 <code>null</code>
* @throws AIServiceException
*/
public AIResponse uploadEntity(final Entity entity) throws AIServiceException {
final ArrayList<Entity> 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 <code>null</code>
* @throws AIServiceException
*/
public AIResponse updateEntityData(final Entity entity) throws AIServiceException {
final ArrayList<Entity> 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 <code>null</code>
* @throws AIServiceException
*/
public AIResponse uploadEntities(final Collection<Entity> 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 <code>null</code>
* @throws AIServiceException
*/
public AIResponse uploadEntities(final Collection<Entity> entities,
AIServiceContext serviceContext) throws AIServiceException {
return getEntitiesAiResponse(entities, config.getEntitiesEndpoint(getSessionId(serviceContext)));
}

private AIResponse getEntitiesAiResponse(Collection<Entity> userEntities, String endpoint) throws AIServiceException {
return getEntitiesAiResponse(userEntities,endpoint,"POST");
}


private AIResponse getEntitiesAiResponse(Collection<Entity> userEntities, String endpoint, String requestMethod) throws AIServiceException {
if (userEntities == null || userEntities.size() == 0) {
throw new AIServiceException("Empty entities list");
}

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.");
Expand Down Expand Up @@ -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 <code>null</code>
* @param requestJson Cannot be <code>null</code>
* @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 <code>null</code>
* @param requestJson Cannot be <code>null</code>
* @param additionalHeaders
* @param requestMethod
* @return Response string
* @throws MalformedURLException
* @throws AIServiceException
*/
protected String doTextRequest(final String endpoint, final String requestJson,
final Map<String, String> additionalHeaders)
final Map<String, String> additionalHeaders, String requestMethod)
throws MalformedURLException, AIServiceException {
// TODO call doRequest method
assert endpoint != null;
Expand All @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion libai/src/test/java/ai/api/AIDataServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void testCustomContext() throws AIServiceException {

@Override
protected String doTextRequest(String endpoint, String requestJson,
Map<String, String> additionalHeaders) throws MalformedURLException, AIServiceException {
Map<String, String> additionalHeaders, String requestMethod) throws MalformedURLException, AIServiceException {
endpointValue.set(endpoint);
requestJsonValue.set(requestJson);
return "{}";
Expand Down
5 changes: 5 additions & 0 deletions libai/src/test/java/ai/api/test/ProtocolProdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ protected String getBrAccessToken(){
return "";
}

protected String getDevAccessToken(){

return "fdee465864a146b08260f4134d010a04";
}

protected String getPtBrAccessToken(){
return "42db6ad6a51c47088318a8104833b66c";
}
Expand Down
20 changes: 20 additions & 0 deletions libai/src/test/java/ai/api/test/ProtocolTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public abstract class ProtocolTestBase {

protected abstract String getJaAccessToken();

protected abstract String getDevAccessToken();

protected ProtocolTestBase() {
}

Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
<modelVersion>4.0.0</modelVersion>
<groupId>ai.api</groupId>
<artifactId>libai-base</artifactId>

<version>1.4.8</version>

<packaging>pom</packaging>

<name>Api.ai SDK</name>
Expand Down
2 changes: 2 additions & 0 deletions speech/gcp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
<parent>
<groupId>ai.api</groupId>
<artifactId>libai-base</artifactId>

<version>1.4.8</version>

<relativePath>../../pom.xml</relativePath>
</parent>
<properties>
Expand Down
2 changes: 2 additions & 0 deletions web/servlet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
<parent>
<groupId>ai.api</groupId>
<artifactId>libai-base</artifactId>

<version>1.4.8</version>

<relativePath>../../pom.xml</relativePath>
</parent>
<groupId>ai.api.libai.web</groupId>
Expand Down