#' Download data from Systembolaget API
#' @description \code{rget} is used to download data from Systembolaget API
#' @param API Specifies which data set to download (all_products only implemented in this version)
#' @return \code{rget} return a data frame of classes "rbolaget" and "data.frame".
#' \itemize{
#' \item{\code{ProductNameBold}}{ The main product name.}
#' \item{\code{ProductNumber}}{ The super-key of the data.}
#' \item{\code{ProductNumberShort}}{ The product number displayed in customer system of Systembolaget.}
#' \item{\code{Category}}{ The main division of products.}
#' \item{\code{SubCategory}}{ Sub-divisions of each category.}
#' \item{\code{Type}}{ The type of product within the sub-category.}
#' \item{\code{Style}}{ The style of the product type.}
#' \item{\code{Price}}{ The price in SEK.}
#' \item{\code{AlcoholPercentage}}{ The alcohol percentage of the product.}
#' \item{\code{Volume}}{ The volume in milliliter (ml) of the product.}
#' \item{\code{BottleTextShort}}{ The container of the product.}
#' \item{\code{RecycleFee}}{ The recycling fee in SEK.}
#' \item{\code{Country}}{ The country of which the product is produced in.}
#' \item{\code{ProducerName}}{ The name of the producer.}
#' \item{\code{IsKosher}}{ \code{TRUE} if the product is classed "Kosher", \code{FALSE} otherwise.}
#' \item{\code{IsOrganic}}{ \code{TRUE} if the product is classed "Organic", \code{FALSE} otherwise.}
#' \item{\code{IsEthical}}{ \code{TRUE} if the product is classed "Ethical", \code{FALSE} otherwise.}
#' \item{\code{SellStartDate}}{ The date Systembolaget started selling the product.}
#' \item{\code{Taste}}{ A description of the tasting notes in the product.}
#' \item{\code{Usage}}{ Serving suggestions of the product.}
#' \item{\code{APK}}{ Calculated as (AlcoholPercentage * Volume) / Price}.}
#' @examples
#' rget()
#' @export
#' @import dplyr
rget <- function(API = "all_products"){
# så vi kan göra för butiker
if (API == "all_products") {
path <- "/product/v1/product/"
}
# hämta för alla produkter
url <- httr::modify_url("https://api-extern.systembolaget.se", path = path)
key <- "8894b8b4924b416abc338880b8b0ff8f"
resp <- httr::GET(url, httr::add_headers("Ocp-Apim-Subscription-Key" = key))
if ((httr::http_type(resp) == "application/json") & resp$status_code == 200) {
data <- jsonlite::fromJSON(httr::content(resp, "text"), simplifyVector = TRUE)
data <- data %>% select("ProductNameBold", "ProductNumber", "ProductNumberShort", "Category",
"SubCategory", "Type", "Style", "Price", "AlcoholPercentage", "Volume",
"BottleTextShort", "RecycleFee", "Country", "ProducerName", "IsKosher",
"IsOrganic", "IsEthical", "SellStartDate", "Taste", "Usage") %>%
mutate("APK" = ((data$AlcoholPercentage/100) * data$Volume) / data$Price,
"SellStartDate" = lubridate::as_date(data$SellStartDate))
class(data) <- c("rbolaget", "data.frame")
return(data)
} else { # Om felmeddelande dvs inte 200
warning(resp)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.