/R/tuber.R

https://github.com/soodoku/tuber · R · 129 lines · 53 code · 26 blank · 50 comment · 4 complexity · 2fdd88c644aae2fa7503e22196bf2f73 MD5 · raw file

  1. #' @title \pkg{tuber} provides access to the YouTube API V3.
  2. #'
  3. #' @description \pkg{tuber} provides access to the YouTube API V3 via
  4. #' RESTful calls.
  5. #'
  6. #' @name tuber
  7. #' @importFrom httr GET POST DELETE authenticate config stop_for_status
  8. #' @importFrom httr upload_file content oauth_endpoints oauth_app oauth2.0_token
  9. #' @importFrom utils read.table
  10. #' @importFrom plyr ldply
  11. #' @importFrom dplyr bind_rows select pull filter mutate
  12. #' @importFrom tibble enframe
  13. #' @importFrom tidyselect everything all_of
  14. #' @importFrom tidyr pivot_wider unnest unnest_longer
  15. #' @importFrom purrr map_df map_dbl
  16. #' @docType package
  17. NULL
  18. #' Check if authentication token is in options
  19. #' @return A Token2.0 class
  20. #' @export
  21. yt_token <- function() {
  22. getOption("google_token")
  23. }
  24. #' @export
  25. #' @rdname yt_token
  26. yt_authorized <- function() {
  27. !is.null(yt_token())
  28. }
  29. #' @rdname yt_token
  30. yt_check_token <- function() {
  31. if (!yt_authorized()) {
  32. stop("Please get a token using yt_oauth().\n")
  33. }
  34. }
  35. #'
  36. #' GET
  37. #'
  38. #' @param path path to specific API request URL
  39. #' @param query query list
  40. #' @param \dots Additional arguments passed to \code{\link[httr]{GET}}.
  41. #' @return list
  42. tuber_GET <- function(path, query, ...) {
  43. yt_check_token()
  44. req <- GET("https://www.googleapis.com", path = paste0("youtube/v3/", path),
  45. query = query, config(token = getOption("google_token")), ...)
  46. tuber_check(req)
  47. res <- content(req)
  48. res
  49. }
  50. #'
  51. #' POST
  52. #'
  53. #' @param path path to specific API request URL
  54. #' @param query query list
  55. #' @param body passing image through body
  56. #' @param \dots Additional arguments passed to \code{\link[httr]{GET}}.
  57. #'
  58. #' @return list
  59. tuber_POST <- function(path, query, body = "", ...) {
  60. yt_check_token()
  61. req <- POST("https://www.googleapis.com", path = paste0("youtube/v3/", path),
  62. body = body, query = query,
  63. config(token = getOption("google_token")), ...)
  64. tuber_check(req)
  65. res <- content(req)
  66. res
  67. }
  68. #'
  69. #' DELETE
  70. #'
  71. #' @param path path to specific API request URL
  72. #' @param query query list
  73. #' @param \dots Additional arguments passed to \code{\link[httr]{GET}}.
  74. #' @return list
  75. tuber_DELETE <- function(path, query, ...) {
  76. yt_check_token()
  77. req <- DELETE("https://www.googleapis.com", path = paste0("youtube/v3/", path),
  78. query = query, config(token = getOption("google_token")), ...)
  79. tuber_check(req)
  80. res <- content(req)
  81. res
  82. }
  83. #'
  84. #' Request Response Verification
  85. #'
  86. #' @param req request
  87. #' @return in case of failure, a message
  88. tuber_check <- function(req) {
  89. if (req$status_code < 400) return(invisible(NULL))
  90. orig_out <- httr::content(req, as = "text")
  91. out <- try({
  92. jsonlite::fromJSON(
  93. orig_out,
  94. flatten = TRUE)
  95. }, silent = TRUE)
  96. if (inherits(out, "try-error")) {
  97. msg <- orig_out
  98. } else {
  99. msg <- out$error$message
  100. }
  101. msg <- paste0(msg, "\n")
  102. stop("HTTP failure: ", req$status_code, "\n", msg, call. = FALSE)
  103. }