bergant / rapiclient

Dynamic Open API (Swagger) Client for R
Other
66 stars 18 forks source link

get_api doesn't properly use config parameters #18

Closed schaffman5 closed 1 month ago

schaffman5 commented 2 years ago

There is an issue in the get_api function when the API requires specific headers to be set in order to view the page content. An example is when the API requires a token to be set in the httr::config header before it will return the API details.

The provided example does not work likely because of this issue, e.g.

nyt_api <- get_api("http://developer.nytimes.com/top_stories_v2.json/swagger.json")

To resolve this, the get_api function should, itself, use the config parameter when requesting content from the api url. e.g.

api <- tryCatch({
    # new code to properly handle the config parameters instead of just
    # calling jsonlite::fromJSON directly with the url and no other parameters
    res<-httr::GET(url, type="text/json", config=config)
    res.json<-httr::content(res, as="text", encoding="UTF-8")
    jsonlite::fromJSON(res.json, simplifyDataFrame = FALSE)
    # end of new code

    # old code:
    # jsonlite::fromJSON(url, simplifyDataFrame = FALSE)
  }, error = function(x) NULL)
eblondel commented 2 years ago

thanks @schaffman5 I have the same issue on my side trying to read api spec from IATI https://iatistandard.org/ that is versioning the APIs, and that allows getting the specification through a GET request that needs a specific header ie. Accept: application/vnd.swagger.doc+json @bergant is it ok for that I send a pull request for this enhancement? If you need integration tests let me know

LiNk-NY commented 1 month ago

Sorry for the delay. Please tag me for future issues. Resolved in be89a8f5a963becc5f3f0862cf702fac70d6c789