Uploading Entity Type Values

How to script updating values of an Enum Entity Type

Entity Type values

Your buddy most likely has Entity Types, that you would have created using console. In the entity type, you would have added a list of values consisting of the identity of the value and synonyms. For some applications, this list of values may be changing frequently, and it is tedious and time consuming to update this using console. For that reason, Slang provides GET, POST and PUT REST APIs to modify the values in an Entity Type.

GET values for an Entity Type

Consider an example of a buddy for travel ticketing application that allows to book flight and train tickets. Assume that it has an Entity Type TravelMode. You can get all the values of TravelMode with GET api:

$ curl -X GET -u <api-key>:<api-secret> https://console.slanglabs.in/v1/applications/<buddy-id>/types/TravelMode/values\?env\=<env>

Where, <api-key> : your API key <api-secret> : currently the word "password" <buddy-id> : your buddy id <env> : the buddy environment you are manipulating, "stage" or "prod"

If the travel modes were air and train, this command will give an output:

[{"identity":"flight","synonyms":[]},{"identity":"train","synonyms":[]}]

POST to add values to an Entity Type

POST api appends the given values to an Entity Type.

Say, you want to support a new mode bus, and you want to add the value "bus" along with synonyms "road" to the existing values of mode, you can use POST on the same endpoint:

$ curl -X POST -u <api-key>:<api-secret> https://console.slanglabs.in/v1/applications/<buddy-id>/types/TravelMode/values\?env\=<env> -H "Content-Type: text/csv" -d 'bus,road'

If you do GET again, you will get following result:

[{"identity":"flight","synonyms":[]},{"identity":"train","synonyms":[]},{"identity":"bus","synonyms":["road"]}]

In real life, there will not be just one value to be added. In that case, a CSV file cat be used:

$ cat modes.csv
ship,boat,cruise
rocket,spaceship
$ curl -X POST -u <api-key>:<api-secret> https://console.slanglabs.in/v1/applications/<buddy-id>/types/TravelMode/values\?env\=<env> -H "Content-Type: text/csv" --data-binary @modes.csv

And GET will return the existing and new values:

[{"identity":"flight","synonyms":[]},{"identity":"train","synonyms":[]},{"identity":"bus","synonyms":["road"]},{"identity":"ship","synonyms":["boat","cruise"]},{"identity":"rocket","synonyms":["spaceship"]}]

Note that if an existing value with the same identity existed, POST will update synonyms of that value.

PUT to replace values of an Entity Type

In some situations, it may be desirable to replace all the values instead of appending some to existing values. PUT api serves that use case:

$ cat modes.csv
auto,auto rickshaw
motorcycle,scooter
car,motor
bus
train,rail
flight,air
ship,boat,cruise
rocket,spaceship
teleportation
time machine
$ curl -X PUT -u <api-key>:<api-secret> https://console.slanglabs.in/v1/applications/<buddy-id>/types/TravelMode/values\?env\=<env> -H "Content-Type: text/csv" --data-binary @modes.csv

And then the GET will return:

[{"identity":"auto","synonyms":["auto rickshaw"]},{"identity":"bus","synonyms":["road"]},{"identity":"car","synonyms":["motor"]},{"identity":"flight","synonyms":["air"]},{"identity":"motorcycle","synonyms":["scooter"]},{"identity":"rocket","synonyms":["spaceship"]},{"identity":"train","synonyms":["rail"]}]