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

Adds bcdc_list_organizations() & bcdc_list_organization_records() (#322) #323

Merged
merged 12 commits into from
Mar 16, 2023
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export(bcdc_get_record)
export(bcdc_list)
export(bcdc_list_group_records)
export(bcdc_list_groups)
export(bcdc_list_organization_records)
export(bcdc_list_organizations)
export(bcdc_options)
export(bcdc_preview)
export(bcdc_query_geodata)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Add `jsonlite::read_json()` as a file read method, so users can now download & read `json` resources in B.C. Data Catalogue records
* Change the `download_audience` default from `Public` to `NULL` in `bcdc_search()` (#315)
* Fix bug where some/all facet values in `bcdc_search()` need to be quoted to generate a valid API query (#315)
* Add new functions `bcdc_list_organizations` and `bcdc_list_organization_records` as helper functions for finding records #322

# bcdata 0.4.0

Expand Down
50 changes: 50 additions & 0 deletions R/bcdc_search.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ bcdc_list_group_records <- function(group) {
cli <- bcdc_catalogue_client("action/group_show")

r <- cli$get(query = list(id = group, include_datasets = 'true'))

if (r$status_code == 404){
stop("404: URL not found - you may have specified an invalid group?")
stephhazlitt marked this conversation as resolved.
Show resolved Hide resolved
}

r$raise_for_status()

res <- jsonlite::fromJSON(r$parse("UTF-8"))
Expand All @@ -93,6 +98,46 @@ bcdc_list_group_records <- function(group) {

}

#' @export
#' @describeIn bcdc_list_organization_records
#'
bcdc_list_organizations <- function() bcdc_search_facets("organization")

#' Retrieve organization information for B.C. Data Catalogue
#'
#' Returns a tibble of organizations or records. Organizations can be viewed here:
#' https://catalogue.data.gov.bc.ca/organizations or accessed directly from R using `bcdc_list_organizations`
#'
#' @param organization Name of the organization
#' @export
#' @examples
#' \donttest{
#' try(
#' bcdc_list_organization_records('bc-stats')
#' )
#' }

bcdc_list_organization_records <- function(organization) {
if(!has_internet()) stop("No access to internet", call. = FALSE) # nocov

cli <- bcdc_catalogue_client("action/organization_show")

r <- cli$get(query = list(id = organization, include_datasets = 'true'))

if (r$status_code == 404){
stop("404: URL not found - you may have specified an invalid organization?")
}

r$raise_for_status()
stephhazlitt marked this conversation as resolved.
Show resolved Hide resolved

res <- jsonlite::fromJSON(r$parse("UTF-8"))
stopifnot(res$success)

d <- tibble::as_tibble(res$result$packages)
as.bcdc_organization(d, description = res$result$description)

}

#' Return a full list of the names of B.C. Data Catalogue records
#'
#' @return A character vector of the names of B.C. Data Catalogue records
Expand Down Expand Up @@ -318,6 +363,11 @@ as.bcdc_group <- function(x, description) {
description = description)
}

as.bcdc_organization <- function(x, description) {
structure(x,
class = c("bcdc_organization", setdiff(class(x), "bcdc_organization")),
description = description)
}

#' Provide a data frame containing the metadata for all resources from a single B.C. Data Catalogue record
#'
Expand Down
2 changes: 2 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ reference:
- '`bcdc_tidy_resources`'
- '`bcdc_list_groups`'
- '`bcdc_list_group_records`'
- '`bcdc_list_organizations`'
- '`bcdc_list_organization_records`'
- '`bcdc_get_citation`'
- title: "Direct downloads of data sets"
desc: >
Expand Down
30 changes: 30 additions & 0 deletions man/bcdc_list_organization_records.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions tests/testthat/test-get_record.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ test_that("bcdc_list_groups", {
expect_s3_class(bcdc_list_groups(), "data.frame")
})

test_that("bcdc_list_organization_records works", {
skip_on_cran()
skip_if_net_down()

expect_s3_class(bcdc_list_organization_records('bc-stats'), "bcdc_organization")
expect_s3_class(bcdc_list_organization_records('bc-stats'), "tbl_df")

})

test_that("bcdc_list_organizations", {
skip_on_cran()
skip_if_net_down()

expect_s3_class(bcdc_list_organizations(), "data.frame")
})

test_that("bcdc_list works", {
skip_on_cran()
skip_if_net_down()
Expand Down