R/flea_api.R

Defines functions print.fleaflicker_api fleaflicker_getendpoint

Documented in fleaflicker_getendpoint

#### FLEAFLICKER API ####

#' GET any Fleaflicker endpoint
#'
#' The endpoint names and HTTP parameters (i.e. argument names) are CASE SENSITIVE and should be passed in exactly as displayed on the Fleaflicker API reference page.
#'
#' Check out the vignette for more details and example usage.
#'
#' @param endpoint a string defining which endpoint to return from the API
#' @param ... Arguments which will be passed as "argumentname = argument" in an HTTP query parameter
#'
#' @seealso <https://www.fleaflicker.com/api-docs/index.html>
#' @seealso `vignette("fleaflicker_getendpoint")`
#'
#' @return A list object containing the query, response, and parsed content.
#' @export

fleaflicker_getendpoint <- function(endpoint, ...) {

  # PREP URL

  url_query <- httr::modify_url(
    url = glue::glue("https://www.fleaflicker.com/api/{endpoint}"),
    query = list(
      ...
    )
  )

  ## GET FFSCRAPR ENV

  fn_get <- get("get.sleeper", envir = .ffscrapr_env, inherits = TRUE)

  user_agent <- get("user_agent", envir = .ffscrapr_env, inherits = TRUE)

  ## DO QUERY

  response <- fn_get(url_query, user_agent)

  ## CHECK QUERY
  # nocov start

  if (httr::http_error(response) && httr::status_code(response) == 429) {
    stop(glue::glue("You've hit a rate limit wall! Please adjust the
                    built-in rate_limit arguments in fleaflicker_connect()!"), call. = FALSE)
  }

  if (httr::http_error(response)) {
    stop(glue::glue("Fleaflicker API request failed with <{httr::http_status(response)$message}> \n
                    while calling <{url_query}>"), call. = FALSE)
  }

  if (httr::http_type(response) != "application/json") {
    warning(glue::glue("Fleaflicker API did not return json while calling {url_query}"),
      call. = FALSE
    )
  }

  if (httr::http_type(response) == "application/json") {
    parsed <- jsonlite::parse_json(httr::content(x = response, as = "text"))
  }

  if (!is.null(parsed$error)) {
    warning(glue::glue("Fleaflicker says: {parsed$error[[1]]}"), call. = FALSE)
  }

  # nocov end

  ## RETURN S3

  structure(
    list(
      content = parsed,
      query = url_query,
      response = response
    ),
    class = "fleaflicker_api"
  )
}

## PRINT METHOD SLEEPER_API OBJ ##
#' @noRd
#' @export
print.fleaflicker_api <- function(x, ...) {

  # nocov start

  cat("<Fleaflicker - GET - ", httr::http_status(x$response)$message, ">\n", sep = "")
  cat("QUERY: <", x$query, ">\n", sep = "")
  str(x$content, max.level = 1)

  invisible(x)

  # nocov end
}

Try the ffscrapr package in your browser

Any scripts or data that you put into this service are public.

ffscrapr documentation built on Feb. 16, 2023, 10:55 p.m.