R API call exampleΒΆ

Todo

update from S2 to G3

The code below allows you to call any function from the S1 API. It has 2 dependecies: the httr package for calling REST APIs and jsonlite for JSON manipulation.

# Example Geneea API call
#
###############################################################################

require(jsonlite)
require(httr)

GENEEA_API_URL <- "https://api.geneea.com/s2";
GENEEA_API_KEY <- <YOUR API KEY>;

textToJson <- function(text_vector, language = "") {
    if (language == "") { # langugage to be auto-detected
        return(toJSON(unbox(data.frame(text = text_vector))))
    } else {
        return(toJSON(unbox(data.frame(text = text_vector, lang = language))))
    }
}

geneeaApiCall <- function(api_function, text, language = "") {

    post.response <- POST(
            body = textToJson(text, language),
            url = paste(GENEEA_API_URL, api_function, sep ="/"),
            content_type("application/json;charset=utf-8"),
            add_headers(Authorization = paste("user_key", GENEEA_API_KEY))
    )

    parsed.value <- content(post.response, "parsed", "application/json")
    return(parsed.value)
}

result = geneeaApiCall("sentiment", "great text!", "en")
# The result will look like:
#
# $text
# [1] "great text!"
#
# $language
# [1] "en"
#
# $sentiment
# [1] 1
#
# $label
# [1] "positive"