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

How to substitute for fields in an endpoint path? #17

Open
the-mad-statter opened this issue Jun 11, 2021 · 1 comment
Open

How to substitute for fields in an endpoint path? #17

the-mad-statter opened this issue Jun 11, 2021 · 1 comment

Comments

@the-mad-statter
Copy link

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 the-mad-statter changed the title How to substitute for fields in an endpoint? How to substitute for fields in an endpoint path? Jun 11, 2021
@mustberuss
Copy link

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)

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

2 participants