Skip to content

Commit

Permalink
Handle responses with empty users
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaspons committed Dec 22, 2023
1 parent 0e11857 commit 6a78f24
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
5 changes: 4 additions & 1 deletion R/osm_get_user_details.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ osm_get_user_details <- function(user_id, format = c("R", "xml", "json")) {
format <- match.arg(format)

if (length(user_id) == 1) {
out <- osm_details_user(user_id = user_id, format = format)
out <- try(osm_details_user(user_id = user_id, format = format))
if (inherits(out, "try-error")) { # ! HTTP 404 Not Found. Different from osm_details_users()
out <- empty_user()
}
} else {
out <- osm_details_users(user_ids = user_id, format = format)
}
Expand Down
16 changes: 16 additions & 0 deletions R/xml_to_R.R
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ empty_gpx <- function() {
user_details_xml2DF <- function(xml) {
users <- xml2::xml_children(xml)

if (length(users) == 0) {
return(empty_user())
}

user_attrs <- do.call(rbind, xml2::xml_attrs(users))
description <- xml2::xml_text(xml2::xml_child(users, "description"))
img <- xml2::xml_attr(xml2::xml_child(users, "img"), "href")
Expand Down Expand Up @@ -406,6 +410,18 @@ user_details_xml2DF <- function(xml) {
}


empty_user <- function() {
out <- data.frame(
id = character(), display_name = character(), account_created = as.POSIXct(Sys.time())[-1],
description = character(), img = character(), contributor_terms = logical(), roles = character(),
changesets_count = integer(), traces_count = integer(), blocks_received.count = integer(),
blocks_received.active = integer(), blocks_issued.count = integer(), blocks_issued.active = integer()
)

return(out)
}


logged_user_details_xml2list <- function(xml) {
user <- xml2::xml_child(xml)

Expand Down
34 changes: 34 additions & 0 deletions tests/testthat/test-user_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ test_that("osm_details_user works", {

# Check that time is extracted, otherwise it's 00:00:00 in local time
lapply(usr, function(x) expect_false(all(strftime(as.POSIXct(x$account_created), format = "%M:%S") == "00:00")))


## Empty results

with_mock_dir("mock_details_user_empty", {
empty_usr <- osm_get_user_details(user_id = 2)
})

expect_s3_class(empty_usr, "data.frame")
expect_named(empty_usr, column_users)
expect_identical(nrow(empty_usr), 0L)

mapply(
function(x, cl) expect_true(inherits(x, cl)),
x = empty_usr,
cl = class_columns[names(empty_usr)]
)
})


Expand All @@ -50,6 +67,23 @@ test_that("osm_details_users works", {

# Check that time is extracted, otherwise it's 00:00:00 in local time
lapply(usrs, function(x) expect_false(all(strftime(as.POSIXct(x$account_created), format = "%M:%S") == "00:00")))


## Empty results

with_mock_dir("mock_details_users_empty", {
empty_usrs <- osm_get_user_details(user_id = 2:3)
})

expect_s3_class(empty_usrs, "data.frame")
expect_named(empty_usrs, column_users)
expect_identical(nrow(empty_usrs), 0L)

mapply(
function(x, cl) expect_true(inherits(x, cl)),
x = empty_usrs,
cl = class_columns[names(empty_usrs)]
)
})


Expand Down

0 comments on commit 6a78f24

Please sign in to comment.