PMassicotte / gtrendsR

R functions to perform and display Google Trends queries
355 stars 112 forks source link

Adding additional proxy settings #339

Open alfirrell opened 4 years ago

alfirrell commented 4 years ago

Is it possible to include a more generic way to set curl options? We have a need to supply additional curl options to those given in setHandleParameters() and get_api_cookies(), and to set the proxyuserpwdwithout a domain prefix.

Happy to submit a PR with an option, e.g. a setHandle <- function(handle) taking an arbitrary curl handle created by the user and assigning straight into .pkgenv[["cookie_handler"]]

PMassicotte commented 4 years ago

@eddelbuettel any thoughts?

eddelbuettel commented 4 years ago

Not an area I know that much about. As long as it is optional behavior I guess we could.

@alfirrell Can you also cook up a use case that needs it? Without, of course, exposing any tokens etc you may need?

alfirrell commented 4 years ago

Use-case wise, some specifics we'd want are to be able to specify timeout within the curl options, and to omit the domain. So effectively the curl handle we want is

example_timeout <- 60
# set options for the proxy
curl::handle_setopt(handle=cookie_handler,
                    .list=list(ssl_verifypeer=0L,
                               proxyuserpwd=paste0(.pkgenv[["handle_user"]],
                                                   ":",
                                                   .pkgenv[["handle_password"]]),
                               proxyauth=.pkgenv[["handle_proxyauth"]],
                               proxy=.pkgenv[["handle_proxyhost"]],
                               proxyport=.pkgenv[["handle_proxyport"]], 
                               timeout=example_timeout))

Could deal with the omitted domain issue by handling nulls better in the get_api_cookies (i.e. in the paste0, omit the '\\' if the domain is NULL). And could add timeout as another specific parameter in setHandleParameters(), but I wonder if it's easier to add a extra_curl_opts list of name-value pairs to pick up any extra curl params, which can the be passed direct into the .list in handle_setopt()

e.g.

setHandleParameters <- function(user = NULL, password = NULL, domain = NULL, proxyhost = NULL, proxyport = 8080, proxyauth = 15, extra_curl_opts = list()) {
  #assign all the params, plus...
  .pkgenv[["handle_extra_curl_opts"]] <- extra_curl_opts 
}

called with e.g. setHandleParameters("user", "pwd", extra_curl_opts = list(timeout = 60)) and then

cookie_handler <- curl::new_handle()
# set options for the proxy
# handle missing domain
proxy_domain <- ifelse(is.null(.pkgenv[["handle_domain"]]), "", paste0(.pkgenv[["handle_domain"]],"\\"))
proxy_user_pwd <- paste0(proxy_domain, .pkgenv[["handle_user"]],":",.pkgenv[["handle_password"]])

options_list <- list(ssl_verifypeer=0L, 
                     proxyuserpwd=proxy_user_pwd, 
                     proxyauth=.pkgenv[["handle_proxyauth"]],
                     proxy=.pkgenv[["handle_proxyhost"]],
                     proxyport=.pkgenv[["handle_proxyport"]])
options_list <- append(options_list, .pkgenv[["handle_extra_curl_opts"]])

curl::handle_setopt(handle=cookie_handler, .list=options_list)
cookie_req <- curl::curl_fetch_memory(cookie_url, handle = cookie_handler)