#' @import httr jsonlite
lds_auth <- function(api = Sys.getenv("lds_api_key"),
mme = Sys.getenv("lds_mme_code")) {
httr::add_headers(`x-mjf-key` = api, `x-mjf-mme-code` = mme)
}
BASE_URL <- "https://watest.leafdatazone.com/api/v1"
dropNulls <- getFromNamespace("dropNulls", 'shiny')
lds_call <- function(endpoint = c("areas", "batches", "combined_batches", "disposals",
"inventory_types", "inventories", "inventory_adjustments",
"inventory_transfers", "inventory_transfer_deliveries", "lab_results",
"mmes", "plants", "sales", "strains", "taxes", "users",
"drivers", "plants_by_area", "move_plants_to_inventory",
"move_inventory_to_plants", "conversions/create",
"plants/harvest_plants", "split_inventory", "disposals/dispose",
"inventory_transfers/api_in_transit", "inventory_transfer_deliveries/api_in_transit",
"inventory_transfers/api_receive"),
type = c("GET", "POST", "PUT", "DELETE"),
global_id = NULL, query = NULL, body = NULL) {
type <- match.arg(type)
endpoint <- match.arg(endpoint)
url <- switch(type,
GET = modify_url(
BASE_URL, path = paste0("api/v1/", endpoint),
query = list(
query
)),
POST = modify_url(
BASE_URL, path = paste0("api/v1/", endpoint)),
PUT = modify_url(
BASE_URL, path = paste0("api/v1/", endpoint, "/update")),
DELETE = modify_url(
BASE_URL, path = paste0("api/v1/", endpoint, "/", global_id))
)
resp <- switch(type,
GET = GET(url, lds_auth(), httr::accept_json(), user_agent("CannaData")),
POST = POST(url, lds_auth(), body = body, encode = "json", httr::accept_json(), user_agent("CannaData")),
PUT = POST(url, lds_auth(), body = body, encode = "json", httr::accept_json(), user_agent("CannaData")),
DELETE = DELETE(url, lds_auth(), httr::accept_json(), user_agent("CannaData"))
)
if (!(http_type(resp) %in% c("application/json", "text/json"))) {
print(http_status(resp)$message)
stop("Leaf Data Systems API did not return JSON.", call. = FALSE)
}
if (http_error(resp)) {
print(fromJSON(content(resp, "text", encoding = "UTF-8"), simplifyVector = FALSE))
stop(paste("Leaf Data Systems API errored:",
http_status(resp)$message, sep = "\n"), call. = FALSE)
}
fromJSON(content(resp, "text", encoding = "UTF-8"), simplifyVector = FALSE)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.