R/peloton_api.R

Defines functions print.peloton_api peloton_api

Documented in peloton_api

#' Makes a \code{GET} request against one of Peloton's API endpoints
#'
#'
#' Users need not invoke this method directly and may instead use one of the wrappers around specific endpoints that also vectorizes inputs and processes the data returned, such as \code{\link{get_my_info}},  \code{\link{get_performance_graphs}}, \code{\link{get_all_workouts}}, \code{\link{get_workouts_data}}
#'
#' @export
#' @param path API endpoint to query
#' @param print_path Show path/endpoint queried
#' @param ... Additional parameters passed onto methods
#' @examples
#' \dontrun{
#' peloton_auth()
#' peloton_api("api/me")
#' }
#'
peloton_api <- function(path, print_path = FALSE, ...) {
  path <- glue::glue("{path}")
  url <- httr::modify_url("https://api.onepeloton.com/", path = path, ...)

  if (isTRUE(print_path)) cat("api: \n", url)

  resp <- httr::GET(url = url)

  if (httr::http_type(resp) != "application/json") {
    stop("API did not return json", call. = FALSE)
  }

  parsed <- jsonlite::fromJSON(httr::content(resp, "text", encoding = "UTF-8"), simplifyVector = FALSE)

  if (httr::http_error(resp)) {
    msg <- glue::glue(
      "Peloton API request failed ({httr::status_code(resp)})
        {parsed$message}
        "
    )
    stop(
      msg,
      call. = FALSE
    )
  }

  structure(
    list(
      path = path,
      response = resp,
      content = parsed
    ),
    class = "peloton_api"
  )
}


print.peloton_api <- function(x, ...) {
  cat("<Peloton ", x$path, ">\n", sep = "")
  utils::str(x$content)
}
bweiher/pelotonR documentation built on Jan. 9, 2021, 1:36 a.m.