-
Notifications
You must be signed in to change notification settings - Fork 54
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
Make req_template()
append the path, instead of replace
#133
Comments
Sorry I missed this and just submitted. But could you explain a bit more how you're using |
Well... I'll live :) The problem: When copying from the spec, the common bit is not there, so either the user of my API client has to do extra work pasting it themselves, or the common bit needs to be "injected" into Here is an example: library(httr2)
# I have a high-level function:
nice_request <- function(hostname, template, ..., .env = parent.frame()) {
# This is where MediaWikis expose the core REST v1 API:
server <- "/w/rest.php/v1"
request(hostname) %>%
req_url_path(server) %>%
req_template(template, ..., .env = .env)
}
# I copy the template from the spec (which does not have the common bit):
tmp <- "POST /greeting/{endpoint}"
# But...
nice_request("https://my-wiki.org", tmp, endpoint = "hi")
#> <httr2_request>
#> POST https://my-wiki.org/greeting/hi
#> Body: empty Created on 2022-04-28 by the reprex package (v2.0.1) |
Why not do this?
? |
The "POST" in the beginning ruins it: library(httr2)
nice_request <- function(hostname, template, ..., .env = parent.frame()) {
# This is where MediaWikis expose the core REST v1 API:
server <- "/w/rest.php/v1"
request(hostname) %>%
req_template(paste(server, template), ..., .env = .env)
}
# I copy the template from the spec:
tmp <- "POST /greeting/{endpoint}"
# But...
nice_request("https://my-wiki.org", tmp, endpoint = "hi")
#> Error in `req_template()`:
#> ! Can't parse template `template`
#> ℹ Should have form like 'GET /a/b/c' or 'a/b/c/' Created on 2022-04-28 by the reprex package (v2.0.1) |
I am currently doing something like the below, and it works, so it's not a huge issue. It would just make more sense if it was a native behavior (or perhaps a new library(httr2)
nice_request <- function(hostname, template, ..., .env = parent.frame()) {
template <- sub("(GET |POST )?/?(.*)", "\\1/w/rest.php/v1/\\2", template)
request(hostname) %>%
req_template(template, ..., .env = .env)
}
# I copy the template from the spec:
tmp <- "POST /greeting/{endpoint}"
# But...
nice_request("https://my-wiki.org", tmp, endpoint = "hi")
#> <httr2_request>
#> POST https://my-wiki.org/w/rest.php/v1/greeting/hi
#> Body: empty Created on 2022-04-28 by the reprex package (v2.0.1) |
Ooooh I see what you mean. It feels confusing for httr2 is on CRAN, but is failing on a CRAN machine so I'll have to do a patch release so I could still get this in in the next couple of days. |
Hmmm. To me it's more confusing that Funny how this "replace vs modify" keeps popping up in places. |
Using a templating feels very "set"-like to me, because you're presumably giving it a complete path. You're unlikely to want to call I think the difference between paths and the other places we modify requests is that paths have to append to the end; we can't modify specific components. |
I see your point about templating. I think I'd have the same intuition if Looking at this Open API spec. It has a common path component in its "servers" element, and this has to prepend to individual paths. If I wanted to use templating, I couldn't just create a starting request object with server path included. Instead, I would have to ensure the shared component is added to every template (e.g. via a Even though the logic makes sense, it feels like adding hoops to jump through (compared to just adding to existing path). |
Ok, you've convinced me. Let's make |
Alright! Shall I prepare a PR? |
@jchrom yes please 😄 |
It is somewhat inconvenient that when modifying an existing request, the path gets overwritten wholly. Using
req_url_path_append()
instead ofreq_url_path()
would be more convenient. For example, the MediaWiki REST API has a path component which is common for all endpoints, but it gets overwritten if I usereq_template()
.Could we squeeze this in for 0.2, if it makes sense?
The text was updated successfully, but these errors were encountered: