R/get_insight.R

Defines functions get_insight

Documented in get_insight

"dwapi-r
Copyright 2018 data.world, Inc.

Licensed under the Apache License, Version 2.0 (the \"License\");
you may not use this file except in compliance with the License.

You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an \"AS IS\" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.

This product includes software developed at data.world, Inc.
https://data.world"

#' Retrieve an insight.
#' @param project_owner username of the owner of the project.
#' @param project_id identifier of the project.
#' @param id identifier of the insight
#' @return an object of type \code{insight_summary_response}.
#' @examples
#' \dontrun{
#'   dwapi::get_insights(project_owner = "user", project_id = "project_id")
#' }
#' @export
get_insight <- function(project_owner, project_id, id) {
  url <- paste0(
    getOption("dwapi.api_url"),
    "/", "insights", "/",
    project_owner, "/",
    project_id, "/",
    id)
  auth <- paste0("Bearer ", auth_token())
  response <-
    httr::GET(
      url,
      httr::add_headers(`Content-Type` = "application/json",
                        Authorization = auth),
      httr::user_agent(user_agent())
    )
  if (response$status_code == 200) {
    structured_response <-
      rjson::fromJSON(httr::content(
        x = response,
        as = "text",
        encoding = "UTF-8"
      ))
    ret <- insight_summary_response(structured_response)
  } else {
    error_msg <-
      error_message(rjson::fromJSON(httr::content(
        x = response,
        as = "text",
        encoding = "UTF-8"
      )))
    stop(error_msg$message)
  }
  ret
}
datadotworld/dwapi-r documentation built on July 3, 2021, 3:40 a.m.