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

Creating base get_meta() function to retrieve data set meta data #19

Merged
merged 26 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
55e140c
Added initial get_meta() function
rmbielby Sep 2, 2024
c903c56
Expanded get_meta function to extract to json parsed structured list
rmbielby Sep 4, 2024
1dc3ca4
Merge branch 'main' into get-meta
rmbielby Sep 5, 2024
4ed41cc
Added tests for get_meta and helper_functions
rmbielby Sep 5, 2024
c106aad
Code tidying
rmbielby Sep 5, 2024
1c6aec4
Explicitly calling eesyapi functions where used
rmbielby Sep 5, 2024
69be728
Added validation to get_meta
rmbielby Sep 5, 2024
3ee8a57
Added title to get_meta function
rmbielby Sep 5, 2024
1b41720
Updating description for get_meta() and a few other minor tweaks
rmbielby Sep 6, 2024
8f146ad
Produced parsing functions to process filter info from the API repson…
rmbielby Sep 6, 2024
bbc1d88
Updated tests for errors on get_meta()
rmbielby Sep 6, 2024
a28987a
Added location parsing to the meta data retrieval
rmbielby Sep 6, 2024
7543739
Updating version number
rmbielby Sep 6, 2024
4b27e59
Add NEWS.md
rmbielby Sep 6, 2024
8dc5ea4
Updated news.md with changes in latest version update
rmbielby Sep 6, 2024
39da35f
Clearing out some lintr issues around how columns are defined in dply…
rmbielby Sep 6, 2024
dea3484
Updated with a few changes from another branch including some lintr s…
rmbielby Sep 10, 2024
5bdb7bd
Responding to some PR comments - added ordering to time periods in me…
rmbielby Sep 10, 2024
7d6d410
Fixing a bug in time periods parsing
rmbielby Sep 10, 2024
b58572b
Restructured reference list in documentation
rmbielby Sep 10, 2024
a4642e7
Bit of minor rephrasing in the reference list
rmbielby Sep 10, 2024
89976d2
Added test meta data and added test to check time period parsing
rmbielby Sep 10, 2024
4b3eeeb
Added testdata for meta data parsing and created associated new tests
rmbielby Sep 10, 2024
da74ace
Fixed a bug in http_request_error and improved its functionality gene…
rmbielby Sep 10, 2024
effd920
Adapted a few function titles to be more informative
rmbielby Sep 10, 2024
9fe47cd
fix inconsistent full stops in reference list
cjrace Sep 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ Package: eesyapi
Title: EES-y API
Version: 0.0.0.9000
Authors@R:
c(
person("Rich", "Bielby", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-9070-9969"))
comment = c(ORCID = "0000-0001-9070-9969")),
person("Cam", "Race", , "[email protected]", role = "aut"))
cjrace marked this conversation as resolved.
Show resolved Hide resolved
Description: An R package with useful utility functions for connecting to, and
cjrace marked this conversation as resolved.
Show resolved Hide resolved
processing data from, the DfE's Explore Education Statistics API.
License: MIT + file LICENSE
Expand All @@ -15,4 +17,5 @@ Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
Imports:
httr
httr,
jsonlite
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(eesapi_url)
export(get_meta)
export(http_request_error)
107 changes: 107 additions & 0 deletions R/get_meta.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#' Get a data set's metadata
#'
#' @description
#' Get a list of metadata information for a data set available from the EES API. Provides either
#' look-up tables from human readable labels to ids used in the API, or the raw response from the
#' meta endpoint.
#'
cjrace marked this conversation as resolved.
Show resolved Hide resolved
#' @param dataset_id ID of data set to be connected to
#' @param dataset_version Version of data set to be connected to
#' @param api_version EES API version
#' @param parse Parse result into structured list
#'
#' @return Results of query to meta data
#' @export
#'
#' @examples
#' get_meta("d7329101-f275-d277-bbfe-d8cfaa709833")
get_meta <- function(dataset_id, dataset_version = NULL, api_version = NULL, parse = TRUE) {
# Check that the parse flag is valid
if (is.logical(parse) == FALSE) {
stop(
"You have entered an invalid parse argument, this should be a logical TRUE or FALSE only."
)
}

# Use eesyapi_url to retrieve the relevant api url - note that this will perform
# validation checks on dataset_id, dataset_version and api_version, so haven't
# added any explicit validation of those to the current function.
meta_url <- eesyapi::eesapi_url(
endpoint = "get-meta",
dataset_id = dataset_id,
dataset_version = dataset_version
)

response <- httr::GET(meta_url)
if (response$status_code > 299) {
stop(paste0(
"Query returned error, status: ",
response$status,
"\n ",
eesyapi::http_request_error(response$status)
))
} else {
if (parse) {
result <- response |>
httr::content("text") |>
jsonlite::fromJSON()
} else {
result <- response
}
return(result)
}
}

#' Parse API meta to give the filter columns
#'
#' @param api_meta_filters Filter information provided by the API output
#'
#' @return data frame containing column names and labels
#' @export
#'
#' @examples
parse_meta_filter_columns <- function(api_meta_filters) {
data.frame(
col_name = api_meta_filters$id,
col_label = api_meta_filters$label
)
}

#' Parse API meta to give the filter item codes
#'
#' @param api_meta_filters Filter information provided by the API output
#'
#' @return Data frame containing filter item codes matched to filter item labels and col_name
#' @export
#'
#' @examples
parse_meta_filter_item_codes <- function(api_meta_filters) {
nfilters <- length(api_meta_filters$id)
filter_items <- data.frame(
col_name = NA,
item_code = NA,
item_label = NA,
isAggregate = NA
) |>
dplyr::filter(!is.na(col_name))
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved
for (i in 1:nfilters) {
filter_items_i <- as.data.frame(
api_meta_filters$options[i]
) |>
dplyr::rename(
item_code = id,
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
item_label = label
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
) %>%
Fixed Show fixed Hide fixed
dplyr::mutate(col_name = api_meta_filters$id[i])
if (!("isAggregate" %in% names(filter_items_i))) {
filter_items_i <- filter_items_i |>
dplyr::mutate(isAggregate = NA)
}
filter_items <- filter_items |>
rbind(
filter_items_i |>
dplyr::select(col_name, item_label, item_code, isAggregate)
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
)
}
return(filter_items)
}
20 changes: 20 additions & 0 deletions R/helper_functions.R
cjrace marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#' http request error
#'
#' @description
#' Translate a http error code into an error message.
#'
#' @param response_status Response code from an API request
#'
#' @return Translation of the response code
#' @export
#'
#' @examples
#' http_request_error(200)
http_request_error <- function(response_status) {
status_lookup <- list(
"200" = "Successful API request.",
"400" = "Invalid query, data set ID, data set version or API version submitted to API.",
"500" = "Internal server error encountered."
)
status_lookup[format(round(response_status, -1))][[1]]
}
4 changes: 2 additions & 2 deletions man/eesapi_url.Rd

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

26 changes: 26 additions & 0 deletions man/get_meta.Rd

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

20 changes: 20 additions & 0 deletions man/http_request_error.Rd

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

23 changes: 23 additions & 0 deletions tests/testthat/test-get_meta.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
test_that("Dataset ID not found", {
expect_error(get_meta("this-is-not-a-dataset"))
})

test_that("Non-logical parse flag given", {
expect_error(
get_meta("this-is-not-a-dataset", parse = 1),
"You have entered an invalid parse argument, this should be a logical TRUE or FALSE only."
)
})


# This test uses an existing test data set ID from EES admin. So this test could
# fail if either the code is broken, the data set gets taken down or the dev
# site is down. I guess this isn't totally within the usual principles of
# package testing, but putting in for now partially as a useful check on some
# of the above collectively.
test_that("Meta query runs successfully", {
expect_equal(
get_meta("d7329101-f275-d277-bbfe-d8cfaa709833", parse = FALSE)$status,
200
)
})
14 changes: 14 additions & 0 deletions tests/testthat/test-helper_functions.R
cjrace marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
test_that("Successful connection message", {
cjrace marked this conversation as resolved.
Show resolved Hide resolved
expect_equal(http_request_error(200), "Successful API request.")
})

test_that("Bad URL / query message", {
expect_equal(
http_request_error(404),
"Invalid query, data set ID, data set version or API version submitted to API."
)
})

test_that("Server error message", {
expect_equal(http_request_error(503), "Internal server error encountered.")
})
Loading