httr2 icon indicating copy to clipboard operation
httr2 copied to clipboard

httr2 equivalent of httr::set_config ?

Open cole-brokamp opened this issue 1 year ago • 10 comments

https://httr2.r-lib.org/reference/index.html

Is there an equivalent way to set global configurations that will be used by all requests?

I'm specifically looking for a way for an R user to set a proxy configuration that would apply to all requests made in an R session.

cole-brokamp avatar Feb 05 '24 17:02 cole-brokamp

There's nothing equivalent, but maybe there should be, just for proxies.

hadley avatar Feb 05 '24 17:02 hadley

Thanks for confirming! I'll stick with httr for this purpose for now.

cole-brokamp avatar Feb 05 '24 18:02 cole-brokamp

How would you see this being implemented here? I would love to help if possible.

cole-brokamp avatar Feb 05 '24 19:02 cole-brokamp

I don't know yet; I need to think about it.

hadley avatar Feb 05 '24 19:02 hadley

Hello, I am glad this issue already exists. I would like to second the motion to add something similar to httr::set_config. I use it a lot in scripts where I work with API wrappers based on httr. However, now I am set to work with a R package API wrapper based on httr2 which at the moment does not provide support to add options to the underlying HTTP request through its API, making it necessary to have some way to globally set the configuration.

And in addition to that: Thank you so, so much for all your amazing work here, Hadley!

yannikbuhl avatar Aug 06 '24 15:08 yannikbuhl

Out of interest, what sort of options are you looking to set?

hadley avatar Aug 06 '24 17:08 hadley

In order to configure my proxy settings I need the following:

  • Proxy URL
  • Username / password
  • Authentication method (e.g., NTLM)

It works totally fine used in req_options or req_proxy, however using the package (restatis in my case) I cannot pass these on to the httr2 pipeline used in a non-exported function (we might add support for that later but it would be great if there was a way to globally set the above settings).

yannikbuhl avatar Aug 06 '24 18:08 yannikbuhl

Thanks! Definitely something I plan to add when I'm next working on httr2.

hadley avatar Aug 07 '24 12:08 hadley

Would also be useful if you want to set up caching.

hadley avatar Aug 19 '24 00:08 hadley

Since req_throttle() and req_retry() already do this, I wonder if we should have the ability to set options per hostname. That feels like it would be useful in order to add additional options to an arbitrary package, without affecting every single call.

Maybe something like this?

host_config("google.org", \(req) req |> req_set_header("foo"))
host_config("*", \(req) req |> req_proxy_something())

Would then need some kind of host matching algorithm but maybe it's possible to hack something together with regexps that's good enough?

hostname_to_regex <- function(x) {
  x <- gsub(".", "\\\\.", x, fixed = TRUE)
  x <- gsub("*", ".*", x, fixed = TRUE)
  x <- paste0("^", x, "$")
  x
}

hadley avatar Dec 24 '24 16:12 hadley