R/callInvAPI.R

Defines functions callInvAPI

Documented in callInvAPI

#' Make a call to the Inventory API
#'
#' @param upc Either a single UPC or a vector of UPCs to return inventory for
#' @param store_num Either a single store number or a vector of store numbers to return inventory for. Note Online inventory is given by 'JAX'. If you don't supply a store number or search paramters the function will return all store inventory.
#' @param search_postal_code A string containing the postal code to search around
#' @param search_radius A string providing the radius to search around the postal code
#' @param search_radius_units A string specifying either miles or meters for the radius search
#' @param api_key The key provided by IT to access this API.
#' @param ... additional arguments passed to POST
#'
#' @return A dataframe containing the requested UPC and stores or stores within your search radius. If no results exists, the return will be an empty list.
#' @examples \dontrun{ callInvAPI(upc = sample_upc, store_num = sample_store, api_key = Sys.getenv('IBM_API_KEY')) }
#' @export
#'
callInvAPI <- function(upc,
                       store_num = NULL,
                       search_postal_code = NULL,
                       search_radius = NULL,
                       search_radius_units = 'miles',
                       api_key = NULL,
                       ...)
  {

  if (is.null(api_key)) stop("You should provide the api key provided by IT")
  if (is.null(upc)) stop("You must provide a product to get inventory for")

  if (is.null(store_num)) {
    if (is.null(search_postal_code)) {
      if (is.null(search_radius)) {
        message("No store was provided and no search parameters were passed, this will return company-wide inventory")
      }
    }
  }

  if (!is.null(store_num)) {
    if (!is.null(search_radius)) {
      stop("Searching by radius is only definied for postal codes, please rewrite the query")
    }
  }

  if (!is.character(upc)) {
    stop("upcs should be a character vector of length > 0")
  }

  if (!is.null(store_num)) {
    if (!is.character(store_num)) {
      stop("store_numbers should be a character vector of length > 0")
    }
  }

  if (!is.null(search_postal_code)) {
    if (!is.character(search_postal_code)) {
      search_postal_code <- as.character(search_postal_code)
    }
  }

  if (!is.null(search_radius)) {
    if (is.character(search_radius)) {
      search_radius <- as.numeric(search_radius)
    }
  }


  api_url <- 'https://api.us.apiconnect.ibmcloud.com/coachinc-api/qa/Product/GetInventory'

  par_store_num          <- nullToblank(store_num)
  par_search_postal_code <- nullToblank(search_postal_code)

  if (!is.null(search_radius)) {
    if (search_radius_units == 'miles') {
      search_radius <- search_radius * 1609L
      par_search_radius <- nullToblank(as.character(search_radius))
    } else if (search_radius_units == 'meters') {
      par_search_radius <- nullToblank(as.character(search_radius))
    } else {
      stop("Search radius units should be either miles or meters")
    }
  } else {
    par_search_radius <- nullToblank(as.character(search_radius))
  }

  params <- list(GetInventoryRequest = list(list(OutputStoreDetail = 'True',
                                                 ProductIDs        = I(upc),
                                                 StoreNumber       = I(par_store_num),
                                                 SearchRadius      = par_search_radius,
                                                 SearchPostalCode  = par_search_postal_code
  )))

  headers <- do.call(httr::"add_headers", list('X-IBM-Client-Id' = api_key))
  data    <- do.call(httr::"POST", list(url = api_url, headers, body = params, encode = 'json', ...))

  resp <- jsonlite::fromJSON(httr::content(data, 'text', encoding = 'UTF-8'))

  if (data$status_code != 200) {
    stop(paste0('Something went wrong: \n',
                '    Status Code: ', resp$httpCode, '\n',
                '    Message: ',     resp$httpMessage, '\n',
                '    Description: ', resp$moreInformation))
  } else if (length(resp$Inventory) == 0) {
    return('No results were returned for your query')
  }

  if (!is.null(search_radius) & search_radius_units == 'miles') {
    out_data <- resp$Inventory
    out_data$Distance <- out_data$Distance / 1609L
    return(out_data)
  } else {
    out_data <- resp$Inventory
    return(out_data)
  }
}
dwernersexton/rPOS documentation built on May 21, 2019, 6:49 a.m.