Skip to content

Instantly share code, notes, and snippets.

@bravecorvus
Last active July 3, 2018 20:14
Show Gist options
  • Save bravecorvus/369b5357bacaeeb11382bc9223c0191a to your computer and use it in GitHub Desktop.
Save bravecorvus/369b5357bacaeeb11382bc9223c0191a to your computer and use it in GitHub Desktop.
package example
import voiceit2.VoiceIt2
import scala.concurrent.Future
import scala.util.parsing.json.JSON
object Example {
def main(args : Array[String]): Unit = {
// Visit https://voiceit.io/settings and Log In in order to view your API 2.0 Key and Token
var vi = new VoiceIt2("<Insert Valid VoiceIt Key>", "<Insert Valid VoiceIt Token>")
vi.createUser((jsonResponse : String) => {
// The code in the next 7 lines will allow you to extract the value of "userId" from the returned JSON string "jsonResponse" and save it into the string variable "userId"
var parsedCreateUser = JSON.parseFull(jsonResponse)
var userId = ""
parsedCreateUser match {
case Some(m : Map[String, Any]) => m("userId") match {
case s : String => userId = s
case _ => sys.error("No userId returned")
}
}
// Print "userId"
println(userId)
// Note, if you need to maintain a synchronous ordering to your code, you will need to ensure the parts that happen after are enclosed within the classback function.
// Specifically, the following calls to vi.checkUserExists() and vi.deleteUser() will be run non-deterministically, which means that in the event that deleteUser() succeeds before checkUserExists(), checkUserExists() will fail to find that user since you deleted it.
// However, everything after the vi.createUser() call will only run after the function succeeds since it is enclosed in the function callback.
// They are commented out so we can explore other calls without having to create a new user.
/*
vi.checkUserExists(userId, (jsonResponse : String) => {
println("Checking if user exists: (Should be true)")
println(jsonResponse)
})
vi.deleteUser(userId, (jsonResponse : String) => {
println("Deleting user")
println(jsonResponse)
})
*/
// Example of creating a new group, and saving the result to "groupId" variable.
vi.createGroup("a new group", (jsonResponse : String) => {
var parsedCreateGroup = JSON.parseFull(jsonResponse)
var groupId = ""
parsedCreateGroup match {
case Some(m : Map[String, Any]) => m("groupId") match {
case s : String => groupId = s
case _ => sys.error("No groupId returned")
}
}
// More example of synchronizing code (i.e. #5 will always run after #4 returns a value, which will always run after #3 returns a value, which will always run before #2 returns a value, which will always run after #1 returns a value).
vi.addUserToGroup(userId, groupId, (jsonResponse : String) => { // #1
println("Adding user to group")
println(jsonResponse)
vi.createVideoEnrollment(userId, "en-US", "<Valid file path to unique enrollment video #1>", false, (jsonResponse : String) => { // #2
println("Creating video enrollment #1 for user")
println(jsonResponse)
vi.createVideoEnrollment(userId, "en-US", "<Valid file path to unique enrollment video #2>", false, (jsonResponse : String) => { // #3
println("Creating video enrollment #2 for user")
println(jsonResponse)
vi.createVideoEnrollment(userId, "en-US", "<Valid file path to unique enrollment video #1>", false, (jsonResponse : String) => { // #4
println("Creating video enrollment #3 for user")
println(jsonResponse)
vi.videoVerification(userId, "en-US", "<Valid file path to verification video>", false, (jsonResponse : String) => { // #5
println("Verifying video")
println(jsonResponse)
})
})
})
})
})
})
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment