Skip to content

Commit

Permalink
Mass switch of functions to internal
Browse files Browse the repository at this point in the history
  • Loading branch information
rmbielby committed Jan 17, 2025
1 parent ae8dfda commit e5d3407
Show file tree
Hide file tree
Showing 41 changed files with 166 additions and 165 deletions.
21 changes: 0 additions & 21 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,34 +1,13 @@
# Generated by roxygen2: do not edit by hand

export(api_url)
export(api_url_pages)
export(api_url_query)
export(convert_api_filter_type)
export(example_data_raw)
export(example_geography_query)
export(example_id)
export(example_json_query)
export(get_data_catalogue)
export(get_meta)
export(get_meta_response)
export(get_publications)
export(http_request_error)
export(parse_api_dataset)
export(parse_meta_filter_columns)
export(parse_meta_filter_item_ids)
export(parse_meta_location_ids)
export(parse_meta_time_periods)
export(parse_sqids_filters)
export(parse_sqids_indicators)
export(parse_sqids_locations)
export(parse_tourl_filter_in)
export(preview_dataset)
export(query_dataset)
export(validate_api_version)
export(validate_ees_environment)
export(validate_ees_filter_type)
export(validate_ees_id)
export(validate_page_size)
export(validate_time_periods)
export(warning_max_pages)
importFrom(data.table,":=")
10 changes: 5 additions & 5 deletions R/api_url.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ api_url <- function(

# Check that if endpoint requires a data set then dataset_id is not null
if (endpoint %in% c("get-summary", "get-meta", "get-csv", "get-data", "post-data")) {
eesyapi::validate_ees_id(dataset_id, level = "dataset")
validate_ees_id(dataset_id, level = "dataset")
if (is_valid_dataset_info(dataset_id, dataset_version) == FALSE) {
stop(
paste(
Expand Down Expand Up @@ -124,15 +124,15 @@ api_url <- function(
url <- paste0(
endpoint_base_version,
"publications?",
eesyapi::api_url_pages(page_size = page_size, page = page)
api_url_pages(page_size = page_size, page = page)
)
} else if (endpoint == "get-data-catalogue") {
url <- paste0(
endpoint_base_version,
"publications/",
publication_id,
"/data-sets?",
eesyapi::api_url_pages(page_size = page_size, page = page)
api_url_pages(page_size = page_size, page = page)
)
} else {
url <- paste0(
Expand Down Expand Up @@ -173,7 +173,7 @@ api_url <- function(
}
url <- url |>
paste0(
eesyapi::api_url_query(
api_url_query(
indicators = indicators,
time_periods = time_periods,
geographic_levels = geographic_levels,
Expand All @@ -182,7 +182,7 @@ api_url <- function(
),
ifelse(
!is.null(page) & !is.null(page_size),
paste0("&", eesyapi::api_url_pages(page_size = page_size, page = page)),
paste0("&", api_url_pages(page_size = page_size, page = page)),
""
)
)
Expand Down
7 changes: 4 additions & 3 deletions R/api_url_pages.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
#' @param page Page number to return
#'
#' @return String containing pages query
#' @export
#'
#' @keywords internal
#'
#' @examples
#' api_url_pages()
#' api_url_pages(page_size = 20, page = 2)
#' eesyapi:::api_url_pages()
#' eesyapi:::api_url_pages(page_size = 20, page = 2)
api_url_pages <- function(page_size = 40, page = NULL) {
paste0(
ifelse(
Expand Down
21 changes: 11 additions & 10 deletions R/api_url_query.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
#' @param filter_items Filter items required as a string or vector of strings
#'
#' @return String containing data query string to append to GET data query URL
#' @export
#'
#' @keywords internal
#'
#' @examples
#' api_url_query(example_id("indicator"))
#' eesyapi:::api_url_query(example_id("indicator"))
api_url_query <- function(
indicators,
time_periods = NULL,
Expand All @@ -24,37 +25,37 @@ api_url_query <- function(
if (is.null(indicators)) {
stop("The keyword indicators must be supplied")
}
eesyapi::validate_ees_id(indicators, level = "indicator")
validate_ees_id(indicators, level = "indicator")
# Create the appropriate query strings for each level provided
if (!is.null(time_periods)) {
query_time_periods <- eesyapi::parse_tourl_filter_in(time_periods, "time_periods")
query_time_periods <- parse_tourl_filter_in(time_periods, "time_periods")
}
if (!is.null(geographic_levels)) {
query_geographic_levels <- eesyapi::parse_tourl_filter_in(
query_geographic_levels <- parse_tourl_filter_in(
geographic_levels,
filter_type = "geographic_levels"
)
}
if (!is.null(locations)) {
eesyapi::validate_ees_id(locations, level = "location")
query_locations <- eesyapi::parse_tourl_filter_in(locations, filter_type = "locations")
validate_ees_id(locations, level = "location")
query_locations <- parse_tourl_filter_in(locations, filter_type = "locations")
}
if (!is.null(filter_items)) {
# Note the idea below was to differentiate the logic between AND / OR based on whether
# a list of vectors is provided or a single vector. Due to limitations with GET, this
# set up doesn't make a blind bit of difference to the result, the query just performs
# an OR combination regardless.
eesyapi::validate_ees_id(filter_items, level = "filter_item")
validate_ees_id(filter_items, level = "filter_item")
if (filter_items |> typeof() == "list") {
query_filter_items <- ""
for (filter_set in filter_items) {
query_filter_items <- paste0(
query_filter_items,
eesyapi::parse_tourl_filter_in(filter_set, filter_type = "filter_items")
parse_tourl_filter_in(filter_set, filter_type = "filter_items")
)
}
} else {
query_filter_items <- eesyapi::parse_tourl_filter_in(
query_filter_items <- parse_tourl_filter_in(
filter_items,
filter_type = "filter_items"
)
Expand Down
9 changes: 5 additions & 4 deletions R/api_url_query_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
#' "locations" or "filter_items"
#'
#' @return Query string for use in URL based API queries
#' @export
#'
#' @keywords internal
#'
#' @examples
#' parse_tourl_filter_in(c("2024|W11", "2024|W12"), filter_type = "time_periods")
#' eesyapi:::parse_tourl_filter_in(c("2024|W11", "2024|W12"), filter_type = "time_periods")
parse_tourl_filter_in <- function(
items,
filter_type) {
eesyapi::validate_ees_filter_type(filter_type)
type_string <- eesyapi::convert_api_filter_type(filter_type)
validate_ees_filter_type(filter_type)
type_string <- convert_api_filter_type(filter_type)
if (!is.null(items)) {
if (filter_type %in% c("time_period", "locations")) {
items <- gsub("\\|", "%7C", items)
Expand Down
13 changes: 7 additions & 6 deletions R/convert_api_filter_type.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
#' @inheritParams parse_tourl_filter_in
#'
#' @return String containing API friendly filter type descriptor
#' @export
#'
#' @keywords internal
#'
#' @examples
#' convert_api_filter_type("filter_items")
#' convert_api_filter_type("geographic_levels")
#' convert_api_filter_type("locations")
#' convert_api_filter_type("filter_items")
#' eesyapi:::convert_api_filter_type("filter_items")
#' eesyapi:::convert_api_filter_type("geographic_levels")
#' eesyapi:::convert_api_filter_type("locations")
#' eesyapi:::convert_api_filter_type("filter_items")
convert_api_filter_type <- function(filter_type) {
eesyapi::validate_ees_filter_type(filter_type)
validate_ees_filter_type(filter_type)
filter_type <- filter_type |>
stringr::str_replace("_item", "")
gsub("_(\\w?)", "\\U\\1", filter_type, perl = TRUE)
Expand Down
2 changes: 1 addition & 1 deletion R/examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ example_id <- function(
example_data_raw <- function(
group = "attendance",
size = 32) {
eesyapi::api_url(
api_url(
"get-data",
dataset_id = example_id(group = group),
indicators = example_id("indicator", group = group),
Expand Down
10 changes: 5 additions & 5 deletions R/get_data_catalogue.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ get_data_catalogue <- function(
page = NULL,
verbose = FALSE) {
# Validate input parameters
eesyapi::validate_ees_id(publication_id)
eesyapi::validate_page_size(page_size)
validate_ees_id(publication_id)
validate_page_size(page_size)
# Send the GET call to the API
response <- httr::GET(
eesyapi::api_url(
api_url(
endpoint = "get-data-catalogue",
publication_id = publication_id,
ees_environment = ees_environment,
Expand All @@ -36,7 +36,7 @@ get_data_catalogue <- function(
if (response$paging$totalPages > 1) {
for (page in c(2:response$paging$totalPages)) {
response_page <- httr::GET(
eesyapi::api_url(
api_url(
endpoint = "get-data-catalogue",
publication_id = publication_id,
ees_environment = ees_environment,
Expand All @@ -54,6 +54,6 @@ get_data_catalogue <- function(
}
}
# Check that the query hasn't tried to retrieve results beyond the final page of results
response |> eesyapi::warning_max_pages()
response |> warning_max_pages()
return(response$results)
}
37 changes: 21 additions & 16 deletions R/get_meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ get_meta <- function(
#' @param parse Parse result into structured list
#'
#' @return Results of query to API meta data endpoint
#' @export
#'
#' @keywords internal
#'
#' @examples
#' get_meta_response(example_id())
#' eesyapi:::get_meta_response(example_id())
get_meta_response <- function(
dataset_id,
dataset_version = NULL,
Expand All @@ -66,7 +67,7 @@ get_meta_response <- function(
# 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::api_url(
meta_url <- api_url(
endpoint = "get-meta",
dataset_id = dataset_id,
dataset_version = dataset_version,
Expand All @@ -75,7 +76,7 @@ get_meta_response <- function(
)

response <- httr::GET(meta_url)
eesyapi::http_request_error(response)
http_request_error(response)
if (parse) {
result <- response |>
httr::content("text") |>
Expand All @@ -92,11 +93,12 @@ get_meta_response <- function(
#' @param api_meta_time_periods Time periods information provided by the API output
#'
#' @return Data frame containing location item codes matched
#' @export
#'
#' @keywords internal
#'
#' @examples
#' get_meta_response(example_id())$timePeriods |>
#' parse_meta_time_periods()
#' eesyapi:::get_meta_response(example_id())$timePeriods |>
#' eesyapi:::parse_meta_time_periods()
parse_meta_time_periods <- function(api_meta_time_periods,
verbose = FALSE) {
if (!("code" %in% names(api_meta_time_periods))) {
Expand All @@ -117,11 +119,12 @@ parse_meta_time_periods <- function(api_meta_time_periods,
#' @param api_meta_locations Locations information provided by the API output
#'
#' @return Data frame containing location item codes matched
#' @export
#'
#' @keywords internal
#'
#' @examples
#' get_meta_response(example_id())$locations |>
#' parse_meta_location_ids()
#' eesyapi:::get_meta_response(example_id())$locations |>
#' eesyapi:::parse_meta_location_ids()
parse_meta_location_ids <- function(api_meta_locations,
verbose = FALSE) {
nlevels <- nrow(api_meta_locations$level)
Expand Down Expand Up @@ -158,11 +161,12 @@ parse_meta_location_ids <- function(api_meta_locations,
#' @param api_meta_filters Filter information provided by the API output
#'
#' @return data frame containing column names and labels
#' @export
#'
#' @keywords internal
#'
#' @examples
#' get_meta_response(example_id())$filters |>
#' parse_meta_filter_columns()
#' eesyapi:::get_meta_response(example_id())$filters |>
#' eesyapi:::parse_meta_filter_columns()
parse_meta_filter_columns <- function(api_meta_filters,
verbose = FALSE) {
data.frame(
Expand All @@ -178,11 +182,12 @@ parse_meta_filter_columns <- function(api_meta_filters,
#' @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
#'
#' @keywords internal
#'
#' @examples
#' get_meta_response(example_id())$filters |>
#' parse_meta_filter_item_ids()
#' eesyapi:::get_meta_response(example_id())$filters |>
#' eesyapi:::parse_meta_filter_item_ids()
parse_meta_filter_item_ids <- function(api_meta_filters,
verbose = FALSE) {
nfilters <- length(api_meta_filters$id)
Expand Down
8 changes: 4 additions & 4 deletions R/get_publications.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ get_publications <- function(
page_size = 40,
page = NULL,
verbose = FALSE) {
eesyapi::validate_page_size(page_size)
validate_page_size(page_size)
response <- httr::GET(
eesyapi::api_url(
api_url(
ees_environment = ees_environment,
api_version = api_version,
page_size = page_size,
Expand All @@ -30,7 +30,7 @@ get_publications <- function(
if (response$paging$totalPages > 1) {
for (page in c(2:response$paging$totalPages)) {
response_page <- httr::GET(
eesyapi::api_url(
api_url(
ees_environment = ees_environment,
api_version = api_version,
page_size = page_size,
Expand All @@ -45,6 +45,6 @@ get_publications <- function(
}
}
}
response |> eesyapi::warning_max_pages()
response |> warning_max_pages()
return(response$results)
}
5 changes: 3 additions & 2 deletions R/http_request_error.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
#'
#'
#' @return Translation of the response code
#' @export
#'
#' @keywords internal
#'
#' @examples
#' http_request_error(list(status = 200))
#' eesyapi:::http_request_error(list(status = 200))
http_request_error <- function(
response,
verbose = FALSE) {
Expand Down
Loading

0 comments on commit e5d3407

Please sign in to comment.