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

Add basic functionality of get_records() #12

Merged
merged 11 commits into from
Nov 14, 2023
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ RoxygenNote: 7.2.3
Imports:
askpass,
assertthat,
dplyr,
httr2,
janitor,
magrittr,
openssl,
purrr
38 changes: 29 additions & 9 deletions R/get_records.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,41 @@ get_records <- function(inspection_name = "Vespa-Watch",
assertthat::assert_that(assertthat::is.string(access_token))
assertthat::assert_that(assertthat::is.string(inspection_name))
# get the inspection_id for the custom inspection
inspection_fields <- get_fields(access_token = access_token,
name = inspection_name)
inspection_fields <- get_fields(
access_token = access_token,
name = inspection_name
)
# build a request and perform it
get_records_request <-
httr2::request("https://api.iasset.nl/getCustomInspections") %>%
httr2::req_body_form(
access_token = access_token,
"inspection_ids[0]" = inspection_fields$id,
version = "9.7"
) %>%
# retry 3 times if it fails, try for 60 seconds per attempt
httr2::req_retry(
max_tries = 3,
max_seconds = 60
)
# records_response <- httr2::req_perform(get_records_request) #%>%
# httr2::req_retry(max_tries = 3,
# max_seconds = 60)
# records <- httr2::resp_body_json(records_response, check_type = FALSE)
#
# records$returndata$data %>%
# purrr::map_dfr(~.x)
# perform the request
records_response <- httr2::req_perform(get_records_request)

# parse the returned JSON
records <-
httr2::resp_body_json(records_response, check_type = FALSE) %>%
## convert into tibble, rename fields with their labels
purrr::chuck("returndata") %>%
# get the data object for every element
purrr::map(~ purrr::chuck(.x, "data")) %>%
# create a table per record
purrr::map_dfr(~ purrr::discard(.x, function(x) all(x == ""))) %>%
# rename field with values from `get_fields()`
dplyr::rename_with(~ inspection_fields$fields$fieldlabel[
match(., inspection_fields$fields$id)
]) %>%
janitor::clean_names()

# output a tibble with the requested records
return(records)
}