Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetching Calendly Events #9

Merged
merged 7 commits into from
Nov 16, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 106 additions & 13 deletions R/calendly.R
Original file line number Diff line number Diff line change
@@ -1,37 +1,130 @@
# Calendly data extraction handlers

#' Get Calendly API user
#' Handle Calendly GET requests
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now added a main function to handle GET requests. It is called by these other Calendly GET functions.

#' @description This is a function to get the Calendly API user info
#' @param api_key You can provide the API key directly or this function will attempt to grab an API key that was stored using the `authorize("calendly")` function
#' @param token You can provide the API key directly or this function will attempt to grab an API key that was stored using the `authorize("calendly")` function
#' @param user The user param for Calendly. Usually looks like "https://api.calendly.com/users/c208a750-9214-4c62-9ee6-a1a9507c7b43"
#' @param count For paginated GETs, you can specify how many things you'd like returned
#' @param page_token For a paginated GET, what page are we on?
#' @return Calendly REST API response as a list
#' @importFrom utils menu installed.packages
#' @importFrom httr oauth_app oauth_endpoints oauth2.0_token
#' @export
#' @examples \dontrun{
#'
#' authorize("calendly")
#' get_calendly_user()
#' token <- get_token(app_name = "calendly")
#'
#' result_list <- calendly_get(
#' url = "https://api.calendly.com/users/me",
#' token = token
#' )
#' }
get_calendly_user <- function(api_key) {
# Get auth token
token <- get_token(app_name = "calendly")

# Declare URL
url <- "https://api.calendly.com/users/me"

# Github api get
#'
# Get Calendly stuffs
calendly_get <- function(url, token, user = NULL, count = NULL, page_token = NULL) {
result <- httr::GET(
url,
query = list(
user = user,
count = count,
page = page_token
),
httr::add_headers(Authorization = paste0("Bearer ", token)),
httr::accept_json()
)

# Stop if error
if (httr::status_code(result) != 200) {
httr::stop_for_status(result)
}

# Process and return results
result_content <- httr::content(result, "text")
result_list <- jsonlite::fromJSON(result_content)
return(jsonlite::fromJSON(result_content))
}

#' Get Calendly API user
#' @description This is a function to get the Calendly API user info
#' @param api_key You can provide the API key directly or this function will attempt to grab an API key that was stored using the `authorize("calendly")` function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confused with the argument api_key as we are technically grabbing the personal access token. What about using token as an argument instead?

Suggested change
#' @param api_key You can provide the API key directly or this function will attempt to grab an API key that was stored using the `authorize("calendly")` function
#' @param token You can provide the token directly or this function will attempt to grab the token that was stored using the `authorize("calendly")` function

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have been inconsistent in this. At some times it's an api key and sometimes it's a token. I've been realizing this as I've been writing but I've not yet fixed it. Because you've noticed I'll make sure to fix it now lol

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made it more consistent that we always refer to it as a "token" instead of an "api_key"

#' @return Calendly REST API response as a list
#' @importFrom utils menu installed.packages
#' @importFrom httr oauth_app oauth_endpoints oauth2.0_token
#' @export
#' @examples \dontrun{
#'
#' authorize("calendly")
#' get_calendly_user()
#' }
get_calendly_user <- function(api_key) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
get_calendly_user <- function(api_key) {
get_calendly_user <- function(api_key = NULL) {

The function needs a default of NULL for the example on line 56 to work.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added NULLs 👍


if (is.null(api_key)) {
# Get auth token
token <- get_token(app_name = "calendly")
} else {
token <- api_key
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (is.null(api_key)) {
# Get auth token
token <- get_token(app_name = "calendly")
} else {
token <- api_key
}
if (is.null(api_key)) {
# Get auth token
token <- get_token(app_name = "calendly")
message("Using user-supplied token stored using authorize(\"calendly\")")
} else {
token <- api_key
}

To improve the API design, I suggest adding a message telling the user that we will be using the user-supplied token from authorize("calendly"). Kudos to Hadley's R Packages Workshop.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent idea. Yes this whole package needs a lot more error handling and messaging improvements.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added messages to warn users 👍

# Declare URL
result_list <- calendly_get(
url = "https://api.calendly.com/users/me",
token = token
)

return(result_list)
}

#' Get Calendly Event Lists
#' @description This is a function to get the Calendly API user info
#' @param user You can provide the API key directly or this function will attempt to grab an API key that was stored using the `authorize("calendly")` function
#' @param count The number of responses that should be returned. Default is 20 or you can say "all" to retrieve all.
#' @return Calendly REST API response as a list
#' @importFrom utils menu installed.packages
#' @importFrom httr oauth_app oauth_endpoints oauth2.0_token
#' @export
#' @examples \dontrun{
#'
#' authorize("calendly")
#' user <- get_calendly_user()
#' list_calendly_events(user = user$resource$uri)
#' }
#'
list_calendly_events <- function(user, count = 100) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you forgot to add api_key as an argument:

Suggested change
list_calendly_events <- function(user, count = 100) {
list_calendly_events <- function(api_key, user, count = 100) {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this missing argument.

token <- get_token(app_name = "calendly")

# Only can handle requests with 100 at a time
request_count <- ifelse(count > 100, 100, count)

# Declare URL
result_list <- calendly_get(
url = "https://api.calendly.com/scheduled_events",
token = token,
user = user,
count = request_count
)

if (count > result_list$pagination$count) {
# Set up a while loop for us to store the multiple page requests in
cummulative_pages <- list()
cummulative_pages[[1]] <- result_list$collection
page <- 1

while (!is.null(result_list$pagination$next_page_token)) {
result_list <-
calendly_get(
url = "https://api.calendly.com/scheduled_events",
token = get_token(app_name = "calendly"),
user = user,
count = 100,
page_token = result_list$pagination$next_page_token
)
page <- page + 1
cummulative_pages[[page]] <- result_list$collection
}

event_data <- dplyr::bind_rows(cummulative_pages)
} else {
# if less than 100 events were requested then we don't have any page stuff
# to do, just return the first list
event_data <- result_list$collection
}
return(event_data)
}
Loading