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

Add approve_interview() function that mirrors reject_interview() #1

Open
arthur-shaw opened this issue Jun 22, 2022 · 0 comments
Open

Comments

@arthur-shaw
Copy link
Owner

Code from an older effort:

approve_interview <- function(
	server,
	user, 
	password,
	interviewId,
	status,
	comment
) {

	# load necessary libraries

	# packages needed for this program 
	packagesNeeded <- c(
		"httr", 	# to talk with the server
		"dplyr",	# to do basic data wrangling
		"readr" 	# to write failure info to disk		
	)

	# identify and install those packages that are not already installed
	packagesToInstall <- packagesNeeded[!(packagesNeeded %in% installed.packages()[,"Package"])]
	if(length(packagesToInstall)) 
		install.packages(packagesToInstall, quiet = TRUE, 
			repos = 'https://cloud.r-project.org/', dep = TRUE)

	# load all needed packages
	lapply(packagesNeeded, library, character.only = TRUE)

	# move to next interview if not in "approvable" status
	if (!status %in% c(100, 120)) {
		next
	}

	# if comment is NA, convert to an empty string for API call
	if (is.na(comment)) {
		comment = ""
	}

	# formulate API call
	call <- paste0(
		"https://", server, ".mysurvey.solutions", 
		"/api/v1/interviews/", interviewId, 
		case_when(
			status == 100 ~ "/approve", 	# ... if Completed
			status == 120 ~ "/hqapprove" # ... if ApprovedBySupervisor
		),
		"?comment=", curlPercentEncode(comment))

	# make request
	request <- PATCH(url = call, config = authenticate(user = user, password = password))

	# make a second, modified request if interview was initially Completed and first request successful
	if (status == 100 & httr::status_code(request) == 200) {
		call_next <- str_replace(call, pattern = "/approve", replacement = "/hqapprove")
		request <- PATCH(url = call_next, config = authenticate(user = user, password = password))
	}

	# check that request worked
	# if so, indicate success
	if (httr::status_code(request) == 200) {

		print(paste0("Success approving interview: ", interviewId))

	# if no, indicate failure, capture reason, and write reason to disk		
	} else {

		print(paste0("Problem approving interview ", interviewId))

		# extract interview info and reason for failure
		approve_failed <- data.frame(
			interview__id = interviewId,
			interview__status = status,
			status_code = httr::status_code(request),
			reason = content(request)$Message,
			stringsAsFactors = FALSE
		)

		# write failure info to disk
		readr::write_csv(
			approve_failed, 
			path = paste0(project_folder, "failed_approvals.csv"),
			append = TRUE
		)		

	}

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant