rapiclient icon indicating copy to clipboard operation
rapiclient copied to clipboard

How to substitute for fields in an endpoint path?

Open the-mad-statter opened this issue 3 years ago • 1 comments

Sometimes API endpoints are defined such that they have fields contained in the path that need to be substituted for prior to issuing the request.

For example:

library(rapiclient)

fs_api <- get_api("https://docs.figshare.com/swagger.json")
header <- c(Authorization = sprintf("token %s", Sys.getenv("RFIGSHARE_PAT")))
client <- list(operations = get_operations(fs_api, header), 
               schemas = get_schemas(fs_api))

attr(client$operations$private_article_upload_initiate, "definition")$path
#> [1] "/account/articles/{article_id}/files"

Created on 2021-06-11 by the reprex package (v1.0.0)

Does rapiclient work with API endpoints specified like that? If so, how does one substitute for {article_id} prior to issuing the request?

the-mad-statter avatar Jun 11 '21 23:06 the-mad-statter

I'm late to the party but I had the same situation and question. Passing a string of what I wanted to become the url parameter worked for me. I tried a second string and it also worked, passing an integer made the api mad (it didn't return results)

With verbose on I noticed the user agent was libcurl. I set it to this repo so the people on the receiving end will know how the request was generated. I think this awesome package should do that automatically!

library(rapiclient)

pview_api <- get_api("https://search.patentsview.org/static/openapi_v2.yml")
pview_ops <- get_operations(pview_api,  handle_response = content_or_stop, 
   .headers = c("X-Api-Key" = Sys.getenv("PATENTSVIEW_API_KEY"), 
                "User-Agent" = "https://github.com/bergant/rapiclient"))

client <- list(operations = pview_ops, schemas = get_schemas(pview_api))

attr(client$operations$retrievePatentDetail, "definition")$path
#> [1] "/api/v1/patent/{patent_number}/"

httr::with_verbose({
   result <- pview_ops$retrievePatentDetail("10000000")
})

-> GET /api/v1/patent/10000000/ HTTP/1.1
-> Host: search.patentsview.org
-> Accept-Encoding: deflate, gzip
-> Content-Type: application/json
-> Accept: application/json
-> X-Api-Key: my_api_key_was_here
-> User-Agent: https://github.com/bergant/rapiclient
->
<- HTTP/1.1 200 OK

result
$patents[[1]]$patent_number
[1] "10000000"

$patents[[1]]$patent_title
[1] "Coherent LADAR using intra-pixel quadrature detection"

(truncated)

mustberuss avatar Jul 23 '22 22:07 mustberuss