I’d like to share this new (and small!) package
rgeobon whose goal is to serve as the R interface to the GEOBON data portal. The portal is currently in beta and I am hoping to have it follow the developments of this new portal.
The package currently allows to query for the list of available datasets and to download data from it. Any help / advice / contribution is of course welcomed!
httr::content(request)$code == 404 to check the HTTP status. The
request object you get back from the GET call should have the status code so you don’t have to parse the response just go get the status code, e.g.,
request$status_code. That’s a bit odd that the response body has a status code, but I guess I have seen that before
Hi @sckott thanks for pointing this out,
I’ll change it up!
Actually, I realised there is a reason why I have to parse it to get the correct status code. The API returns the same error code (200) for the a valid and a invalid ID (for instance 100 is not valid, as at this time it does not correspond to a dataset), but when parsed, the status code is actually an error code.
# Good id url <- "https://portal.geobon.org/api/v1/datasets/id/49" request <- httr::GET(url) request$status_code # is 200 ! jsonlite::fromJSON(httr::content(request, as="text", encoding = "UTF-8"), flatten = T)$code # is 200 # Bad id url <- "https://portal.geobon.org/api/v1/datasets/id/100" request <- httr::GET(url) request$status_code # is also 200 ! jsonlite::fromJSON(httr::content(request, as="text", encoding = "UTF-8"), flatten = T)$code # is 404 !
The value of unit testing and
expect_error is once again demonstrated!
Would that be a valid point to bring up to the devs at geobon in charge of the API?
Makes sense now that you have to parse the body to get the status code.
Yes, I would bring it up with the developers.
You might want to just parse the response once to save compute time, e.g., you call
httr::content twice here - you could just call it once before the if statement line checking the status rgeobon/geobon_get.R at c6908ba066260e6d2ee9bd225091a1eb5589444d · VLucet/rgeobon · GitHub
I would bring it up with the developers.
You might want to just parse the response once to save compute time
Good point, thanks. I’ll change this up.