Skip to content

Commit

Permalink
Option to send collection of user entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Danil Skachkov committed Aug 3, 2015
1 parent 63975b0 commit b4bf61e
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 79 deletions.
14 changes: 7 additions & 7 deletions ailib/src/main/java/ai/api/AIConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public void setProtocolVersion(final String protocolVersion) {
/**
* Set API service url. Used primarily for test requests.
*/
public void setServiceUrl(String serviceUrl) {
public void setServiceUrl(final String serviceUrl) {
this.serviceUrl = serviceUrl;
}

Expand All @@ -237,35 +237,35 @@ public String getQuestionUrl() {
}
}

public String getUserEntitiesEndpoint() {
String getUserEntitiesEndpoint(final String sessionId) {
if (!TextUtils.isEmpty(protocolVersion)) {
return String.format("%s%s?v=%s", serviceUrl, USER_ENTITIES_ENDPOINT, protocolVersion);
return String.format("%s%s?v=%s&sessionId=%s", serviceUrl, USER_ENTITIES_ENDPOINT, protocolVersion, sessionId);
} else {
return String.format("%s%s", serviceUrl, USER_ENTITIES_ENDPOINT);
return String.format("%s%s?sessionId=%s", serviceUrl, USER_ENTITIES_ENDPOINT, sessionId);
}
}

public AssetFileDescriptor getRecognizerStartSound() {
return recognizerStartSound;
}

public void setRecognizerStartSound(AssetFileDescriptor recognizerStartSound) {
public void setRecognizerStartSound(final AssetFileDescriptor recognizerStartSound) {
this.recognizerStartSound = recognizerStartSound;
}

public AssetFileDescriptor getRecognizerStopSound() {
return recognizerStopSound;
}

public void setRecognizerStopSound(AssetFileDescriptor recognizerStopSound) {
public void setRecognizerStopSound(final AssetFileDescriptor recognizerStopSound) {
this.recognizerStopSound = recognizerStopSound;
}

public AssetFileDescriptor getRecognizerCancelSound() {
return recognizerCancelSound;
}

public void setRecognizerCancelSound(AssetFileDescriptor recognizerCancelSound) {
public void setRecognizerCancelSound(final AssetFileDescriptor recognizerCancelSound) {
this.recognizerCancelSound = recognizerCancelSound;
}

Expand Down
14 changes: 11 additions & 3 deletions ailib/src/main/java/ai/api/AIDataService.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -239,11 +241,17 @@ public boolean resetContexts() {
}

public AIResponse uploadUserEntity(final Entity userEntity) throws AIServiceException {
final UserEntityWrapper wrapper = new UserEntityWrapper(userEntity, sessionId);
final String requestData = gson.toJson(wrapper);
return uploadUserEntities(Collections.singleton(userEntity));
}

public AIResponse uploadUserEntities(final Collection<Entity> userEntities) 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(), requestData);
final String response = doTextRequest(config.getUserEntitiesEndpoint(sessionId), requestData);
if (TextUtils.isEmpty(response)) {
throw new AIServiceException("Empty response from ai service. Please check configuration and Internet connection.");
}
Expand Down
15 changes: 13 additions & 2 deletions ailib/src/main/java/ai/api/AIService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import android.content.Context;

import java.util.Collection;
import java.util.List;

import ai.api.model.AIContext;
Expand Down Expand Up @@ -173,12 +174,22 @@ public boolean resetContexts() {
}

/**
* Upload user entities for using while session
* @param userEntity
* Upload user entity for using while session
* @param userEntity entity to upload
* @return uploading result
* @throws AIServiceException
*/
public AIResponse uploadUserEntity(final Entity userEntity) throws AIServiceException {
return aiDataService.uploadUserEntity(userEntity);
}

/**
* Upload user entities for using while session
* @param userEntities collection of user entities
* @return uploading result
* @throws AIServiceException if request to the API.AI service failed
*/
public AIResponse uploadUserEntities(final Collection<Entity> userEntities) throws AIServiceException {
return aiDataService.uploadUserEntities(userEntities);
}
}
49 changes: 0 additions & 49 deletions ailib/src/main/java/ai/api/UserEntityWrapper.java

This file was deleted.

15 changes: 15 additions & 0 deletions ailib/src/main/java/ai/api/model/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
import java.util.ArrayList;
import java.util.List;

/**
* User Entity what can be uploaded to the API.AI service and override (or extend) entities, described in agent.
* User Entities works only in specified session, and do not affect other sessions.
*/
public class Entity implements Serializable {

@SerializedName("name")
Expand All @@ -35,6 +39,9 @@ public class Entity implements Serializable {
@SerializedName("entries")
private List<EntityEntry> entries;

@SerializedName("extend")
private Boolean extend;

public Entity() {
}

Expand Down Expand Up @@ -64,4 +71,12 @@ public void addEntry(final EntityEntry entry) {
}
entries.add(entry);
}

public Boolean getExtend() {
return extend;
}

public void setExtend(final boolean extend) {
this.extend = extend;
}
}
Loading

0 comments on commit b4bf61e

Please sign in to comment.