diff --git a/NAMESPACE b/NAMESPACE index 764f988..62cbf8f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,4 +4,5 @@ S3method(print,cognitive_endpoint) export(az_cognitive_service) export(call_cognitive_endpoint) export(cognitive_endpoint) +export(get_cognitive_token) import(AzureRMR) diff --git a/R/AzureCognitive.R b/R/AzureCognitive.R index ae392cf..67a22a8 100644 --- a/R/AzureCognitive.R +++ b/R/AzureCognitive.R @@ -44,6 +44,7 @@ globalVariables(c("self", "private")) #' - `CustomVision.Prediction`: Prediction endpoint for a custom vision service #' - `ContentModerator`: Content moderation (text and images) #' - `Text`: text analytics +#' - `TextTranslate`: text translation #' #' The possible tiers depend on the type of service created. Consult the Azure Cognitive Service documentation for more information. Usually there will be at least one free tier available. #' diff --git a/R/endpoint.R b/R/endpoint.R index 7412b8b..e265b2f 100644 --- a/R/endpoint.R +++ b/R/endpoint.R @@ -14,6 +14,7 @@ #' - `CustomVision.Prediction`: Prediction endpoint for a custom vision service #' - `ContentModerator`: Content moderation (text and images) #' - `Text`: text analytics +#' - `TextTranslate`: text translation #' #' @return #' An object inheriting from class `cognitive_endpoint`, that can be used to communicate with the REST endpoint. The object will have a subclass based on the type of the service. @@ -158,6 +159,7 @@ get_api_path <- function(type) customvision=, customvision_training=, customvision_prediction="customvision/v3.0", contentmoderator="contentmoderator/moderate/v1.0", text="text/analytics/v2.0", + texttranslate="translate", stop("Unknown cognitive service", call.=FALSE) ) } diff --git a/R/get_cognitive_token.R b/R/get_cognitive_token.R new file mode 100644 index 0000000..a843d14 --- /dev/null +++ b/R/get_cognitive_token.R @@ -0,0 +1,21 @@ +#' Obtain authentication token for a cognitive service +#' +#' @param key The subscription key for the service. +#' @param region The Azure region where the service is located. +#' @param token_host Optionally, the URL for the token endpoint. +#' @export +get_cognitive_token <- function(key, region, token_host=NULL) +{ + if(is.null(token_host)) + token_host <- sprintf("https://%s.api.cognitive.microsoft.com/sts/v1.0/issueToken", normalize_region(region)) + + hdrs <- httr::add_headers(`Ocp-Apim-Subscription-Key`=unname(key)) + res <- httr::POST(token_host, encode="form", hdrs) + rawToChar(process_cognitive_response(res, "stop")) +} + + +normalize_region <- function(region) +{ + tolower(gsub(" ", "", region)) +} diff --git a/man/cognitive_endpoint.Rd b/man/cognitive_endpoint.Rd index f6a2596..d8deba2 100644 --- a/man/cognitive_endpoint.Rd +++ b/man/cognitive_endpoint.Rd @@ -34,6 +34,7 @@ Currently, \code{cognitive_endpoint} recognises the following service types: \item \code{CustomVision.Prediction}: Prediction endpoint for a custom vision service \item \code{ContentModerator}: Content moderation (text and images) \item \code{Text}: text analytics +\item \code{TextTranslate}: text translation } } \seealso{ diff --git a/man/create_cognitive_service.Rd b/man/create_cognitive_service.Rd index cbab94b..6bf64f1 100644 --- a/man/create_cognitive_service.Rd +++ b/man/create_cognitive_service.Rd @@ -43,6 +43,7 @@ For \code{create_cognitive_service}, the type of service created can be one of t \item \code{CustomVision.Prediction}: Prediction endpoint for a custom vision service \item \code{ContentModerator}: Content moderation (text and images) \item \code{Text}: text analytics +\item \code{TextTranslate}: text translation } The possible tiers depend on the type of service created. Consult the Azure Cognitive Service documentation for more information. Usually there will be at least one free tier available. diff --git a/man/get_cognitive_token.Rd b/man/get_cognitive_token.Rd new file mode 100644 index 0000000..8e22628 --- /dev/null +++ b/man/get_cognitive_token.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_cognitive_token.R +\name{get_cognitive_token} +\alias{get_cognitive_token} +\title{Obtain authentication token for a cognitive service} +\usage{ +get_cognitive_token(key, region, token_host = NULL) +} +\arguments{ +\item{key}{The subscription key for the service.} + +\item{region}{The Azure region where the service is located.} + +\item{token_host}{Optionally, the URL for the token endpoint.} +} +\description{ +Obtain authentication token for a cognitive service +}