R/wrappers.etsy.R

Defines functions eGET

Documented in eGET

#' eGET
#' @title Etsy HTTP wrappers
#' @description Generic wrappers to the HTTP methods used in the etsy API
#'
#' @param methodName Name of the etsy API method to use
#' @param accessToken An Auth1.0 token with the required authorization scope for \code{methodName}
#' @param debugMode Logical flag, default value FALSE. When TRUE, the GET call is made verbose and
#' the value returned includes details on the content and the response
#' @param ... In \code{eGET}, named list of the query parameters to be passed on to the GET call.
#' When a etsy API method uri contains a parameter, this is also passed as a query parameter to \code{eGET}
#' In \code{ePUT} and \code{ePOST}, \code{...} is passed on to the PUT/POST httr methods, and thus added to \code{config()}
#'
#' @return eGET returns the response of \code{methodName}, parsed into a \code{data.frame}.
#' When \code{debugMode = TRUE}, the returned value is a list containing:
#' \itemize{
#' \item content. The parsed content of the response
#' \item contentDetails. List of attributes of the content, including count and pagination.
#' \item responseDetails. List of attributes of the response, including http_status, url, etc.}
#' @importFrom magrittr %>%
#' @importFrom assertthat assert_that
#' @import jsonlite httr
#' @export
#'
#' @seealso [etsy.access_token()]
#' @examples
#' allActiveListings <- eGET("findAllListingActive")
#' myShopActiveListings <- eGET("findAllShopListingsActive", shop_id = etsy$shop_id, limit = 100)
#' myReceipts <- eGET("findAllShopReceipts", token = etsy.access_token("transactions_r"), shop_id = etsy$shop_id)
#' ePUT("updateListing", accessToken = etsy.access_token("listings_w"), listing_id = 757810862, debugMode = TRUE, body = list(state = "inactive"))
#' ePOST("createListing", 
#'        body = list(quantity = 1, 
#'                    title = "test title", 
#'                    description = "test description", 
#'                    price = 1, 
#'                    who_made = "i_did", 
#'                    is_supply = "false", 
#'                    when_made = "made_to_order", 
#'                    shipping_template_id = "27897639858"), 
#'        accessToken = etsy.access_token("listings_w"))
eGET <- function(methodName, accessToken = NULL, debugMode = FALSE, ...) {
  properties <- getMethodProperties(methodName, "etsy")
  assert_that(properties$http_method == "GET")
  params <- list(...)
  
  response <- GET(url = buildURL("etsy", properties, params),
                  query = buildQuery("etsy", properties, params),
                  config(token = accessToken, verbose = debugMode)
  ) %>%
    content
  
  response$results %<>% lapply(toTibble) %>% do.call(rbind, .)

  invisible(response)
}


#' ePUT
#' 
#' @describeIn eGET Etsy PUT wrapper
#' @export
#' 
ePUT <- function(methodName, body, accessToken, debugMode = FALSE, ...) {
  properties <- getMethodProperties(methodName, "etsy")
  assert_that(properties$http_method == "PUT")
  params <- list(...)
  
  response <- PUT(url = paste0(buildURL("etsy", properties, params), urlEncodeBody(body)),
                  config(token = accessToken, verbose = debugMode),
                  ...
  )
  invisible(response)
}


#' ePOST
#' @describeIn eGET Etsy POST wrapper
#' @export
#' 
ePOST <- function(methodName, body, accessToken, debugMode = FALSE, ...) {
  properties <- getMethodProperties(methodName, "etsy")
  assert_that(properties$http_method == "POST")
  params <- list(...)
  
  response <- POST(url = buildURL("etsy", properties, params),
                   body = body,
                   config(token = accessToken, verbose = debugMode),
                   ...
  )
  invisible(response)
}
agpknitweardesign/festotuAPIs documentation built on Feb. 2, 2020, 12:20 p.m.