R/get_layer_details.R

Defines functions get_layer_details

Documented in get_layer_details

#' Get Layer Details
#'
#' Get the details of a layer
#'
#' @param endpoint the endpoint of the layer (can be generated by either \code{feature_server_endpoint} or \code{map_server_endpoint})
#' @param my_token an access token acquired by \code{get_token}
#' @return a list of parsed json
#' @importFrom httr GET
#' @importFrom httr content
#' @importFrom httr status_code
#' @importFrom RcppSimdJson fparse
#' @importFrom httr oauth_callback
get_layer_details <-
  function(endpoint, my_token = NULL) {
    # Parse the access token, returning NULL if it is null
    token <- parse_access_token(my_token)
    # Build the parameters - json & token
    query_string <- query_string(my_token = my_token)
    query_url <- paste0(endpoint, query_string)
    response <- httr::GET(query_url, httr::add_headers(referer = httr::oauth_callback()))

    # Fail if api Error
    # This does not fail properly if the layer is not found
    # because api still returns a 200 code
    stopifnot(httr::status_code(response) == 200)

    # Convert the json to a list
    parsed_content <- parse_rjson(response)

    # This code is added to catch an error where the api doesn't return a failed code but
    # should still error (where the layer wasn't found)
    #Add all clause to avoid R warning message
    ## Turn this into a function at some point
    if (all(names(parsed_content) == "error")) {
      stop(paste0("\n", paste0(paste0("- ",
                                      names(parsed_content$error), ": ", parsed_content$error
      ), collapse = "\n")))
    }
    return(parsed_content)
  }
MatthewJWhittle/getarc documentation built on April 22, 2023, 12:16 p.m.