-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 3 commits
e5f6609
ac2318f
decac40
29092c7
7dcdf7e
58f2472
ce082ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||||||||||||||||||||||||||||
#' @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 | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confused with the argument
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The function needs a default of NULL for the example on line 56 to work. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
To improve the API design, I suggest adding a message telling the user that we will be using the user-supplied token from There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you forgot to add
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
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.